Search Unity

EditorWindow loosing reference on play how to fix

Discussion in 'Immediate Mode GUI (IMGUI)' started by TukkerTerror, Dec 5, 2017.

  1. TukkerTerror

    TukkerTerror

    Joined:
    May 8, 2017
    Posts:
    4
    I already posted a thread of what the problem was here https://forum.unity.com/threads/editorwindow-loses-reference-on-play.506861/#post-3309611

    I now simplified the classes to show only the key elements of what I try to do.

    The main problem lies here the _sprite reference get lost when I play the scene.
    Code (CSharp):
    1. public class Child : Base {
    2.     [SerializeField]
    3.     private Image _image;
    4.     [SerializeField]
    5.     private Sprite _sprite;
    6.  
    7.     public override void DrawEditSettings() {
    8.         GUILayout.BeginArea(new Rect(0, 20, 200, 500));
    9.         _sprite = (Sprite)EditorGUILayout.ObjectField(_sprite, typeof(Sprite), true);
    10.         if(_sprite != null) {
    11.             _image.sprite = _sprite;
    12.         }
    13.         GUILayout.EndArea();
    14.     }
    15. }
    The editor window also again nothing special maybe only the function DrawEditorSettings.

    Code (CSharp):
    1. public class EditorWindowBase : EditorWindow {
    2.  
    3.     WindowCreater _windowCreator;
    4.  
    5.     void OnGUI() {
    6.         if(GUILayout.Button("Create a Base and add to WindowCreater")) {
    7.             GameObject child = PrefabUtility.InstantiatePrefab(Resources.Load(@"TEMP/Childprefab")) as GameObject;
    8.             child.transform.parent = _windowCreator.transform;
    9.             _windowCreator._bases.Add(child.GetComponent<Base>());
    10.         }
    11.         //---------- here i call the function DrawEditorSettings only for the first item in the list for the question
    12.         if(_windowCreator._bases.Count>0) {
    13.             _windowCreator._bases[0].DrawEditSettings();
    14.         }
    15.     }
    16.  
    17.     public void SetValues(WindowCreater pWindowCreator) {
    18.         _windowCreator = pWindowCreator;
    19.     }
    20. }
    Here the code that creates the Editor Window nothings special but still.

    Code (CSharp):
    1. [CustomEditor(typeof(WindowCreater))]
    2. public class WindowCreaterEditor : Editor {
    3.     public override void OnInspectorGUI() {
    4.         WindowCreater WindowCreater = (WindowCreater)target;
    5.         if(GUILayout.Button("Create Winow")) {
    6.             EditorWindowBase window = (EditorWindowBase)EditorWindow.GetWindow(typeof(EditorWindowBase));
    7.             window.SetValues(WindowCreater);
    8.             Selection.activeGameObject = WindowCreater.gameObject;
    9.             SceneView.lastActiveSceneView.FrameSelected();
    10.         }
    11.     }
    12. }
    13.     public class WindowCreater : MonoBehaviour {
    14.     public List<Base> _bases = new List<Base>();//public just for the question
    15. }
    And here the Base class just for measure
    Code (CSharp):
    1. public abstract class Base : MonoBehaviour {
    2.     public abstract void DrawEditSettings();
    3. }
    Thanks in advance for helping me.
     
  2. TukkerTerror

    TukkerTerror

    Joined:
    May 8, 2017
    Posts:
    4
    it finally worked found the solution
    Code (CSharp):
    1.         if(_sprite != null) {
    2.             Undo.RecordObject(this, "Some Random text");
    3.             _image.sprite = _sprite;
    4.             EditorUtility.SetDirty(this);
    5.         }
     
    Plaximo, FlightOfOne and videon like this.