Exemplo n.º 1
0
    /// <summary>
    /// 初始化运行时控件
    /// </summary>
    private void InitComponent()
    {
        for (int i = 0; i < Targets.Count; i++)
        {
            LinkageAnimationTarget lat = Targets[i];

            if (lat.Target)
            {
                if (lat.PropertysRunTime == null)
                {
                    lat.PropertysRunTime = new List <LAPropertyRunTime>();
                }

                for (int j = 0; j < lat.Propertys.Count; j++)
                {
                    LAProperty        lap   = lat.Propertys[j];
                    Component         cp    = lat.Target.GetComponent(lap.ComponentName);
                    PropertyInfo      pi    = cp ? cp.GetType().GetProperty(lap.PropertyName) : null;
                    bool              valid = (cp != null && pi != null);
                    LAPropertyRunTime laprt = new LAPropertyRunTime(valid, cp, pi);
                    lat.PropertysRunTime.Add(laprt);
                }
            }
        }
    }
    /// <summary>
    /// 添加动画目标属性
    /// </summary>
    private void AddProperty(LinkageAnimationTarget lat, LAProperty lap)
    {
        lat.Propertys.Add(lap);

        object value = LinkageAnimationTool.GenerateOriginalValue(lap.PropertyType);

        for (int i = 0; i < lat.Frames.Count; i++)
        {
            lat.Frames[i].AddFrameValue(lap.PropertyType, value);
        }
    }
