Esempio n. 1
0
 private void SetAnimationByIndex(int index)
 {
     if (index >= 0 && model.animations.Count > index)
     {
         if (animIndex != index && model.animations[index] != null)
         {
             EditorPrefs.SetInt(model.GetInstanceID().ToString(), index);
             animIndex = index;
         }
         animReorderableList.index = index;
     }
 }
Esempio n. 2
0
        void OnEnable()
        {
            model = target as MeshModel;

            animReorderableList = new UnityEditorInternal.ReorderableList(serializedObject, serializedObject.FindProperty("animations"))
            {
                drawHeaderCallback = (Rect rect) =>
                {
                    EditorGUI.LabelField(rect, new GUIContent("Animations", "animation list to bake"));
                },

                drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
                {
                    SerializedProperty element = animReorderableList.serializedProperty.GetArrayElementAtIndex(index);
                    rect.y += 2;

                    const float LEFT_MARGIN    = 50;
                    float       rectWidth      = rect.width - LEFT_MARGIN;
                    float       popupWidth     = rect.width * 0.4f;
                    const float CHECKBOX_WIDTH = 15;

                    SerializedProperty clipProperty = element.FindPropertyRelative("clip");
                    float animClipWidth             = rectWidth - popupWidth - CHECKBOX_WIDTH;
                    EditorGUI.BeginChangeCheck();
                    EditorGUI.PropertyField(
                        new Rect(rect.x + LEFT_MARGIN, rect.y, animClipWidth, EditorGUIUtility.singleLineHeight),
                        clipProperty, GUIContent.none);
                    if (EditorGUI.EndChangeCheck())
                    {
                        model.animations[index].selectedFrames.Clear();
                    }

                    if (model.referenceController != null && refStateMachine != null && refStateMachine.states.Length > 0 && clipProperty.objectReferenceValue != null)
                    {
                        string[] stateNames = new string[refStateMachine.states.Length];
                        for (int i = 0; i < refStateMachine.states.Length; ++i)
                        {
                            stateNames[i] = refStateMachine.states[i].state.name;
                        }

                        SerializedProperty stateIndexProperty = element.FindPropertyRelative("stateIndex");
                        stateIndexProperty.intValue =
                            EditorGUI.Popup(new Rect(rect.x + LEFT_MARGIN + animClipWidth + 5, rect.y, popupWidth - CHECKBOX_WIDTH - 10, EditorGUIUtility.singleLineHeight),
                                            stateIndexProperty.intValue, stateNames);

                        SerializedProperty stateNameProperty = element.FindPropertyRelative("stateName");
                        if (stateNames.Length > stateIndexProperty.intValue)
                        {
                            stateNameProperty.stringValue = stateNames[stateIndexProperty.intValue];
                        }
                    }

                    SerializedProperty loopingProperty = element.FindPropertyRelative("isLooping");
                    loopingProperty.boolValue =
                        GUI.Toggle(new Rect(rect.x + rect.width - CHECKBOX_WIDTH - 10, rect.y, CHECKBOX_WIDTH + 10, EditorGUIUtility.singleLineHeight),
                                   loopingProperty.boolValue, new GUIContent(LoopMarkTexture, "generates looping animation clip"), GUI.skin.button);
                },

                onAddCallback = (UnityEditorInternal.ReorderableList l) => {
                    var index = l.serializedProperty.arraySize;
                    l.serializedProperty.arraySize++;
                    SerializedProperty element = l.serializedProperty.GetArrayElementAtIndex(index);
                    element.FindPropertyRelative("clip").objectReferenceValue = null;
                },

                onSelectCallback = (UnityEditorInternal.ReorderableList l) =>
                {
                    SetAnimationByIndex(l.index);
                },

                onRemoveCallback = (UnityEditorInternal.ReorderableList l) =>
                {
                    int index = l.index;
                    UnityEditorInternal.ReorderableList.defaultBehaviours.DoRemoveButton(l);

                    if (l.serializedProperty.arraySize > index)
                    {
                        reservedAnimIndex = index;
                    }
                    else if (l.serializedProperty.arraySize > 0)
                    {
                        reservedAnimIndex = l.serializedProperty.arraySize - 1;
                    }
                }
            };

            animIndex = EditorPrefs.GetInt(model.GetInstanceID().ToString(), -1);
            if (model.animations.Count > 0 && animIndex < 0)
            {
                animIndex = 0;
            }

            SetAnimationByIndex(animIndex);
        }