Search Unity

Editor Window with Custom Class

Discussion in 'Immediate Mode GUI (IMGUI)' started by ashley, Oct 18, 2016.

  1. ashley

    ashley

    Joined:
    Nov 5, 2011
    Posts:
    84
    Hi all,

    I'm trying to get my head around custom editors to hopefully in the future make my life a bit easier. I've managed to get to grips with certain things but I'm having trouble with using a custom class.

    Within my Character class I have a CharImages class that contains a string, int and sprite. I want to have an array of those and add them to the character editor window I already have working (so I can set up/update characters through there).

    I've got arrays of 'standard' types (e.g. sprites) working but can't seem to get it working for my custom class. I assume it's because I'm telling the editor window to expect one item when it should expect three (string, int and sprite) but unsure how to then tie all those together as one and add it as my custom class.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEditor;
    5.  
    6. public class Character : MonoBehaviour {
    7.  
    8. //other fields redacted
    9.  
    10.     [System.Serializable]
    11.     public class CharImages
    12.     {
    13.         public string imageName;
    14.         public int imageAge;
    15.         public Sprite image;
    16.     }
    17.  
    18. //more redacted
    19. }
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections.Generic;
    4.  
    5. [System.Serializable]
    6. public class CharacterWindow : EditorWindow
    7. {
    8. //other things redacted
    9. Character.CharImages[] charExtraImages;
    10.  
    11.     void OnGUI()
    12.     {
    13.         //layout stuff redacted
    14.         ScriptableObject target = this;
    15.         SerializedObject so = new SerializedObject (target);
    16.         SerializedProperty charExtraImages = so.FindProperty ("charExtraImages");
    17.         EditorGUILayout.PropertyField (charExtraImages, true);
    18.         so.ApplyModifiedProperties ();
    19.         if (GUILayout.Button ("Create Character")) {
    20.             createCharacter.CreateChar ( (Character.CharImages[])charExtraImages);
    21.         //layout stuff redacted          
    22.     }
    23. }
    The scripts have been edited for simplicity (there's a lot more character fields that are being handled) but let me know if you need anything else.

    As I mentioned above, I'm assuming the SerializedProperty thing isn't correct, but I don't know what to replace it with and I can't see anything within the EditorGUILayout reference that jumps out at me. I used it for an array which is why I thought of using it here, but this is an array of a custom class so I'm not sure if I'm missing something or if I need to change something somewhere else.

    If I add the class to a GameObject it seems fine in the inspector, so I'm just looking to replicate that through an editor window.

    Thanks!
     
    Last edited: Oct 20, 2016
  2. Deleted User

    Deleted User

    Guest

    Try making charExtraImages public or marking it with the [SerializeField] attribute. This might not fix your original problem, but non-public fields definitely will not be serialized by default.