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
1
Question by andrew-lukasik · Sep 01, 2016 at 09:37 AM · serializationeditorwindowjsoninstanceunityengine.object

UnityEngine.Object field pointing to Project file gets serialized by JSON as instance ID. After deserialization field points to different Object. Why?

I'm writing todo list in EditorWindow. All it's data is being serialized as separate text JSON files. One of the fields in every entry is of type UnityEngine.Object and they are pointing to Project files for some context (serialized by JSON as instanceID). But problem is: after editor restart and deserialization this field now points to very different Object.

I observed that one particular asset has id of "19658" but reference to it gets serialized as different number, "16670" for example. I suspect instanceID is not good value to use but I'm not sure what is the best way to go about it. Any ideas?

Serialize Object field as asset path instead? (not ideal as easily breakable)

Some another type of GUID?

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
1
Best Answer

Answer by troien · Sep 01, 2016 at 09:51 AM

I'm doing this out of my head and I'm not 100% sure. But I believe InstanceID as you describe does not survive serialization/deserialization (I thought that it for this reason didn't even survive going in/out of playmode but I'm not sure on that one).

Anyway, I believe you actually provided the answer kinda with your question :p

Use the guid, which you can find from the assetpath

  • AssetDatabase.GetAssetPath()

  • AssetDatabase.AssetPathToGUID()

  • AssetDatabase.GUIDToAssetPath()

  • AssetDatabase.LoadAssetAtPath()

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 andrew-lukasik · Sep 01, 2016 at 08:31 PM 0
Share

Thank you! Worked like a charm.

Solution example for future reference:

 [System.Serializable]
 class SomeClassWithObjectField : ISerializationCallbackReceiver {
     #region fields
 
     [System.NonSerialized] public Object subject;
     [SerializeField] string _subject;
 
     #endregion
     #region ISerializationCallbackReceiver implementation
 
     public void OnBeforeSerialize () {
         if( this.subject!=null ) {
             this._subject = AssetDatabase.AssetPathToGUID(
                 AssetDatabase.GetAssetPath( this.subject )
             );
         }
         else {
             this._subject = null;
         }
     }
 
     public void OnAfterDeserialize () {
         if( this._subject!=null ) {
             this.subject = AssetDatabase.Load$$anonymous$$ainAssetAtPath(
                 AssetDatabase.GUIDToAssetPath( this._subject )
             );
         }
     }
 
     #endregion
 }

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

56 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

Related Questions

When does the "InstanceID" get assigned, and does it seralize with the Object? 2 Answers

EditorWindow - Child class gets cast back to parent class at compilation. 2 Answers

How do I go about deserializing a json array? 3 Answers

What are the pros and cons of ScriptableObjects vs. JSON for data files? 2 Answers

How do serialized Texture2D fields store and manage Texture2D data pulled from a dll ? 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