Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Kwillson805 · Sep 29, 2017 at 06:36 PM · arrayinspectorserializationcustom-inspectorcustom inspector

Cutom Array Inspector in a Custom Inspector

I want to make a custom inspector for an array of serialized classes, that can be displayed in a custom inspector of a controller class. I have made a limited capability of this, which can't support serializedObject, or at least I don't know how too. So I can't get any UnityEvents, Classes, etc in the Inspector.

Here is my Serialized Class that will be in the Array

 [System.Serializable]
 public class CustomArrayClassExample
 {
     public class SubClass
     {
         GameObject obj1;
         string str1;
     }
 
     public bool hasObj;
     public GameObject obj;
 
     public bool hasUnityEvent;
     public UnityEvent unityEvent;
 
     public bool hasSubClass;
     public SubClass subClass;
 
 #if UNITY_EDITOR
     public void CustomInspector()
     {
         
         bool show = false;
         //EditorGUI, EditorGIULayout, GUILayout...
         EditorGUI.indentLevel++;
 
         hasUnityEvent = EditorGUILayout.Toggle(new GUIContent("Has UnityEvent", "Does this have an associated unity Event"), hasUnityEvent);
         if (hasSubClass)
         {
             // TODO: ADD Unity Event Inspector support
         }
 
         hasObj = EditorGUILayout.Toggle(new GUIContent("Has Object", "Does this have an associated object with it"), hasObj);
         if (hasObj)
         {
             obj = (GameObject)EditorGUILayout.ObjectField( new GUIContent("Game Object"), obj, typeof(GameObject), true);
         }
 
         hasSubClass = EditorGUILayout.Toggle(new GUIContent("Has SubClass", "Does this have an associated subclass"), hasSubClass);
         if (hasSubClass)
         {
             //ERROR
            // subClass = (SubClass) EditorGUILayout.ObjectField(subClass, typeof(SubClass), true);
         }
 
         EditorGUI.indentLevel--;
     }
 #endif
 
 }
 

And here is my Controller Class:

 public class CustomArrayControllerExample : MonoBehaviour {
     public int maxArrSize = 25;
 
     public int numSerialClasses = 1;
     public List<CustomArrayClassExample> serialClass = new List<CustomArrayClassExample>();
     
 
 
 #if UNITY_EDITOR
     [CustomEditor(typeof(CustomArrayControllerExample))]
     public class MyScriptEditor : Editor
     {
         bool[] showPositions;
         override public void OnInspectorGUI()
         {
             var myScript = target as CustomArrayControllerExample;
 
             EditorGUILayout.HelpBox(string.Format("The current maximum supported size for an array is {0}.  This can be changed in the code by changing\n\tpublic int maxArrSize = 25;", myScript.maxArrSize), MessageType.Warning);
 
             myScript.numSerialClasses = EditorGUILayout.IntField(new GUIContent("Number in Array", "The number of elements present in the array"), myScript.numSerialClasses);
             Repaint();
 
             EditorGUI.indentLevel++;
             for (int i = 0; i < myScript.numSerialClasses; i++)
             {
                 showPositions[i] = EditorGUILayout.Foldout(showPositions[i], "Serial Class " + i);
                 if (showPositions[i])
                     if (Selection.activeTransform)
                     {
                         serializedObject.Update();
                         myScript.serialClass.Add(new CustomArrayClassExample());
                         myScript.serialClass[i].CustomInspector();
 
                         serializedObject.ApplyModifiedProperties();
                     }
 
                 if (!Selection.activeTransform)
                 {
                 }
 
             }
             EditorGUI.indentLevel--;
 
             serializedObject.Update();
            // EditorGUILayout.PropertyField(serializedObject.FindProperty("serialClass"), true);
             serializedObject.ApplyModifiedProperties();
             
         }
 
         private void OnEnable()
         {
             var myScript = target as CustomArrayControllerExample;
             showPositions = new bool[myScript.maxArrSize];
 
         }
     }
 #endif
 
     // Use this for initialization
     void Start () {
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 }
 

So here is what the Controller looks like in the inspector, as you can see I can toggle different EditorGUILayout fields, but I need to get UnityEvents and Classes: alt text

customarraycontroller.png (23.2 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Sep 29, 2017 at 07:12 PM

Your code is a mess. You mix runtime code and editor code inside runtime classes. Also you mix "serializedObject" and "target".Stick with one way.

What's actually the reasson why you want to create a custom inspector? A custom inspector can't change what is actually serialized. It can only change how the serialized data is displayed.

Note that your nested "SubClass" is not marked "Serializable" so the "subClass" field is not serialized.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kwillson805 · Sep 29, 2017 at 07:27 PM 0
Share

The editor code in the serialized class enables its custom inspector to be drawn in the Controllers custom editor inspector. So if I toggle a bool in the serial class it displays the GameObject field (in the Controller inspector).

As for the serialized object and target, I am still learning proper techniques, so if you could elaborate on that I would greatly appreciate it.

And I'll fix that note.

avatar image Kwillson805 Kwillson805 · Sep 30, 2017 at 12:06 AM 0
Share

O I get what you mean by editor code in a run time class. So the proper practice is put it in "Editor/ControllerEditor"

As for how to get the nested custom editor I am still at a loss for

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

82 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Error while using SerializeField and HideInInspector tags 0 Answers

How should I serialize data that is also editable in the Inspector? 2 Answers

How can I recreate the Array Inspector element for a custom Inspector GUI? 7 Answers

Custom Inspector & Arrays 0 Answers

Moving the input fields in a custom inspector 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges