Search Unity

  1. All Pro and Enterprise subscribers: find helpful & inspiring creative, tech, and business know-how in the new Unity Success Hub. Sign in to stay up to date.
    Dismiss Notice
  2. Dismiss Notice

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,840
    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,840
    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
     
    zosichd likes this.
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    11,394
  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);
     
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    11,394
    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:
    11,394
    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:
    11,394
    I guess not! :) I've never tried it myself.
     
  11. Graph

    Graph

    Joined:
    Jun 8, 2014
    Posts:
    152
    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:
    11,394
    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:
    152
    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:
    132
    Thanks fast and easy way
     
  15. pointcache

    pointcache

    Joined:
    Sep 22, 2012
    Posts:
    546
    this thread should be named "for custom inspector" and not for editor window.
     
  16. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,840
    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:
    546
    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,840
    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
     
    AnomalusUndrdog likes this.
  19. pointcache

    pointcache

    Joined:
    Sep 22, 2012
    Posts:
    546
    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:
    287
    Yeah it's the same for a window and for an editor.
     
  21. Dr_SuiuS

    Dr_SuiuS

    Joined:
    Jun 20, 2019
    Posts:
    6
    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
    f4bo and Mabia like this.
unityunity