Search Unity

Display Array in custom editor window

Discussion in 'Immediate Mode GUI (IMGUI)' started by QFSW, Jan 20, 2016.

  1. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,906
    Sorry for the n00b question, but I just don't know how to do it
    How does one display an array in a custom editor window, not a custom inspector
    Thanks, that's all
     
    RobertAalpoel likes this.
  2. Crizz92

    Crizz92

    Joined:
    Sep 17, 2015
    Posts:
    44
    public class MyClass : MonoBehaviour
    {
    // simply define the array as public
    public int[] myTab;

    // Or if u want the array to be private
    [Serialized]
    private int[] myTab;
    }

    attach this script on a Object and u'll see the tab appear in the inspector.

    it work with list too
     
  3. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,906
    Idk if I've just misunderstood, but I don't want the array exposed in the editor of an object, I have my own custom editor window using the legacy GUI system, which is where I want it exposed
     
    ChaosEaterY and zosichd like this.
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,706
  5. msklywenn

    msklywenn

    Joined:
    Nov 28, 2014
    Posts:
    67
    I've been trying multiple times to use EditorGUI.PropertyField with the includeChildren parameter but it always only displays "Element X". I tried setting isExpanded to true in the SerializedProperty of the array entry but that didn't help...

    Any idea of what I could be missing ?

    SerializedProperty prop = values.GetArrayElementAtIndex(index);
    prop.isExpanded = true;
    EditorGUILayout.PropertyField(prop, true);
    if (EditorGUI.EndChangeCheck())
    EditorUtility.SetDirty(target);
     
    ChaosEaterY likes this.
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,706
    Are you sure your property has serializable children?
     
  7. msklywenn

    msklywenn

    Joined:
    Nov 28, 2014
    Posts:
    67
    it's a List of

    [Serializable]
    public class CameraShakePreset
    {
    [Serializable]
    public struct Shake
    {
    public float Frequency;
    public float Amplitude;
    }

    public Shake[] RotationalX;
    public Shake[] RotationalY;
    public Shake[] RotationalZ;
    public float Duration = 0.5f;
    }

    It should be, right ?
     
  8. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,706
    Hmm, this works for me:

    CameraShakeIsPresent.cs:
    Code (csharp):
    1. using System;
    2.  
    3. [Serializable]
    4. public class CameraShakePreset
    5. {
    6.     [Serializable]
    7.     public struct Shake
    8.     {
    9.         public float Frequency;
    10.         public float Amplitude;
    11.     }
    12.  
    13.     public Shake[] RotationalX;
    14.     public Shake[] RotationalY;
    15.     public Shake[] RotationalZ;
    16.     public float Duration = 0.5f;
    17. }
    CameraShakeIsPresentList.cs:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. public class CameraShakePresentList : MonoBehaviour {
    5.     public List<CameraShakePreset> list = new List<CameraShakePreset>();
    6. }
    CameraShakeIsPresentListEditor.cs:
    Code (csharp):
    1. using UnityEditor;
    2.  
    3. [CustomEditor(typeof(CameraShakePresentList))]
    4. public class CameraShakePresentListEditor : Editor
    5. {
    6.  
    7.     public override void OnInspectorGUI()
    8.     {
    9.         EditorGUILayout.LabelField("Custom editor:");
    10.         var serializedObject = new SerializedObject(target);
    11.         var property = serializedObject.FindProperty("list");
    12.         serializedObject.Update();
    13.         EditorGUILayout.PropertyField(property, true);
    14.         serializedObject.ApplyModifiedProperties();
    15.     }
    16. }
     
  9. msklywenn

    msklywenn

    Joined:
    Nov 28, 2014
    Posts:
    67
    I'm trying to display each element separately, not the whole list.

    But I just understood the problem! Since I'm trying to display the list in a different manner but still want the rest of the inspector display as usual, I had hidden the list with [HideInInspector], used DrawDefaultInspector() to draw everything else and then do my custom display where entries would be displayed as usual with PropertyField. But it seems that since the list is hidden, all children are too. I'm not sure if it's supposed to be that way... If I remove the HideInInspector, PropertyField works fine.

    Shouldn't PropertyField ignore the hidden status of the parent serialized property ?
     
  10. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,706
    I guess not! :) I've never tried it myself.
     
  11. Graph

    Graph

    Joined:
    Jun 8, 2014
    Posts:
    154
    I've got a similar problem.. I'd like to "take" a single item out of the array, present it in an ObjectField and insert that back into the serialized object.. any ideas?
     
  12. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,706
    I'm not sure what you mean. Like this?
    Code (csharp):
    1. [Serializable]
    2. public class MyClass {
    3.     public SomeType[] myArray;
    4. }
    5. ...
    6. var myArrayProperty = serializedObject.FindProperty("myArray");
    7. var myElement = myArrayProperty.GetArrayElementAtIndex(42);
    8. myElement.objectReferenceValue = EditorGUILayout.ObjectField(myElement.objectReferenceValue, typeof(SomeType), true);
     
    Graph likes this.
  13. Graph

    Graph

    Joined:
    Jun 8, 2014
    Posts:
    154
    yeah turns out exactly like that and i just had a brainFart :p
    switched up the type on what's line 7 of your code
     
  14. xxhaissamxx

    xxhaissamxx

    Joined:
    Jan 12, 2015
    Posts:
    134
    Thanks fast and easy way
     
  15. pointcache

    pointcache

    Joined:
    Sep 22, 2012
    Posts:
    579
    this thread should be named "for custom inspector" and not for editor window.
     
    Prosto_Lyubo, OGTechnology and sjk000 like this.
  16. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,906
    I literally stated in the OP that I wanted it for a custom editor window, NOT a custom inspector. So I have no idea why you're claiming that. Especially since its such an old thread.
     
    zosichd and i123iu like this.
  17. pointcache

    pointcache

    Joined:
    Sep 22, 2012
    Posts:
    579
    Sorry then i got confused by all the responses that use SerializedProperty, assumed the thread was about inspector. You dont have the serialized properties in editor window? Or i am missing something.
     
  18. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,906
    Yeah, I'm pretty sure you're correct with that, so you're right in saying these responses are for inspectors but it was never what i asked for
     
    Prosto_Lyubo and AnomalusUndrdog like this.
  19. pointcache

    pointcache

    Joined:
    Sep 22, 2012
    Posts:
    579
    Yeah sry again, i solved same problem with just doing it with gui methods
    upload_2018-1-18_22-17-39.png
     
  20. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    289
    Yeah it's the same for a window and for an editor.
     
  21. Dr_SuiuS

    Dr_SuiuS

    Joined:
    Jun 20, 2019
    Posts:
    7
    I know it's a relatively old thread and everybody was using older Unity versions, but if anyone wants to do it on the last Unity version I would suggest to not find properties in
    OnInspectorGUI()
    - do it in the
    OnEnable()
    instead.
    Also, I'm not sure if there is any need for
    var serializedObject = new SerializedObject(target);
    , as new Unity automatically updates
    serializedObject
    when you call
    serializedObject.Update()
    .
    An advice for people who like things like
    SerializeField
    and
    HideInInspector
    - DON'T USE
    HideInInspector
    on arrays and lists if you want to use them with custom inspector - they just don't click together with it.

    This thread helped me a lot, thanks everybody :)
     
    Last edited: Dec 1, 2020
    andreiagmu, f4bo and Mabia like this.
  22. TH_Akash

    TH_Akash

    Joined:
    Dec 13, 2021
    Posts:
    1
    Below is the best solution I have found.

    BlockData.cs:
    Code (CSharp):
    1. public class BlockData : ScriptableObject
    2. {
    3.     public Vector3[] vertices;
    4. }
    BlockDataEditor.cs:
    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEditor;
    3. using UnityEngine;
    4.  
    5. [CustomEditor(typeof(BlockData))]
    6. public class BlockDataEditor : UnityEditor.Editor
    7. {
    8.    public override void OnInspectorGUI()
    9.    {
    10.       var blockData = (BlockData)target;
    11.  
    12.       // Triangles - Int Arrays
    13.       EditorGUILayout.PropertyField
    14.       (
    15.          serializedObject.FindProperty(nameof(blockData.triangles)),
    16.          new GUIContent("Triangles")
    17.       );
    18.    }
    19. }
     
  23. Trumped

    Trumped

    Joined:
    Jul 3, 2017
    Posts:
    5
    I looked through lots of these post and couldn't find one I liked or understood properly so I decided to try to make my owe method and this is what I came up with it works perfect for me and what I was trying to achieve.

    quest.itemArrayLength = EditorGUILayout.IntField("Item Reward Number: ", quest.itemArrayLength);
    Array.Resize(ref quest.ItemReward, quest.itemArrayLength);
    for (int i = 0; i < quest.itemArrayLength; i++)
    {
    quest.ItemReward = (GameObject)EditorGUILayout.ObjectField("Reward Item: " + i, quest.ItemReward, typeof(GameObject), false, GUILayout.Height(EditorGUIUtility.singleLineHeight));
    }
     
  24. nndwn_

    nndwn_

    Joined:
    Nov 7, 2018
    Posts:
    4
    Code (CSharp):
    1.  
    2.  
    3. public class TestEditor : MonoBehaviour
    4. {
    5.        public int[] posXButton;
    6. }
    7.  
    8. [CustomEditor(typeof(TestEditor))]
    9. public class TestEditorEditor : UnityEditor.Editor
    10. {
    11.        private SerializedProperty _posXbutton;
    12.  
    13.        public void OnEnable()
    14.       {
    15.           _posXbutton = serializedObject.FindProperty("posXButton");
    16.       }
    17.  
    18.        public override void OnInspectorGUI()
    19.        {
    20.  
    21.           serializedObject.Update();
    22.  
    23.           EditorGUILayout.PropertyField( _posXbutton,new GUIContent("button position"));
    24.  
    25.           serializedObject.ApplyModifiedProperties();
    26.  
    27.           DrawDefaultInspector();
    28.        }
    29.  
    30. }
    i hope help someone
     
    Last edited: Jan 13, 2024