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 vexe · Feb 22, 2014 at 09:37 AM · serializedpropertycastunityengine.object

Is there any way to successfully cast a UnityEngine.Object to a System.Object?

From what I know there isn't.

But I'm asking just in case maybe one of you guys knows a hack to get this to work somehow.

A typical need for this cast, is when you have a SerializedProperty that's referencing a System.Object - I haven't found any way to get the typed reference in this case because SP.objectReferenceValue returns a UE.Object, that's why I'm asking about the cast.

i.e.

 [System.Serializable]
 public class SomeClass { public void doStuff() { } }

 public class Test : MonoBehaviour { public SomeClass someClassObject; }

 [CustomEditor(typeof(Test))]
 public class TestEditor : Editor
 {
     override void OnInspectorGUI()
    {
        SerializedProperty sp_someClassObject = serializedObject.FindProperty("someClassObject");
        (magical cast on sp_someClassObject).doStuff();
    }
 }

Any ideas?

Thanks!

Comment
Add comment · Show 4
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 vexe · Feb 22, 2014 at 12:58 PM 0
Share

That's pretty awesome thanks! - But if I wanted to get an object directly, I would just get it via 'target' - I mean why reflection?

in my case just (target as Test).doStuff(); ins$$anonymous$$d of (GetValue(target, "myClassObject") as Test).doStuff();

In both cases we got a direct reference to the object, and we're not benefiting from SerializedProperties anymore.

(unless I'm missing something and doing it your way will still make us somehow benefit from SPs (undo, redo, etc))

avatar image whydoidoit · Feb 22, 2014 at 01:33 PM 0
Share

$$anonymous$$y way let's you get it when you only have a serialized property - that happens most often in the case of PropertyDrawers, though I've ended up using it elsewhere to make code which is re-usable across a range of objects. You're right, if you know what you've got, it's faster and clearer to do it directly.

avatar image vexe · Feb 22, 2014 at 01:39 PM 0
Share

Thanks. Finally, can you shed some light on GetParent? what is 'parent' in this context? and what kind of useful things I could do with this parent?

avatar image whydoidoit · Feb 22, 2014 at 01:45 PM 1
Share

So that's really the PropertyDrawer thing - a Drawer knows the individual property type etc, but it has no idea what owns that property. I (and a few other people) have had problems where we need to understand the context of the property to create a useful drawer.

So GetParent returns the object that owns the property that is currently being used.

1 Reply

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

Answer by whydoidoit · Feb 22, 2014 at 10:41 AM

As far as I know you can't do that - the serialized properties of your custom classes end up in the stream of serialization rather than having object reference values. It appears that this causes issues around the duplication of shared objects in the serialization at times.

This: http://answers.unity3d.com/questions/425012/get-the-instance-the-serializedproperty-belongs-to.html might help as it uses reflection to map property accesses to the names in the serialization stream to return the actual object being reference (or its parent).

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

20 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

Related Questions

Is there a reason not to inherit from UnityEngine.Object if I wanted the basic functionalities that UE.Object gives? 1 Answer

Testing for null, bug or feature? 2 Answers

Why this error when trying to instantiate object ?(Solved) 2 Answers

Is there a way to Undo.RecordObject on a System.Object? 1 Answer

[SerializeField] Not Working Properly? 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