Search Unity

custom editor for custom classes

Discussion in 'Scripting' started by manni1981, Mar 6, 2010.

  1. manni1981

    manni1981

    Joined:
    Feb 18, 2010
    Posts:
    15
    I'm trying to create an AnimationSystem for uv-animation with a custom editor for my animations class settings. However, the components (and their members) seem to get created a new on starting the game, which means that the constructor for my UVAnimationSystem and it's members are called, which throws away all editor choices.

    Do i need to override a clone() or copy() function or create a copy constructor for that to work? sorry to ask such a dumb question, but i couldn't find any documentation to that problem. Standard datatypes (float, int, ...) seem to work, but for custom classes you seem to have to do something additional to make it work

    beneath are snippets of my current classes

    thanks for any help
    Code (csharp):
    1.  
    2. class UVAnimationSystem extends MonoBehaviour
    3. {
    4.     public function UVAnimationSystem()
    5.     {
    6. ...
    7. }
    8.     private var animations:Array = new Array();
    9. // array holds UVAnimation instances
    10. }
    11.  
    12. class UVAnimation
    13. {
    14.     public function UVAnimation()
    15.     {  
    16.         anim = AnimId.ANIM_NONE;
    17.         mode = CounterMode.MODE_LOOP;
    18.     }
    19.     private var anim:AnimId;
    20.     private var mode:CounterMode;
    21. ...
    22. + set/get
    23. }
    24.  
    25. // editor
    26. @CustomEditor(UVAnimationSystem)
    27.  
    28. class UVAnimationSystemEditor extends Editor
    29. {
    30.     public function OnInspectorGUI()
    31.     {
    32.         var animSystem:UVAnimationSystem = target;
    33.         var animations:Array = animSystem.GetAnimations();
    34.         var lastAnimationsLength:int = animations.length;
    35.         var animationCount:int = animations.length;
    36.         EditorGUILayout.BeginHorizontal();
    37.         animationCount = EditorGUILayout.IntField("AnimationCount", animationCount);
    38.         animations.length = (animationCount > 0)?animationCount:0;
    39.         EditorGUILayout.EndHorizontal();
    40.        
    41.         var i:int;
    42.         for (i  = 0; i < animations.length; i++)
    43.         {
    44.             if (animations[i] == null)
    45.             {
    46.                 animations[i] = new UVAnimation();
    47.             }
    48.        
    49.             EditorGUILayout.BeginHorizontal();
    50.             animations[i].SetAnim(EditorGUILayout.EnumPopup("Animation", animations[i].GetAnim()));
    51.             EditorGUILayout.EndHorizontal();
    52.             EditorGUILayout.BeginHorizontal();
    53.             animations[i].SetMode(EditorGUILayout.EnumPopup("Mode", animations[i].GetMode()));
    54.             EditorGUILayout.EndHorizontal();
    55. EditorGUILayout.Separator();
    56. }
    57. }
    58. }
    59.  
     
  2. manni1981

    manni1981

    Joined:
    Feb 18, 2010
    Posts:
    15
    no idea anyone? i'm grateful for any direction you can point me. as reward i'll be putting the animationssystem online once that is worked out (it's really the last stone that's missing..)

    cheers, manni
     
  3. manni1981

    manni1981

    Joined:
    Feb 18, 2010
    Posts:
    15
    okay, to make it a bit easier, here is a shorter version for testing. still the array length of MyComponent gets set to zero on starting. Is there a need for a copy constructor? clone function? any ideas? or am i getting something wrong with the editor?

    thx

    Code (csharp):
    1.  
    2. class MyComponent extends MonoBehaviour
    3. {
    4.     public function MyComponent()
    5.     {
    6.     }
    7.    
    8.     public var myClassArray:Array = new Array();
    9. }
    10.  
    11. class MyClass extends Object
    12. {
    13.     public function MyClass()
    14.     {
    15.     }
    16.    
    17.     public var a:int = 4;
    18.     public var b:int = 3;
    19. }
    20.  
    21. @CustomEditor(MyComponent)
    22. class MyComponentEditor extends Editor
    23. {
    24.     public function OnInspectorGUI()
    25.     {
    26.         var myComponent:MyComponent = target;
    27.  
    28.         var myArray:Array = myComponent.myClassArray;
    29.         var arrayLength:int = myArray.length;
    30.  
    31.         EditorGUILayout.BeginHorizontal();
    32.         myComponent.myClassArray.length = EditorGUILayout.IntField("MyClassA", arrayLength);
    33.         EditorGUILayout.EndHorizontal();
    34.        
    35.         var i:int;
    36.         if (arrayLength != myComponent.myClassArray.length)
    37.         {
    38.             for (i = 0 ; i <myComponent.myClassArray.length; i++)
    39.             {
    40.                 myComponent.myClassArray[i] = new MyClass();
    41.             }
    42.             Repaint();
    43.         }
    44.        
    45.         for (i = 0 ; i <myComponent.myClassArray.length; i++)
    46.         {
    47.             EditorGUILayout.BeginHorizontal();
    48.             myComponent.myClassArray[i].a = EditorGUILayout.IntField("MyClass-a"+i, myComponent.myClassArray[i].a);
    49.             EditorGUILayout.EndHorizontal();           
    50.             EditorGUILayout.BeginHorizontal();
    51.             myComponent.myClassArray[i].b = EditorGUILayout.IntField("MyClass-b"+i, myComponent.myClassArray[i].b);
    52.             EditorGUILayout.EndHorizontal();           
    53.         }
    54.     }
    55. }
    56.  
     
  4. Der Dude

    Der Dude

    Joined:
    Aug 7, 2006
    Posts:
    213
    I think the problem is that Unity does not store the Array in MyComponent. You can force Unity to do this by giving it the @SerializeField attribute.
    If this does not work try using the other array declaration, as this is editable in the default inspector:

    Code (csharp):
    1.  
    2. var myClassArray : MyClass[];
    3.  
    I think as a rule of thumb you can say that all fields that aren't editable in the default inspector, do not get saved.
     
  5. manni1981

    manni1981

    Joined:
    Feb 18, 2010
    Posts:
    15
    @Serialize didn't help, because it was already public

    using the other array definition did solve the problem however. I'll post the animsystem as soon as i'm finished, perhaps someone can use it.
     
  6. manni1981

    manni1981

    Joined:
    Feb 18, 2010
    Posts:
    15
    here is my code, drag the uvAnimationSystem on a gameobject, edit the settings and add the animations

    then in the gamecode use
    var animSystem:Animation = SystemgameObject.getComponent(AnimationSystem);
    animSystem.PlayAnimation(AnimId.ANIM_MOVE_LOOP); to play the animation

    cheerio, comments will be added later :)
     

    Attached Files: