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
2
Question by Eye_of_Triad · Jul 10, 2017 at 11:27 PM · guiarrayunityeditorinspectorcustom-inspector

How to show an array of custom objects in Inspector?

I can show one object, but not an array of them.

BasicLight in Inspector: (I want to show an array of these) BasicLight

LightTube contains an array of BasicLights and looks like this: (This does not work, I want to show an array of four fields each) LightTube

How do I get an array of BasicLights to show the fields of each element (that we see in the first image)? I have also tried making a CustomPropertyDrawer (not shown here), but I haven't been able to get it to work with the custom inspector. Any suggestions?

Code is below.

BasicLight (a basic light, of which arrays will be made of in the inspector)

 [Serializable]
 public class BasicLight : MonoBehaviour
 {
     public bool isLightOn = false;
 
     public string name;
     public GameObject light;
     public float range = 30.0f;
     public float intensity = 1.5f;
 }

EditBasicLight (creates the GUI controls in Inspector)

 [CustomEditor(typeof(BasicLight))]
 public class EditBasicLight : Editor
 {
     public override void OnInspectorGUI ()
     {
         serializedObject.Update ();
         BasicLight basicLight = target as BasicLight;
 
         basicLight.isLightOn = EditorGUILayout.Toggle ("on", basicLight.isLightOn);
 
         EditorGUI.indentLevel++;
         basicLight.name = EditorGUILayout.TextField ("name", basicLight.name);
         basicLight.light = EditorGUILayout.ObjectField ("light", basicLight.light);
         basicLight.range = EditorGUILayout.FloatField ("range", basicLight.range);
         basicLight.intensity = EditorGUILayout.FloatField ("intnsity", basicLight.intensity);
         EditorGUI.indentLevel--;
     }
 }


BaseLight (LightTube inherits from this) (attached to the GameObject) (Other classes will extend this, hence the name ‘Base’Light.)

 public class BaseLight : MonoBehaviour
 {
     public bool on = false;
     public BasicLight [] lights;
 }


EditBaseLight (BaseLight Editor. Creates the GUI controls in Inspector) (But how do you get the array to show correctly)

 [CustomEditor(typeof(BaseLight))]
 public class EditBaseLight : Editor
 {
     public override void OnInspectorGUI ()
     {
         serializedObject.Update ();
         BaseLight baseLight = target as BaseLight;
 
         EditorGUIUtility.LookLikeInspector ();
 
         SerializedObject serializedObj = new SerializedObject (target);
         SerializedProperty lights = serializedObject.FindProperty ("lights");
 
         serializedObj.Update ();
         EditorGUILayout.HelpBox ("Default Inspector", MessageType.None);
         DrawDefaultInspector();
         serializedObj.ApplyModifiedProperties ();
     }
 }


LightTube

 public class LightTube : BaseLight
 {
 
 }


EditLightTube

 [CustomEditor(typeof(LightTube))]
 public class EditLightTube : EditBaseLight
 {
     public override void OnInspectorGUI()
     {
         LightTube lightTube = target as LightTube;
 
         base.OnInspectorGUI ();
     }
 }

Comment
Add comment · Show 1
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 Eye_of_Triad · Jul 10, 2017 at 11:53 PM 0
Share

I also tried putting [SerializeField] before every variable in the BasicLight but there was no change.

2 Replies

· Add your reply
  • Sort: 
avatar image
11

Answer by dtumolo · Jul 29, 2017 at 05:51 AM

When I add "[System.Serializable]" in front of my class definition, it works.

Comment
Add comment · 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
3

Answer by Eye_of_Triad · Jul 14, 2017 at 02:18 PM

I discovered that if I remove the extension from MonoBehaviour, it shows the fields for each array element in Inspector.

However, I want to do special checks for input (eg when they check a box, turn on a light). So when I create the CustomEditor class, it requires a MonoBehaviour object.

So I'm stuck choosing one or the other. Either I can have the array show it correctly, or I can have a custom editor.

Is there any way to have both?

Comment
Add comment · 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

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

115 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 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

Cutom Array Inspector in a Custom Inspector 1 Answer

Align text into PropertyField 1 Answer

GUI, Inspector has no control for Arrays? 3 Answers

Prefix label greyed out when following component disabled 1 Answer

Detecting GUI.changed using DrawDefaultInspector 3 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