Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to access a list created on EditorWindow script?

Discussion in 'Immediate Mode GUI (IMGUI)' started by soltex1, Jun 4, 2016.

  1. soltex1

    soltex1

    Joined:
    Apr 2, 2015
    Posts:
    14
    Imagine that I have a simple list on my EditorWindow script:
    Code (CSharp):
    1. [SerializeField]
    2. public List<int> intValues;
    onGui I have a button that adds elements to the list:
    Code (CSharp):
    1. if (GUILayout.Button("Add Value", GUILayout.Width(115), GUILayout.Height(20)))
    2. {
    3.     intValues.add(1);
    4. }

    My question is: How do I get access to the updated list from another script, Monobehaviour?


    This is what I have tried on Monobehaviour script:
    Code (CSharp):
    1. serializedObject.FindProperty("intValues");
    However:
    Code (CSharp):
    1. serializedPropertyMyInt.arraySize
    is 0. The only way that works is declaring an array like this:
    Code (CSharp):
    1.  Array1 =  new[] { 1, 2, 3, 4, 5 };
    on EditorWindow script and call it on Monobehaviour using the same code:
    Code (CSharp):
    1. serializedObject.FindProperty("Array1");
    But If I update, simple doesn't work. Using "toArray()" on onGUI didn't work either. I need to work with those values inside the Monobehaviour script Update function.

    Any ideias?
     
    Last edited: Jun 4, 2016
  2. soltex1

    soltex1

    Joined:
    Apr 2, 2015
    Posts:
    14
    I found out an alternative. Instead of declare the list on EditorWindow script, I declared on Monobehaviour script the same way:
    Code (CSharp):
    1. [SerializeField] public List<int> intValues;
    This way, I can use it, updated, whatever I want, by first using the reference to the Monobehaviour script (MyBehaviour.cs):
    Code (CSharp):
    1. [SerializeField] private MyBehaviour _scriptReference;
    2. _scriptReference = FindObjectOfType(typeof (MyBehaviour)) as MyBehaviour;
    and then, point to the List:
    Code (CSharp):
    1. [SerializeField]_scriptReference.intValues
    If I add values through this, they will be updated when the Monobehaviour script is used. In my case, I used the script on a GameObject and it worked.

    I am still curious about how to do it by declaring it on EditorWindow script.
     
  3. TrickyHandz

    TrickyHandz

    Joined:
    Jul 23, 2010
    Posts:
    196
    An EditorWindow isn't meant to be used to get data to persist within the instance, rather they are used to access and manipulate data that is stored outside of the window. So the data should reside in a MonoBehaviour (as you did above), in ScriptableObject asset, Player/Editor Preferences, or some other serialized file (.txt, .json, .xml, etc).
     
    soltex1 likes this.
  4. BMayne

    BMayne

    Joined:
    Aug 4, 2014
    Posts:
    186
    Hey there,

    The reason you could not do it the first way is because the Runtime Assembly (the code not in editor folders) knows nothing about the Editor Assembly (The code in Editor folders). The Editor Assembly however knows everything about the Runtime Assembly. Their relationship is one directional which it has to be or you would have circular dependency. Circular dependency is a problem for the compiler as it can't figure out what to compile first since they both require each other to build.

    * You can still do this with reflection but it's not suggested.