Beispiel #1
0
        /// <summary>
        /// 找到一组元素中最接近的一个
        /// </summary>
        /// <returns> 最接近的元素下标 </returns>
        public static int FindNearest(this float[] items, float value)
        {
            if (GeneralKit.IsNullOrEmpty(items)) return -1;

            int result = 0;
            float minError = Mathf.Abs(value - items[0]);
            float error;

            for (int i = 1; i < items.Length; i++)
            {
                error = Mathf.Abs(value - items[i]);
                if (error < minError)
                {
                    minError = error;
                    result = i;
                }
            }

            return result;
        }
        protected override void OnValidate()
        {
            base.OnValidate();

            if (!GeneralKit.IsNullOrEmpty(_animations))
            {
                // 删除自身引用的错误的 Animation 组件
                GetComponents(_tweenList);
                _tweenList.Remove(this);
                for (int i = 0; i < _animations.Count; i++)
                {
                    if (_animations[i] && _animations[i].gameObject == gameObject && !AnyContainsInTweenList(_animations[i]))
                    {
                        continue;
                    }
                    _animations.RemoveAt(i--);
                }
                _tweenList.Clear();
            }

            _objectList.Add(gameObject);
        }
            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();
            }