Exemplo n.º 3
0
    /// <summary>
    /// 更新动画帧(一个固定值)
    /// </summary>
    private void UpdateFrame(LinkageAnimationTarget lat, int currentIndex)
    {
        if (lat.Target)
        {
            LAFrame currentLAF = lat.Frames[currentIndex];

            for (int i = 0; i < lat.PropertysRunTime.Count; i++)
            {
                LAProperty        lap   = lat.Propertys[i];
                LAPropertyRunTime laprt = lat.PropertysRunTime[i];

                if (laprt.IsValid)
                {
                    object value = currentLAF.GetFrameValue(i);
                    laprt.PropertyValue.SetValue(laprt.PropertyComponent, value, null);
                }
            }
        }
    }
    /// <summary>
    /// 添加关键帧
    /// </summary>
    private void AddFrame()
    {
        if (_LA.Targets.Count <= 0)
        {
            return;
        }

        for (int i = 0; i < _LA.Targets.Count; i++)
        {
            LinkageAnimationTarget lat = _LA.Targets[i];
            LAFrame laf = new LAFrame();

            for (int j = 0; j < lat.Propertys.Count; j++)
            {
                LAProperty lap   = lat.Propertys[j];
                object     value = LinkageAnimationTool.GenerateOriginalValue(lap.PropertyType);
                laf.AddFrameValue(lap.PropertyType, value);
            }
            lat.Frames.Add(laf);
        }
        _LA.FrameLength += 1;
    }
    private void ViewGUI()
    {
        #region 右键菜单
        if (Event.current.button == 1)
        {
            if (Event.current.type == EventType.MouseDown)
            {
                GenericMenu gm       = new GenericMenu();
                Vector2     mousePos = Event.current.mousePosition;
                gm.AddItem(new GUIContent("Add Target"), false, delegate()
                {
                    AddTarget(mousePos);
                });
                gm.AddItem(new GUIContent("Add Frame"), false, delegate()
                {
                    AddFrame();
                });
                gm.ShowAsContext();
            }
        }
        #endregion

        #region 移动视野
        if (Event.current.button == 2)
        {
            if (Event.current.type == EventType.MouseDown)
            {
                _mouseDownPos = Event.current.mousePosition;
            }
            else if (Event.current.type == EventType.MouseDrag)
            {
                Vector2 direction = (Event.current.mousePosition - _mouseDownPos);
                for (int i = 0; i < _LA.Targets.Count; i++)
                {
                    _LA.Targets[i].Anchor += direction;
                }
                _mouseDownPos = Event.current.mousePosition;
                Repaint();
            }
            Event.current.Use();
        }
        #endregion

        for (int i = 0; i < _LA.Targets.Count; i++)
        {
            LinkageAnimationTarget lat = _LA.Targets[i];

            #region 窗体锚点
            SetGUIState(Color.white, Color.white, true);
            if (GUI.RepeatButton(new Rect(lat.Anchor.x - 25, lat.Anchor.y - 15, 50, 30), ""))
            {
                lat.Anchor       = Event.current.mousePosition;
                _showTargetIndex = i;
                Repaint();
            }
            #endregion

            string style = (_showTargetIndex == i ? "flow node 0 on" : "flow node 0");
            GUI.BeginGroup(new Rect(lat.Anchor.x - _width / 2, lat.Anchor.y, _width, lat.Height), new GUIStyle(style));

            #region 动画目标物体
            int h = 5;
            SetGUIState(lat.Target ? Color.white : Color.red, Color.white, true);
            lat.Target = EditorGUI.ObjectField(new Rect(5, h, _width - 56, 16), lat.Target, typeof(GameObject), true) as GameObject;
            SetGUIState(Color.white, Color.cyan, true);
            if (GUI.Button(new Rect(_width - 46, h, 18, 16), lat.ShowPropertys ? "-" : "+"))
            {
                lat.ShowPropertys = !lat.ShowPropertys;
            }
            if (GUI.Button(new Rect(_width - 23, h, 18, 16), "x"))
            {
                if (EditorUtility.DisplayDialog("提示", "是否删除物体 " + (lat.Target ? lat.Target.name: "None") + " 及其所有动画属性、动画帧!", "确定", "取消"))
                {
                    RemoveTarget(lat);
                    break;
                }
            }
            h = h + 16 + _space;
            #endregion

            #region 属性列表
            if (lat.ShowPropertys)
            {
                SetGUIState(Color.white, Color.white, lat.Target);
                EditorGUI.LabelField(new Rect(5, h, _width - 10, 16), new GUIContent("Property List:"), "BoldLabel");
                h = h + 16 + _space;

                for (int j = 0; j < lat.Propertys.Count; j++)
                {
                    LAProperty lap = lat.Propertys[j];

                    string property = lap.ComponentName + "." + lap.PropertyName + "[" + lap.PropertyType + "]";
                    if (GUI.Button(new Rect(5, h, 18, 16), "x"))
                    {
                        if (EditorUtility.DisplayDialog("提示", "是否删除 " + lat.Target.name + " 的属性 " + property + "!", "确定", "取消"))
                        {
                            RemoveProperty(lat, j);
                            break;
                        }
                    }
                    EditorGUI.LabelField(new Rect(23, h, _width - 28, 16), property);
                    h = h + 16 + _space;

                    if (_showFrameIndex != -1)
                    {
                        switch (lap.PropertyType)
                        {
                        case "Bool":
                            bool oldB = (bool)lat.Frames[_showFrameIndex].GetFrameValue(j);
                            bool newB = EditorGUI.Toggle(new Rect(23, h, _width - 28, 16), oldB);
                            h = h + 16 + _space;
                            if (oldB != newB)
                            {
                                lat.Frames[_showFrameIndex].SetFrameValue(j, newB);
                            }
                            break;

                        case "Color":
                            Color oldC = (Color)lat.Frames[_showFrameIndex].GetFrameValue(j);
                            Color newC = EditorGUI.ColorField(new Rect(23, h, _width - 28, 16), oldC);
                            h = h + 16 + _space;
                            if (oldC != newC)
                            {
                                lat.Frames[_showFrameIndex].SetFrameValue(j, newC);
                            }
                            break;

                        case "Float":
                            float oldF = (float)lat.Frames[_showFrameIndex].GetFrameValue(j);
                            float newF = EditorGUI.FloatField(new Rect(23, h, _width - 28, 16), oldF);
                            h = h + 16 + _space;
                            if (oldF != newF)
                            {
                                lat.Frames[_showFrameIndex].SetFrameValue(j, newF);
                            }
                            break;

                        case "Int":
                            int oldI = (int)lat.Frames[_showFrameIndex].GetFrameValue(j);
                            int newI = EditorGUI.IntField(new Rect(23, h, _width - 28, 16), oldI);
                            h = h + 16 + _space;
                            if (oldI != newI)
                            {
                                lat.Frames[_showFrameIndex].SetFrameValue(j, newI);
                            }
                            break;

                        case "Quaternion":
                            Vector3 oldQ = ((Quaternion)lat.Frames[_showFrameIndex].GetFrameValue(j)).eulerAngles;
                            Vector3 newQ = EditorGUI.Vector3Field(new Rect(23, h, _width - 28, 16), "", oldQ);
                            h = h + 16 + _space;
                            if (oldQ != newQ)
                            {
                                lat.Frames[_showFrameIndex].SetFrameValue(j, Quaternion.Euler(newQ));
                            }
                            break;

                        case "String":
                            string oldS = (string)lat.Frames[_showFrameIndex].GetFrameValue(j);
                            string newS = EditorGUI.TextField(new Rect(23, h, _width - 28, 16), oldS);
                            h = h + 16 + _space;
                            if (oldS != newS)
                            {
                                lat.Frames[_showFrameIndex].SetFrameValue(j, newS);
                            }
                            break;

                        case "Vector2":
                            Vector2 oldV2 = (Vector2)lat.Frames[_showFrameIndex].GetFrameValue(j);
                            Vector2 newV2 = EditorGUI.Vector2Field(new Rect(23, h, _width - 28, 16), "", oldV2);
                            h = h + 16 + _space;
                            if (oldV2 != newV2)
                            {
                                lat.Frames[_showFrameIndex].SetFrameValue(j, newV2);
                            }
                            break;

                        case "Vector3":
                            Vector3 oldV3 = (Vector3)lat.Frames[_showFrameIndex].GetFrameValue(j);
                            Vector3 newV3 = EditorGUI.Vector3Field(new Rect(23, h, _width - 28, 16), "", oldV3);
                            h = h + 16 + _space;
                            if (oldV3 != newV3)
                            {
                                lat.Frames[_showFrameIndex].SetFrameValue(j, newV3);
                            }
                            break;

                        case "Vector4":
                            Vector4 oldV4 = (Vector4)lat.Frames[_showFrameIndex].GetFrameValue(j);
                            Vector4 newV4 = EditorGUI.Vector4Field(new Rect(23, h, _width - 28, 16), "", oldV4);
                            h = h + 16 + _space;
                            if (oldV4 != newV4)
                            {
                                lat.Frames[_showFrameIndex].SetFrameValue(j, newV4);
                            }
                            break;

                        case "Sprite":
                            Sprite oldSp = (Sprite)lat.Frames[_showFrameIndex].GetFrameValue(j);
                            Sprite newSp = EditorGUI.ObjectField(new Rect(23, h, _width - 28, 16), oldSp, typeof(Sprite), true) as Sprite;
                            h = h + 16 + _space;
                            if (oldSp != newSp)
                            {
                                lat.Frames[_showFrameIndex].SetFrameValue(j, newSp);
                            }
                            break;
                        }
                    }
                }
            }
            #endregion

            #region 添加属性
            if (lat.ShowPropertys)
            {
                if (GUI.Button(new Rect(5, h, _width - 10, 16), "Add Property"))
                {
                    GenericMenu gm  = new GenericMenu();
                    Component[] cps = lat.Target.GetComponents <Component>();
                    for (int m = 0; m < cps.Length; m++)
                    {
                        Type           type = cps[m].GetType();
                        PropertyInfo[] pis  = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
                        for (int n = 0; n < pis.Length; n++)
                        {
                            PropertyInfo pi           = pis[n];
                            string       propertyType = pi.PropertyType.Name;
                            propertyType = LinkageAnimationTool.ReplaceType(propertyType);
                            bool allow = LinkageAnimationTool.IsAllowType(propertyType);

                            if (allow)
                            {
                                if (pi.CanRead && pi.CanWrite)
                                {
                                    gm.AddItem(new GUIContent(type.Name + "/" + "[" + propertyType + "] " + pi.Name), false, delegate()
                                    {
                                        LAProperty lap = new LAProperty(type.Name, propertyType, pi.Name);
                                        AddProperty(lat, lap);
                                    });
                                }
                            }
                        }
                    }
                    gm.ShowAsContext();
                }
                h = h + 16 + _space;
            }
            lat.Height = h;
            #endregion

            GUI.EndGroup();
        }
    }