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
4
Question by $$anonymous$$ · Mar 21, 2015 at 02:34 AM · typeserializedproperty

Get field type of SerializedProperty

I'm in a situation where I require to know the type that a SerializedProperty is storing. I know the serialized property is of SerializedPropertyType.ObjectReference, but that's all I can work out.

What I need is the actual type (GameObject, Transform, TextAsset, Camera, etc.). Is there any way of doing this?

Thanks

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by $$anonymous$$ · Mar 21, 2015 at 05:49 AM

I resorted to using reflection, although I had hoped I wouldn't need to. I'd still like to know any solutions that are more straightforward.

 string[] parts = property.propertyPath.Split('.');
 
 Type currentType = target.GetType();
 
 for (int i = 0; i < parts.Length; i++)
 {
     Debug.Log(currentType + " " + parts[i]);    
     currentType = currentType.GetField(parts[i], BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.Instance).FieldType;
 }
 
 Type targetType = currentType;


Essentially, using the path of the property, I find the field with reflection to get the type.

Comment
Add comment · Show 1 · 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 scanzy · Apr 19, 2017 at 05:05 PM 0
Share

This won't work with properties inside array, since path is like someName.Array.data[4].someName... Check my answer for a modified solution

avatar image
2

Answer by scanzy · Apr 19, 2017 at 05:03 PM

This works also for properties inside arrays

  //gets parent type info
  string[] slices = prop.propertyPath.Split('.');
  System.Type type = prop.serializedObject.targetObject.GetType();
 
  for(int i = 0; i < slices.Length; i++)                
      if (slices[i] == "Array")
      {
          i++; //skips "data[x]"
          type = type.GetElementType(); //gets info on array elements
      }                   
                 
      //gets info on field and its type
      else type = type.GetField(slices[i], BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.Instance).FieldType;  
 
 //type is now the type of the property

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 mhofer · Oct 31, 2017 at 04:29 AM 0
Share

type.GetElementType() only returns something when the element is not null. Do you happen to know how I can get the type that fits in there?

avatar image mhofer · Oct 31, 2017 at 04:40 AM 0
Share

Figured it out! it's type.GetEnumerableType();

avatar image rockwalrus · Feb 06, 2019 at 01:02 AM 1
Share

This is pretty similar to the code that Unity uses internally to get the type.

avatar image
1

Answer by llamagod · Sep 25, 2016 at 01:45 PM

@Narmdo Sorry for resurrecting an old thread. If you want the object reference type of the property you can use regex and Type.GetType, or if you're in a PropertyDrawer you can just use the PropertyDrawer.field field. Here are two methods from my EditorExtensions library.

 public static string GetPropertyType(SerializedProperty property)
     {
         var type = property.type;
         var match = Regex.Match(type, @"PPtr<\$(.*?)>");
         if (match.Success)
             type = match.Groups[1].Value;
         return type;
     }
 
     public static Type GetPropertyObjectType(SerializedProperty property)
     {
         return typeof(UnityEngine.Object).Assembly.GetType("UnityEngine." + GetPropertyType(property));
     }



Comment
Add comment · Show 1 · 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 scanzy · Apr 19, 2017 at 03:31 PM 0
Share

This won't work for user defined types, unless they belong to UnityEngine namespace

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

25 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

Related Questions

What does a [SomeName] Above A Variable Mean? 2 Answers

ObjectField functions ignoring parameter 1 Answer

Servers connection differences/uses 1 Answer

Type in bug in editor 1 Answer

Determine if game object is tyoepf One Prefab 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