Search Unity

Custom Inspector GUI - Script Serialization Not Saving

Discussion in 'Scripting' started by CodeGoBrrr, Jan 31, 2015.

  1. CodeGoBrrr

    CodeGoBrrr

    Joined:
    Aug 17, 2013
    Posts:
    13
    Hey everyone,

    So I've been working on a small script to allow you to dynamically set the parameter values within the Animator, it works via reflection and I decided to make a custom GUI in the inspector for it, to make it easier to select the field name and parameters. As originally I had it so you had to type out the name of the field, which is not a huge issue but can be a pain when the lookup for the field fails and throws an exception.

    All of that works, but when ever I reload the scene or go into play, it does not save the component. I've googled around and tried as many solutions as I could find but nothing has worked. This has been my first attempt at a custom GUI and use of Serializable.

    Heres how it currently looks:


    and the code:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using UnityEditorInternal;
    4. using System.Collections;
    5. using System.Reflection;
    6. using System;
    7.  
    8. public class AnimationController : MonoBehaviour {
    9.  
    10.     public Animator animator;
    11.     [SerializeField]
    12.     public ArrayList classes = new ArrayList();
    13.     private GUIContent[] paramaters;
    14.  
    15.     void Update () {
    16.         foreach (AnimationClass animationClass in classes)
    17.             foreach (AnimationField animationField in animationClass.animations)
    18.                 setParamater(animationClass.getValue(animationField), getParameter(animationField));
    19.     }
    20.  
    21.     void setParamater(object perm, string str) {
    22.         if (perm is float)
    23.             animator.SetFloat(str, (float)perm);
    24.         else if (perm is int)
    25.             animator.SetInteger(str, (int)perm);
    26.         else if (perm is bool)
    27.             animator.SetBool(str, (bool)perm);
    28.     }
    29.  
    30.     public GUIContent[] getParamaters() {
    31.         AnimatorController controller = AnimatorController.GetEffectiveAnimatorController(animator);
    32.         if(paramaters == null || paramaters.Length != controller.parameterCount){
    33.             paramaters = new GUIContent[controller.parameterCount];
    34.             for(int i = 0; i < paramaters.Length; i++){
    35.                 AnimatorControllerParameter par = controller.GetParameter(i);
    36.                 paramaters[i] = new GUIContent(par.name, par.type.ToString());
    37.             }
    38.         }
    39.         return paramaters;
    40.     }
    41.  
    42.     public string getParameter(AnimationField animation) {
    43.         return paramaters[animation.parameterIndex].text;
    44.     }
    45.  
    46.     [Serializable]
    47.     public class AnimationClass {
    48.         private GUIContent[] content;
    49.         private FieldInfo[] fields;
    50.         public bool show;
    51.  
    52.         public MonoBehaviour component;
    53.         public ArrayList animations = new ArrayList();
    54.  
    55.         public FieldInfo[] getFields() {
    56.             if (fields == null)
    57.                 fields = component.GetType().GetFields();
    58.             return fields;
    59.         }
    60.  
    61.         public GUIContent[] getFieldContent() {
    62.             if (fields == null)
    63.                 fields = component.GetType().GetFields();
    64.  
    65.             if (content == null) {
    66.                 content = new GUIContent[fields.Length];
    67.                 for (int i = 0; i < content.Length; i++)
    68.                     content[i] = new GUIContent(fields[i].Name);
    69.             }
    70.             return content;
    71.         }
    72.  
    73.         public object getValue(AnimationField animation) {
    74.             float time = Time.time;
    75.             object val = fields[animation.fieldIndex].GetValue(component);
    76.             print(Time.time - time);
    77.             return val;
    78.         }
    79.     }
    80.  
    81.     public class AnimationField {
    82.         public int fieldIndex = 0;
    83.         public int parameterIndex = 0;
    84.     }
    85.  
    86.     [CustomEditor(typeof(AnimationController))]
    87.     public class AnimationControlEditor : Editor {
    88.  
    89.         public override void OnInspectorGUI() {
    90.             AnimationController controller = target as AnimationController;
    91.  
    92.             DrawDefaultInspector();
    93.             EditorGUILayout.Separator();
    94.  
    95.             foreach (AnimationClass animationClass in controller.classes) {
    96.                 if (animationClass.show = EditorGUILayout.Foldout(animationClass.show, new GUIContent(animationClass.component != null ? animationClass.component.name : "Animation Class"))) {
    97.                     animationClass.component = EditorGUILayout.ObjectField(new GUIContent("Component:"), animationClass.component, typeof(MonoBehaviour)) as MonoBehaviour;
    98.                     if (animationClass.component == null)
    99.                         continue;
    100.  
    101.                     foreach (AnimationField animationField in animationClass.animations) {
    102.                         EditorGUILayout.Space();
    103.                         animationField.fieldIndex = EditorGUILayout.Popup(new GUIContent("Field:"), animationField.fieldIndex, animationClass.getFieldContent());
    104.                         animationField.parameterIndex = EditorGUILayout.Popup(new GUIContent("Paramater:"), animationField.parameterIndex, controller.getParamaters());
    105.                     }
    106.  
    107.                     if (GUILayout.Button("Add Field"))
    108.                         animationClass.animations.Add(new AnimationField());
    109.                 }
    110.             }
    111.  
    112.             if (GUILayout.Button("Add Class"))
    113.                 controller.classes.Add(new AnimationClass());
    114.  
    115.             EditorGUILayout.Separator();
    116.             if (GUI.changed) {
    117.                 EditorUtility.SetDirty(controller);
    118.             }
    119.         }
    120.     }
    121. }
    Any help or pointing in the right direction will be greatly appreciated.
     
  2. CodeGoBrrr

    CodeGoBrrr

    Joined:
    Aug 17, 2013
    Posts:
    13
    Nevermind after reading the documentation I realised that it was ArrayList which was causing the issue, changed it to List<> and it has fixed the issue.