public override void OnInspectorGUI()
        {
            CharAnimationController targetComp = (CharAnimationController)target;

            if (GUILayout.Button("Open Editor..."))
            {
                CharAnimationWindow.Init(targetComp);
            }

            if (targetComp.IsDataBroken())
            {
                targetComp.CreateSpriteFrames();
            }

            EditorGUI.BeginChangeCheck();
            targetComp.SpriteCharSet = (Sprite)EditorGUILayout.ObjectField("SpriteCharSet", targetComp.SpriteCharSet, typeof(Sprite), false);
            targetComp.CharsetType   = (CharAnimationController.eCharSetType)EditorGUILayout.EnumPopup("Charset Type", targetComp.CharsetType);
            if (EditorGUI.EndChangeCheck())
            {
                targetComp.CreateSpriteFrames();
            }

            targetComp.TargetSpriteRenderer = (SpriteRenderer)EditorGUILayout.ObjectField("Target Sprite Render", targetComp.TargetSpriteRenderer, typeof(SpriteRenderer), true);
            targetComp.AnimSpeed            = EditorGUILayout.FloatField("Anim Speed", targetComp.AnimSpeed);
            targetComp.IsPingPongAnim       = EditorGUILayout.ToggleLeft("Ping-Pong Anim", targetComp.IsPingPongAnim);
            targetComp.CurrentDir           = (CharAnimationController.eDir)EditorGUILayout.EnumPopup("Facing Dir", targetComp.CurrentDir);
            EditorGUILayout.PropertyField(serializedObject.FindProperty("IsAnimated"));
            EditorGUILayout.PropertyField(serializedObject.FindProperty("PixelsPerUnit"));

            serializedObject.ApplyModifiedProperties();
            if (GUI.changed)
            {
                EditorUtility.SetDirty(targetComp);
            }
        }
Exemplo n.º 2
0
        void OnGUI()
        {
            // Change m_animCtrl if another object is selected with this component attached
            if (m_prevSelectedGameObject != Selection.activeGameObject)
            {
                CharAnimationController animCtrl = Selection.activeGameObject != null?Selection.activeGameObject.GetComponent <CharAnimationController>() : null;

                if (animCtrl != null)
                {
                    m_animCtrl = animCtrl;
                    if (animCtrl.CreateSpriteFrames())
                    {
                        m_gridSize = animCtrl.GetSpriteFrames()[0].rect.width / 4;
                    }
                }
            }
            m_prevSelectedGameObject = Selection.activeGameObject;

            if (m_animCtrl == null)
            {
                Close();
                return;
            }

            if (m_animCtrl.IsDataBroken())
            {
                m_animCtrl.CreateSpriteFrames();
            }

            if (s_gridTex == null)
            {
                s_gridTex            = new Texture2D(2, 2);
                s_gridTex.wrapMode   = TextureWrapMode.Repeat;
                s_gridTex.filterMode = FilterMode.Point;
                s_gridTex.SetPixels32(new Color32[] { m_gridColor0, m_gridColor1, m_gridColor1, m_gridColor0 });
                s_gridTex.Apply();
            }

            Repaint();

            if (Event.current.type == EventType.Repaint)
            {
                float timeDt = Time.realtimeSinceStartup - m_prevDt;
                m_prevDt = Time.realtimeSinceStartup;

                if (!Application.isPlaying)
                {
                    m_animCtrl.UpdateAnim(timeDt);
                }

                m_walkAnimTimer -= timeDt * m_walkSpeed;
                while (m_walkAnimTimer <= 0f)
                {
                    m_walkAnimTimer += 1f;
                }
                m_walkAnimTimer %= 1;
            }
            else if (Event.current.type == EventType.scrollWheel && m_rGridView.Contains(m_mousePos))
            {
                float prevZoom = m_zoom;
                if (Event.current.delta.y > 0) // back
                {
                    if (m_zoom > 1f)
                    {
                        m_zoom = Mathf.Max(m_zoom - 1, 1);
                    }
                    else
                    {
                        m_zoom = Mathf.Max(m_zoom / 2f, 0.05f);
                    }
                }
                else if (Event.current.delta.y < 0) // forward
                {
                    if (m_zoom >= 1f)
                    {
                        m_zoom = Mathf.Min(m_zoom + 1, 10);
                    }
                    else
                    {
                        m_zoom *= 2f;
                    }
                }
                m_zoom = Mathf.Clamp(m_zoom, 1f, 5f);

                if (prevZoom != m_zoom)
                {
                    Vector2 vZoomCenter = (m_mousePos - m_rGridView.position);
                    m_vTrans += (prevZoom - m_zoom) * vZoomCenter / (prevZoom * m_zoom);
                }
            }
            else if (Event.current.type == EventType.MouseDrag && m_rGridView.Contains(m_mousePos))
            {
                m_vTrans += Event.current.delta / m_zoom;
            }

            m_mousePos = Event.current.mousePosition;

            DrawGUI();

            if (GUI.changed)
            {
                EditorUtility.SetDirty(m_animCtrl);
            }
        }