Skip to content

Instantly share code, notes, and snippets.

@frarees
Created October 7, 2014 13:38
Show Gist options
  • Save frarees/29ca649090310f3c1ffa to your computer and use it in GitHub Desktop.
Save frarees/29ca649090310f3c1ffa to your computer and use it in GitHub Desktop.
Class Layout Update on Unity3D
using UnityEngine;
using System.Collections;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class Test : MonoBehaviour, ISerializationCallbackReceiver {
[SerializeField, HideInInspector] int m_SerializedVersion;
[SerializeField] string m_NewString;
#if UNITY_EDITOR
string m_Buffer;
#endif
void ISerializationCallbackReceiver.OnBeforeSerialize () {
}
void ISerializationCallbackReceiver.OnAfterDeserialize () {
#if UNITY_EDITOR
if (m_SerializedVersion < 1) {
m_NewString = m_Buffer;
}
m_SerializedVersion = 1;
#endif
}
}
using UnityEngine;
using System.Collections;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class Test : MonoBehaviour, ISerializationCallbackReceiver {
[SerializeField, HideInInspector] int m_SerializedVersion;
[SerializeField] string m_OldString;
#if UNITY_EDITOR
string m_Buffer;
#endif
void ISerializationCallbackReceiver.OnBeforeSerialize () {
#if UNITY_EDITOR
m_Buffer = m_OldString;
#endif
}
void ISerializationCallbackReceiver.OnAfterDeserialize () {
#if UNITY_EDITOR
if (m_SerializedVersion > 0) {
m_OldString = m_Buffer;
}
m_SerializedVersion = 0;
#endif
}
}
@frarees
Copy link
Author

frarees commented Oct 7, 2014

  1. Create Test.cs in your project
  2. Put the contents of Old.cs in your newly created Test.cs
  3. Create a new GameObject, add a Test component to it and put some text on Old String
  4. Put the contents of New.cs in Test.cs
  5. You will see your New String using the data that was before on Old String
  6. You can also go back to Old.cs, and will get the data that you got in New String there

@frarees
Copy link
Author

frarees commented Oct 7, 2014

Note that m_Buffer is actually serialized and not saved to disk (i.e. data will survive an assembly reload). This won't work if you mark that field with NonSerializable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment