1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. Unity 5.6 is now released.
    Dismiss Notice
  3. Check out all the fixes for 5.5 in patch releases 1 & 2.
    Dismiss Notice
  4. Get further faster with the Unity Plus Accelerator Pack, free for new Unity Plus subscribers for a limited time. Click here for more details.
    Dismiss Notice
  5. Learn how you'll soon be able to publish your games to China in four simple steps with Xiaomi. Sign up now for early access.
    Dismiss Notice

SerializedObject data getting wiped when apply to prefab?

Discussion in 'Scripting' started by dmcdonough, Jun 27, 2012.

  1. dmcdonough

    dmcdonough

    Joined:
    May 18, 2012
    Posts:
    2
    Like so many others I am having terrific frustrations trying to get a custom editor to set data on my objects/prefabs and have it save. After following the guide on this thread, my code looks like this:

    First, the storage container class and the actual custom component:

    Code (csharp):
    1. [System.Serializable()]
    2.     public class CreatureAnimationPair : Object
    3.     {
    4.         public string animationHandle { get; set; }
    5.         public CreaturerAnimationType type { get; set; }
    6.    
    7.         public CreatureAnimationPair (string animationHandle, CreatureAnimationType type)
    8.         {
    9.             this.animationHandle = animationHandle;
    10.             this.type = type;
    11.         }
    12.     }
    13.    
    14.     [System.Serializable()]
    15.     public class CreatureAnimation : MonoBehaviour
    16.     {
    17.         public List<CreatureAnimationPair> animationPairs;
    18.        
    19.         ...
    20.        
    21.     }
    Then, the editor class:

    Code (csharp):
    1.  [CustomEditor(typeof(CreatureAnimation))]
    2.     public class CreatureAnimationEditor : Editor
    3.     {
    4.         SerializedObject creatureAnimationSO;
    5.         CreatureAnimation creatureAnimation;
    6.    
    7.     void OnEnable ()
    8.     {
    9.         creatureAnimationSO = new SerializedObject(target);
    10.         creatureAnimation = (CreatureAnimation)target;
    11.        
    12.         Animation animation = creatureAnimation.gameObject.GetComponentInChildren<Animation>();
    13.         if (animation != null  creatureAnimation.animationPairs.Count <= 0)
    14.         {
    15.             List<CreatureAnimationPair> newPairs = new List<CreatureAnimationPair>();
    16.             foreach (AnimationState anim in animation)
    17.             {
    18.                 newPairs.Add(new CreatureAnimationPair(anim.name, CreatureAnimationType.None));
    19.             }
    20.        
    21.         creatureAnimation.animationPairs = newPairs;
    22.        
    23.         EditorUtility.SetDirty(creatureAnimation);
    24.         creatureAnimationSO.ApplyModifiedProperties();
    25.     }
    In this case, CreatureAnimationPair is an enum set I've made. When I use this on a creature instantiated in the scene, it'll go through all its animations like I want it to and fill out the list of pairs. I can see them in the inspector using my custom GUI just fine. But as soon as I apply that to a prefab version of the creature, all the data in that list gets wiped -- all the strings are emptied and all the enum values become garbage. Why is this happening? How do I get the changes I make in the scene to persist into the prefab?