Unity Editor Window Save Values

Hello Guys, Here is my code,

My editor window

 void OnGUI()
    {
      //bools = CoreGlobal.OnMobile.
        EditorGUILayout.BeginVertical();
        EditorGUILayout.BeginHorizontal();

        EditorGUILayout.EndHorizontal();
        EditorGUILayout.EndVertical();
        title = "CB Core";
        GUILayout.Label("Global Core Settings", EditorStyles.boldLabel);
  
        CoreGlobal.OnMobile = EditorGUILayout.Toggle("Mobile Build", CoreGlobal.OnMobile);
        CoreGlobal.DevelopmentBuild = EditorGUILayout.Toggle("Development Build", CoreGlobal.DevelopmentBuild);

}


       void OnFocus()
        {
            if (EditorPrefs.HasKey("CBC_OnMobile"))
                CoreGlobal.OnMobile = EditorPrefs.GetBool("CBC_OnMobile");
            if (EditorPrefs.HasKey("CBC_DevelopmentBuild"))
                CoreGlobal.DevelopmentBuild = EditorPrefs.GetBool("CBC_DevelopmentBuild");
        }

         void OnLostFocus()
        {
            EditorPrefs.SetBool("CBC_OnMobile", CoreGlobal.OnMobile);
            EditorPrefs.SetBool("CBC_DevelopmentBuild", CoreGlobal.DevelopmentBuild);
        }
         void OnDestroy()
        {
            EditorPrefs.SetBool("CBC_OnMobile", CoreGlobal.OnMobile);
            EditorPrefs.SetBool("CBC_DevelopmentBuild", CoreGlobal.DevelopmentBuild);
        }

My CoreGlobal script

using UnityEngine;
using System.Collections;

public class CoreGlobal{


    [SerializeField]
    private static bool onMobile;
    [SerializeField]
    private static bool developmentBuild;


    public static bool OnMobile
    {
        get
        {
            return onMobile;
        }
        set 
        {
            onMobile = value;
        }
    }

    public static bool DevelopmentBuild
    {
        get
        {
            return developmentBuild;
        }
        set
        {
            developmentBuild = value;
        }
    }

}

Now this works as expected and saved my editor values as I want them. My problem here now is, when I close Unity and Open it, the values aren’t loaded to core global until I open the editor. Here is a screenshot.
47239-screenshot-8.png

I made a custom editor menu,

   [MenuItem("LOG", false, 3)]
    public static void CBQ()
    {
        Debug.Log(CoreGlobal.OnMobile);
    }

to log the value inside my editor windows script. I got false for the value and continued to get false till I launched my editor window from the menu, and then i started getting true.

What are the ways for me to load this value immediately ? This values will be used both in the editor window and the gameplay. They let me decide certain stuffs for building on different platforms and on different settings. I have used this kind of code in my older games without having a custom editor window.

onMobile = CoreGlobal.OnMobile;

Whats the use of having custom editors when I can’t load the values to Build ?

Strangely all who comment on this question didn’t seem to have read the question carefully ^^.

I suggest the following changes:

  • First of all you should move the saving and loading code into your static class. Just add a public static Load and Save method.
  • Those methods can be called from your EditorWindow in order to save / load the values.
  • Add a static constructor to your “CoreGlobal” class and call the Load method from there.
  • Finally add the [InitializeOnLoad] attribute to your static “CoreGlobal” class. This will make Unity initialize the class when Unity is loaded so your values will be initialized when Unity is loaded.

Further more youc an remove the SerializedField attributes from the static variables since it has no effect on static variables.

The saving of the values can be done right in the property setter. That way the saving and loading is completely independent from the editorwindow. The values get automatically loaded when Unity starts or reinitializes the managed environment and is automatically saves the values when you assign a new value to one of the properties.

[InitializeOnLoad]
public class CoreGlobal
{
    private static bool onMobile;
    private static bool developmentBuild;
    static CoreGlobal()
    {
        Load();
    }
    public static void Load()
    {
        onMobile = EditorPrefs.GetBool("CBC_OnMobile", false);
        developmentBuild = EditorPrefs.GetBool("CBC_DevelopmentBuild", false);
    }
    public static void Save()
    {
        EditorPrefs.SetBool("CBC_OnMobile", onMobile);
        EditorPrefs.SetBool("CBC_DevelopmentBuild", developmentBuild);
    } 
    public static bool OnMobile
    {
        get { return onMobile; }
        set 
        {
            if (onMobile != value) {
                onMobile = value;
                Save();
            }
        }
    }
    public static bool DevelopmentBuild
    {
        get { return developmentBuild; }
        set
        {
            if (developmentBuild != value) {
                developmentBuild = value;
                Save();
            }
        }
    }
}