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 skalev · Oct 31, 2012 at 09:12 PM · editorcomponentserializeguid

Unity doesn't store GUID field?

Hi All.

I have a MonoBehaviour that has two GUID fields in it. I set those through the custom inspector, but for some reason, it doesn't store the changes into the component, so when I hit play or save the scene the values disappear.

I'm setting the target to be dirty in my custom inspector, and I have also tried to mark the GUIDs with [SerializeField].

Any one has any insight on this?

EDIT:

I've solved the problem by storing the GUID as string. This still doesn't explain the issue though, would love an explanation.

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

2 Replies

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

Answer by Bunny83 · Oct 31, 2012 at 11:38 PM

The Guid type is not a basic data type. Just read the documentation of SerializeField. There's a list of types which Unity can serialize.

Unity only serializes basic datatypes and basic datatype-variables in custom classes when the class has a Serializable attribute. Guid is not a class, it's a struct, so that's the first reason why it isn't serialized. Unity can only serialize custom classes. The second reason is, all the GUID parts are private members (shorts). Private members aren't serliazed unless they have the SerializeField attribute.

SerializeField can only be used for fields of a supported 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 skalev · Nov 01, 2012 at 12:34 AM 0
Share

Thanks, That makes sense.

avatar image
0

Answer by pld · Jan 22, 2016 at 02:08 PM

I know this is an ancient question, but here is an example of how to create a wrapper class and custom propery draw-er to work around this restriction.

 using UnityEngine;
 #if UNITY_EDITOR
 using UnityEditor;
 #endif
 
 #if UNITY_EDITOR
 [CustomPropertyDrawer(typeof(StringyGuid))]
 public class StringyGuidDrawer : PropertyDrawer {
   bool DO_VALIDATION = true;
   public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
     EditorGUI.BeginProperty(position, label, property);
     position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
     var indent = EditorGUI.indentLevel;
     EditorGUI.indentLevel = 0;
 
     var storageProp = property.FindPropertyRelative("m_storage");
     if (DO_VALIDATION) {
       var oldval = storageProp.stringValue;
       var newval = EditorGUI.DelayedTextField(position, oldval);
       if (oldval != newval) {
         try {
           storageProp.stringValue = new System.Guid(newval).ToString("D");
         } catch (System.FormatException) {}
       }
     } else {
       EditorGUI.PropertyField(position, storageProp, GUIContent.none);
     }
     EditorGUI.indentLevel = indent;
     EditorGUI.EndProperty();
   }
 }
 #endif
 
 [System.Serializable]
 public struct StringyGuid {
   [SerializeField]
   private string m_storage;
 
   public static implicit operator StringyGuid(System.Guid rhs) {
     return new StringyGuid { m_storage = rhs.ToString("D") };
   }
 
   public static implicit operator System.Guid(StringyGuid rhs) {
     if (rhs.m_storage == null) return System.Guid.Empty;
     try {
       return new System.Guid(rhs.m_storage);
     } catch (System.FormatException) {
       return System.Guid.Empty;
     }
   }
 }
 
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

12 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

Related Questions

[RESOLVED]How to get all available parameters from target Component 3 Answers

Create a script that inherits from a class, and than attach multiple inherited classes to unity editor 2 Answers

Way to scale all assets in a scene 1 Answer

Keeping information on custom Inspector window using List of List 1 Answer

How to add a component on a GameObject in Custom Inspector 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