I have a static variable in a class, let’s call it StaticClass.ExternalValue. In the editor, I have a button that does the following:
StaticClass.ExternalValue = "Some string! Hello."
UnityEditor.EditorApplication.isPlaying = true;
In other words, it sets the value of the static field, and then starts the game playing. The problem is that the act of starting the game playing seems to cause the static fields to be overwritten with their default values again. How can I pass data from the editor to the game in this way?
As much as i know you have three ways to do that,
1- Use the EditorPrefs to store the value and read the value when playing in static constructor or Awake or somewhere else
2- Write the value to a file and read it back when you are playing
3- Put the values in a ScriptableObject and save them, then use the ScripableObject as an asset in your script.
The reason of overwriting is that unity simulates an executable running from scratch when you press play or go to play mode by code. You can not serialize static variables (not fully sure) so you can not tell the editor to save them.