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:
I fixed this by using
[SerializeField, HideInInspector]
before the variables I wanted to bake.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
{
}
- Call function
override public void OnInspectorGUI()
then call functions of the typeEditorGUILayout
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]