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
1
Question by Long2904 · Oct 25, 2020 at 11:41 AM · onguipropertydrawerserializedpropertyserializable

How to find property of a serializeObject that has the same name with a field of another property?

I had a custom attribute and property drawer class to show or hide field depends on some condition. In the OnGui method of the drawer class, I would find a property and then do something with it.

     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {
         ShowWhenAttribute attribute = (ShowWhenAttribute)this.attribute;
         SerializedProperty conditionField = property.serializedObject.FindProperty(attribute.conditionFieldName);
         // Do something here
     }

I also had a serializable class and an instance of that class in a monobehaviour class.

 public enum StatusType { Burn, Bleed, Poison, Freeze, Slow, Blind, Injured }
 
 [System.Serializable]
 public class State
 {
     public StatusType type;
     public float duration;
     [ShowWhen("duration", /* For example: ">5" */)] public float percent;
     public int damage;
     public float timeBtwHits;
 }
 
 public class Item : MonoBehaviour
 {
   public float duration;
   public State state;
 }

The problem is the conditionField is not state.duration but duration. How to fix it?

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 PlayCreatively · Oct 25, 2020 at 12:24 PM 0
Share

SerializedProperty has a FindPropertyRelative function. Use FindProperty to find 'state', then use FindPropertyRelative on that to find its 'duration 'property.

You should also use SerializedProperty.Next to loop through all of the properties and print their names just to understand what properties you're accessing.

 while (property.Next(true))
       Debug.Log(property.name);

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Bunny83 · Oct 25, 2020 at 12:51 PM

Because you used property.serializedObject.FindProperty. So you accessed the SerializedObject (the root object that is actually serialized, in your case the Item class) and used FindProperty to find a property with that name. If you want to use a property on the "same level" you would need to find the property relative to the parent. Unfortunately the SerializedProperty class doesn't have a GetParent method. However you can create by disecting the property path. Note that this of course has an edge case when you actually have a property inside the root object itself in which case there is no parent serialized property. So I quickly made this FindSiblingProperty extension method:

 public static class SerializedPropertyExt
 {
     public static SerializedProperty GetParent(this SerializedProperty aProperty)
     {
         var path = aProperty.propertyPath;
         int i = path.LastIndexOf('.');
         if (i < 0)
             return null;
         return aProperty.serializedObject.FindProperty(path.Substring(0, i));
     }
     public static SerializedProperty FindSiblingProperty(this SerializedProperty aProperty, string aPath)
     {
         var parent = aProperty.GetParent();
         if (parent == null)
             return aProperty.serializedObject.FindProperty(aPath);
         return parent.FindPropertyRelative(aPath);
     }
 }

With those extension methods you can simply do

 SerializedProperty conditionField = property.FindSiblingProperty("condition");

So we first look for a parent property and if there is none we just grab the property on the serialized object. Otherwise we look for the property relative to the parent.

Comment
Add comment · Show 3 · 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 ENOSI_Studio · Dec 12, 2021 at 07:41 PM 0
Share

Work well but throw me a compiling error when i try to build my game.

Assets\Scripts\Helper Scripts\SerializedPropertyExt.cs(5,53): error CS0246: The type or namespace name 'SerializedProperty' could not be found (are you missing a using directive or an assembly reference?)

avatar image Bunny83 ENOSI_Studio · Dec 12, 2021 at 09:48 PM 0
Share

The SerializedObject and SerializedProperty classes are editor only classes. So it should be obvious that this extension class belongs into an editor folder as well, just like any editor code.

avatar image ENOSI_Studio Bunny83 · Dec 12, 2021 at 10:25 PM 1
Share

My problem was with a "PropertyAttribute" I didn't know it shouldn't be in the Editor folder but now it works, thanks.

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

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

SerializedObject.FindProperty returning null 2 Answers

Should EditorGUILayout.PropertyField work with serializable classes? 1 Answer

objectReferenceValue in SerializedProperty 5 Answers

Error when trying to Serialize a field that is in a class 0 Answers

Get SerializedProperty from outside class for use in popup (PropertyDrawer) 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