コード例 #1
0
 // Unity 重新编译后,非序列化的引用会丢失,这里将旧的实例都销毁掉
 void OnEnable()
 {
     if (gameObject != _globalGameObject)
     {
         GeneralKit.DestroySafely(gameObject);
     }
 }
コード例 #2
0
 void OnDestroy()
 {
     foreach (var editor in _editors)
     {
         GeneralKit.DestroySafely(editor);
     }
     _editors.Clear();
 }
コード例 #3
0
            public override void OnInspectorGUI()
            {
                EditorGUILayout.BeginVertical(EditorStyles.helpBox);

                var rect  = EditorGUILayout.GetControlRect();
                var rect2 = rect;

                // controller foldout
                using (var scope = new ChangeCheckScope(target))
                {
                    rect2.width = rect2.height;
                    var result = GUI.Toggle(rect2, target._foldoutController, GUIContent.none, EditorStyles.foldout);
                    if (scope.changed)
                    {
                        target._foldoutController = result;
                    }
                }

                // controller label
                rect.xMin = rect2.xMax;
                EditorGUI.LabelField(rect, "Controller", EditorStyles.boldLabel);

                serializedObject.Update();

                // controller settings
                if (target._foldoutController)
                {
                    EditorGUILayout.PropertyField(_durationProp);
                    EditorGUILayout.PropertyField(_updateModeProp);
                    EditorGUILayout.PropertyField(_timeModeProp);
                    EditorGUILayout.PropertyField(_wrapModeProp);
                    EditorGUILayout.PropertyField(_arrivedActionProp);
                    GUILayout.Space(4);
                }

                EditorGUI.DrawRect(EditorGUILayout.GetControlRect(false, 1), EditorStyles.centeredGreyMiniLabel.normal.textColor);
                GUILayout.Space(4);

                rect  = EditorGUILayout.GetControlRect();
                rect2 = rect;

                // play button
                rect2.width = EditorGUIUtility.singleLineHeight * 2 - 4;
                using (new GUIContentColorScope(target.playing ? progressForegroundValid : EditorGUIKit.defaultTextColor))
                {
                    target.playing = GUI.Toggle(rect2, target.playing,
                                                EditorGUIKit.TempContent(image: EditorAsset.instance.play), imageButtonStyle);
                }

                // direction button
                rect2.x = rect.xMax - rect2.width;
                using (new DisabledScope(!target.playing))
                {
                    using (new GUIContentColorScope(EditorGUIKit.defaultTextColor))
                    {
                        if (GUI.Button(rect2, EditorGUIKit.TempContent(image: target.direction == PlayDirection.Forward ?
                                                                       EditorAsset.instance.rightArrow : EditorAsset.instance.leftArrow), imageButtonStyle))
                        {
                            target.ReverseDirection();
                        }
                    }
                }

                rect.xMin += EditorGUIUtility.singleLineHeight * 2;
                rect.xMax -= EditorGUIUtility.singleLineHeight * 2;

                // 鼠标开始拖动
                if (Event.current.type == EventType.MouseDown)
                {
                    if (rect.Contains(Event.current.mousePosition))
                    {
                        target.dragging = true;
                    }
                }

                // 鼠标结束拖动
                if (Event.current.rawType == EventType.MouseUp)
                {
                    if (target.dragging)
                    {
                        target.dragging = false;
                        Repaint();
                    }
                }

                // progress bar
                using (var scope = new ChangeCheckScope(null))
                {
                    float progress = EditorGUIKit.ProgressBar(rect, target._normalizedTime, progressBackgroundInvalid, progressForegroundValid);
                    if (scope.changed && target.dragging)
                    {
                        target.normalizedTime = progress;
                    }
                }

                GUILayout.Space(4);

                EditorGUILayout.EndVertical();
                GUILayout.Space(4);
                EditorGUILayout.BeginVertical(EditorStyles.helpBox);

                rect  = EditorGUILayout.GetControlRect();
                rect2 = rect;

                // events foldout
                using (var scope = new ChangeCheckScope(target))
                {
                    rect2.width = rect2.height;
                    var result = GUI.Toggle(rect2, target._foldoutEvents, GUIContent.none, EditorStyles.foldout);
                    if (scope.changed)
                    {
                        target._foldoutEvents = result;
                    }
                }

                // events label
                rect.xMin = rect2.xMax;
                EditorGUI.LabelField(rect, "Events", EditorStyles.boldLabel);

                // events
                if (target._foldoutEvents)
                {
                    EditorGUILayout.PropertyField(_onForwardArrivedProp);
                    GUILayout.Space(2);
                    EditorGUILayout.PropertyField(_onBackArrivedProp);
                    GUILayout.Space(2);
                }

                serializedObject.ApplyModifiedProperties();

                EditorGUILayout.EndVertical();
                GUILayout.Space(4);
                EditorGUILayout.BeginVertical(EditorStyles.helpBox);

                // 绘制 Animation 列表。为了正确处理各种意外情况(比如其他代码增删了列表),因此处理比较复杂 :-/
                if (!GeneralKit.IsNullOrEmpty(target._animations))
                {
                    int editorIndex = 0;
                    for (int i = 0; i < target._animations.Count; i++)
                    {
                        var anim = target._animations[i];
                        if (anim)
                        {
                            if (_editors.Count <= editorIndex)
                            {
                                _editors.Add(null);
                            }

                            if (!_editors[editorIndex] || _editors[editorIndex].target != anim)
                            {
                                GeneralKit.DestroySafely(_editors[editorIndex]);
                                _editors[editorIndex] = CreateEditor(anim);
                            }

                            (_editors[editorIndex] as TweenAnimation.Editor).OnInspectorGUI(target);
                            editorIndex++;
                        }
                        else
                        {
                            target._animations.RemoveAt(i--);
                        }
                    }
                    for (; editorIndex < _editors.Count; editorIndex++)
                    {
                        GeneralKit.DestroySafely(_editors[editorIndex]);
                        _editors.RemoveAt(editorIndex--);
                    }
                }

                // add button
                GUILayout.Space(4);
                var buttonRect = EditorGUILayout.GetControlRect();

                if (GUI.Button(buttonRect, "Add Animation...", EditorStyles.miniButton))
                {
                    ShowAddMenu(buttonRect);
                }
                GUILayout.Space(4);

                EditorGUILayout.EndVertical();
            }