Home > Back-end >  How to bake or compute my script data before play
How to bake or compute my script data before play

Time:08-30

So, I am working on a unity project that involves a lot of math. It takes about 1-2 seconds for the math to finish on start.

Is there any way to "bake" or "compute" this data before the game starts. I saw something similar done in this script from this project. My project has nothing to do with this script, it is just an example.

I would prefer not to share my code. I need to compute the variables from a method called Initialize()

CodePudding user response:

  1. I fixed this by using [SerializeField, HideInInspector] before the variables I wanted to bake.

  2. Then I would create a new class (can be in the same script). You have to using UnityEditor then your class should be something like

[CustomEditor(typeof(classToBake))]
public class BakeGUI : Editor
{

}
  1. Call function override public void OnInspectorGUI() then call functions of the type EditorGUILayout Then you have to create a variable of the type of script.
using UnityEditor;

[CustomEditor(typeof(classToBake))]
public class BakeGUI: Editor
{
    override public void OnInspectorGUI()
    {
        classToBake bake = (classToBake)target;
        if (GUILayout.Button("Compile Data"))
        {
            bake.Compile();
        }
        if (GUILayout.Button("Delete Data"))
        {
            bake.ResetData();
        }
        DrawDefaultInspector();
    }
}

Finally, how to use this.

  • change classtoBake to the script with the data you want to bake
  • In the if statements, you can run methods when they click the buttons.

The most important thing

After you have this working, to save your data, YOU MUST have the variables you are going to change have the attributes [SerializeField, HideInInspector]
  • Related