Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 ChaoticBoredom · Jul 23, 2014 at 11:02 PM · listcustom inspector

Trying to Create Default List in Custom Inspector

Creating a custom inspector for a tool. In the tool, there is a list of custom objects, and I'm creating a custom inspector for this list. Inside of this custom object, there is another list, and for this nested list I'd like to draw the default list inspector.

To this end, I've tried using the FindProperty, and EditorGUILayout.PropertyField method but keep getting the error that "Type 'CustomObj' does not contain a definition for 'FindProperty'"

The CustomObj is Serializable, so I'm not sure where I've gone wrong. Sample code below.

Main Class

 [Serializable]
 public class CustomObj {
   public bool data1 = false;
   public string someString;
   public Transform[] nestedList = new Transform[1];
 }
 
 public class SomeTool:MonoBehaviour {
   public CustomObj[] customList = null;
   ...
 }


Editor Class

 [CustomEditor(typeof(SomeTool))]
 class SomeToolEditor:Editor {
   private bool showAll = true;
 
   public override void OnInspectorGUI() {
     SomeTool script = target as SomeTool;
   
     showAll = EditorGUILayout.Foldout( showAll, "Tools" );
     if (showAll) {
       foreach (CustomObj co in script.customList) {
         EditorGUILayout.PropertyField(co.FindProperty("nestedList"));
       }
     }
   }
 }
 

I've tried setting CustomObj to inhereit MonoBehaviour w/ no success as well.

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
3

Answer by vexe · Jul 23, 2014 at 11:07 PM

FindProperty is located inside SerializedObject and not MonoBehaviour or System.Object - If you want a relative property, use FindPropertyRelative.

It becomes a bit tricky when you nest and deal with System.Objects via SerializedProperties.

First of all, if you want to draw a list/array you need to pass true to PropertyField's 'includeChildren' flag, like so:

 EditorGUILayout.PropertyField(spMyList, true);

If you don't do this, all you'll get is the list's name (sometimes with a foldout) - you won't get the elements drawn.

Now for FindProperty to work, like I mentioned you have to perform it on a SerializedObject - Now when you create an Editor you'll get a serializedObject property for your target object so you don't need to create a new SerializedObject.

But in your case, if you want FindProperty to pick up your nested lists, you need to create a SerializedObject for each CustomObj - With your current setup, it won't work because the constructor for SerializedObject expects a UnityEngine.Object and not a System.Object

One way around this is to make your CustomObj either a MonoBehaviour or a ScriptableObject. In both cases you have to save the script live somewhere, in case of a MB you could attach it to an empty GO or a prefab that you keep just for this kind of stuff (See Bunny's hack in the comments here). In case of a ScriptableObject, you need to create an asset for it.

To create a SerializedObject for a UnityEngine.Object, you do:

 var so = new SerializedObject(targetObj);

If you don't like all this, you can forget about SerializedProperties and draw the lists manually. You can always implement Undo on your own. Either via Unity's Undo, or DEVBUS.


EDIT: It seems that PropertyField is more than enough, here's a working solution (at least in my Unity 4.5.2)

TestBehaviour.cs

 using UnityEngine;
 
 public class TestBehaviour : MonoBehaviour
 {
     public TestObject[] array;
 }

TestObject.cs

 using System;
 
 [Serializable]
 public class TestObject
 {
     public Transform[] nestedArray;
 }

TestEditor.cs

 using UnityEditor;

 [CustomEditor(typeof(TestBehaviour))]
 public class TestEditor : Editor
 {
     public override void OnInspectorGUI()
     {
         EditorGUILayout.PropertyField(serializedObject.FindProperty("array"), true);
     }
 }


Comment
Add comment · Show 7 · 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 ChaoticBoredom · Jul 23, 2014 at 11:16 PM 0
Share

Note that I'm looking for a property on the CustomObj, not relative to the SomeTool object. The issue is that neither of the two functions seem to exist on the CustomObj object.

avatar image vexe · Jul 23, 2014 at 11:19 PM 0
Share

Yes please see updated answer. FindProperty is not located in $$anonymous$$onoBehaviour or System.Object. It's a method inside SerializedObject.

avatar image vexe · Jul 23, 2014 at 11:26 PM 0
Share

Alright let me try and work this out and hopefully get back to you with a working result ins$$anonymous$$d of my mombojumbo.

avatar image vexe · Jul 23, 2014 at 11:35 PM 0
Share

Alright see update.

avatar image vexe · Jul 25, 2014 at 11:02 PM 0
Share

@ChaoticBoredom did it work honey?

Show more comments

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

23 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

Related Questions

A node in a childnode? 1 Answer

How do I persist data from a custom inspector for a list? (C#) 0 Answers

List of custom classes in MonoBehaviour's Data gets lost after Re-Opening Unity!!! 0 Answers

Adding dropdown to a list add buttom 2 Answers

Custom Inspector - break link between duplicate fields? 1 Answer


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