Search Unity

Sharing Data Between Unity Editor Window and the Game

Discussion in 'Scripting' started by KKS21199, May 29, 2015.

  1. KKS21199

    KKS21199

    Joined:
    Nov 22, 2012
    Posts:
    139
    Hey Guys,

    I have a custom editor window built in unity called CBCore. I have a global class called, CoreGlobal.
    In my CoreGlobal,
    Code (CSharp):
    1.  using UnityEngine;
    2. using System.Collections;
    3. public class CoreGlobal{
    4.      [SerializeField]
    5.      private static bool onMobile;
    6.      [SerializeField]
    7.      private static bool developmentBuild;
    8.      public static bool OnMobile
    9.      {
    10.          get
    11.          {
    12.              return onMobile;
    13.          }
    14.          set
    15.          {
    16.              onMobile = value;
    17.          }
    18.      }
    19.      public static bool DevelopmentBuild
    20.      {
    21.          get
    22.          {
    23.              return developmentBuild;
    24.          }
    25.          set
    26.          {
    27.              developmentBuild = value;
    28.          }
    29.      }
    30. }
    and in my CBCore I call the values by,
    Code (CSharp):
    1.   void OnGUI()
    2.      {
    3.        //bools = CoreGlobal.OnMobile.
    4.          EditorGUILayout.BeginVertical();
    5.          EditorGUILayout.BeginHorizontal();
    6.          EditorGUILayout.EndHorizontal();
    7.          EditorGUILayout.EndVertical();
    8.          title = "CB Core";
    9.          GUILayout.Label("Global Core Settings", EditorStyles.boldLabel);
    10.  
    11.          CoreGlobal.OnMobile = EditorGUILayout.Toggle("Mobile Build", CoreGlobal.OnMobile);
    12.          CoreGlobal.DevelopmentBuild = EditorGUILayout.Toggle("Development Build", CoreGlobal.DevelopmentBuild);
    13. }
    14.         void OnFocus()
    15.          {
    16.              if (EditorPrefs.HasKey("CBC_OnMobile"))
    17.                  CoreGlobal.OnMobile = EditorPrefs.GetBool("CBC_OnMobile");
    18.              if (EditorPrefs.HasKey("CBC_DevelopmentBuild"))
    19.                  CoreGlobal.DevelopmentBuild = EditorPrefs.GetBool("CBC_DevelopmentBuild");
    20.          }
    21.           void OnLostFocus()
    22.          {
    23.              EditorPrefs.SetBool("CBC_OnMobile", CoreGlobal.OnMobile);
    24.              EditorPrefs.SetBool("CBC_DevelopmentBuild", CoreGlobal.DevelopmentBuild);
    25.          }
    26.           void OnDestroy()
    27.          {
    28.              EditorPrefs.SetBool("CBC_OnMobile", CoreGlobal.OnMobile);
    29.              EditorPrefs.SetBool("CBC_DevelopmentBuild", CoreGlobal.DevelopmentBuild);
    30.          }
    This works perfectly inside the editor. [One thing is that the Custom editor window should be loaded after Opening the project or the value will be wrong]

    But how do I access this values from a Game Build ? I checked the Onmobile value true and then built my game but in my game I got the value was false. But doing the same from a script like this,
    Code (CSharp):
    1. public class test:MonoBehaviour
    2. {
    3. public bool test;
    4.  
    5. void Start()
    6. {
    7.         CoreGlobal.OnMobile = test;
    8. }
    9.  
    10.  
    11. }
    I checked the test in inspector and then built my games for web and in game it was true. I unchecked it and built again, now it was false.

    If I can't use the values from a EditorWindow in my game then whats the use ? I need custom editors and I need a way to store values in them. This one was global, but some other values might be Object-wise :/