Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
0
Question by Xarbrough · Jul 24, 2017 at 10:06 AM · c#serializationresetfield

Prevent Reset() from clearing out serialized fields

I have a component which stores a unique id, once it gets created. After it was once set, the id should never change.

However, when a designer selects the cog and "Reset" function on the component, all serialized values are cleared. I know I can implement my own Reset() call, but that doesn't help, because it gets called, after the fields were already cleared.

Is there any way I can prevent this or get a callback so that I can save and restore my unique id? I'm trying to prevent my level designes from accidentally corrupting references to said ids.

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 CaseyHofland · Jul 24, 2020 at 12:51 AM 0
Share

EDIT: This was my old answer, I've since created a better solution: look for my icon to find my new answer.

Old question, but it is certainly possible! All you need is a NonSerialized value that you use as a dump for your serialized value. Like so:

 public class MyClass : MonoBehaviour, ISerializationCallbackReceiver
 {
     // [HideInspector] in your case I'd suggest this as well.
     [SerializeField] private int uniqueID;
     private int uniqueIDDump;
 
     public void OnAfterDeserialize()
     {
         if(uniqueIDDump != default) // or use a Nullable
         {
             uniqueID = uniqueIDDump;
         }
     }
 
     public void OnBeforeSerialize()
     {
         uniqueIDDump = uniqueID;
     }
 }


Even if you call your uniqueID in Reset, it will have its old value.

Note that the "default" check is important! Since the dump is not serialized, in some cases (e.g. Editor Restart) it will reset the dump. In these cases we may assume the uniqueID has been serialized and will restore the dump on its soonest OnBeforeSerialize call, which will always be before any editing takes place.

2 Replies

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

Answer by Bunny83 · Jul 24, 2017 at 10:27 AM

No, you can't prevent that fundamental editor feature. Just like you can't prevent the designer from removing the component and re-adding a new one which would result in the same problem.

Yout only options would be setting the hideflags and prevent interaction completely. Either set the "NotEditable" flag which would display the whole component "inactive" (just like the components on an imported fbx model). Or if the component doesn't even need to be seen you can completely hide it from the inspector by setting the "HideInInspector" flag as well.

However keep in mind that there are still ways to at least mess up your IDs. If the object gets duplicated or stored as prefab it would still have the same ID. So you would have multiple objects with the same ID.

However it depends on the actual usage of the component.

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 Xarbrough · Jul 24, 2017 at 11:03 AM 0
Share

Thanks, those are some helpful pointers. I think the NotEditable flag might help. Still have to think about prefabs though. $$anonymous$$aybe I can retrieve the local file identifier from the scene asset and assign it as guid to my component when it is added to the scene, or something like that.

avatar image
0

Answer by CaseyHofland · May 11 at 09:39 PM

alt text
It is possible, and in a very intuitive way. First, copy this wrapper:

     [Serializable]
     public struct NonResetable<T> : ISerializationCallbackReceiver
     {
         public T value;
         private T _dump;
 
         [SerializeField] [HideInInspector] private bool valid;
 
         public static implicit operator T(NonResetable<T> nonResetable) => nonResetable.value;
         public static implicit operator NonResetable<T>(T value) => new NonResetable<T> { value = value };
 
         public void OnBeforeSerialize()
         {
             _dump = value;
         }
 
         public void OnAfterDeserialize()
         {
             if (!valid)
             {
                 value = _dump;
                 valid = true;
             }
         }
     }


You don't need to know how it works in order to use it. It automatically converts to and from the type it's wrapping, so you can use a NonResetable 'mostly' like you would a float.

 public NonResetable<float> f = 5f;
 float SomeMethod() => 10 * f; // Returns 50.


For styling, I would also suggest you copy this script inside an Editor folder:

     [CustomPropertyDrawer(typeof(NonResetable<>))]
     public class NonResetableDrawer : PropertyDrawer
     {
         public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
         {
             EditorGUI.BeginProperty(position, label, property);
             
             var valueProperty = property.FindPropertyRelative(nameof(NonResetable<bool>.value));
             EditorGUI.PropertyField(position, valueProperty, label);
 
             EditorGUI.EndProperty();
         }
     }



ezgifcom-gif-maker-1.gif (389.5 kB)
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

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Difference between inline constructor with [SerializeField] above it, and calling a constructor from Reset() 2 Answers

does this key not found error have to do with serialization? 2 Answers

How can you put a serializable object in an editor window? 0 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