public override void OnInspectorGUI()
    {
        spriteUiVisible = EditorGUILayout.Foldout(spriteUiVisible, "Sprite");
        if (spriteUiVisible)
        {
            base.OnInspectorGUI();
        }

        Init();
        if (animLibs == null)
        {
            GUILayout.Label("no libraries found");
            if (GUILayout.Button("Refresh"))
            {
                initialized = false;
                Init();
            }
        }
        else
        {
            tk2dAnimatedSprite sprite = (tk2dAnimatedSprite)target;

            EditorGUIUtility.LookLikeInspector();
            EditorGUI.indentLevel = 1;

            if (sprite.anim == null)
            {
                sprite.anim = animLibs[0];
                GUI.changed = true;
            }

            // Display animation library
            int selAnimLib = 0;
            for (int i = 0; i < animLibs.Length; ++i)
            {
                if (animLibs[i] == sprite.anim)
                {
                    selAnimLib = i;
                    break;
                }
            }

            int newAnimLib = EditorGUILayout.Popup("Anim Lib", selAnimLib, animLibNames);
            if (newAnimLib != selAnimLib)
            {
                sprite.anim   = animLibs[newAnimLib];
                sprite.clipId = 0;

                if (sprite.anim.clips.Length > 0)
                {
                    // automatically switch to the first frame of the new clip
                    sprite.SwitchCollectionAndSprite(sprite.anim.clips[sprite.clipId].frames[0].spriteCollection,
                                                     sprite.anim.clips[sprite.clipId].frames[0].spriteId);
                }
            }

            // Everything else
            if (sprite.anim && sprite.anim.clips.Length > 0)
            {
                int clipId = sprite.clipId;

                // Sanity check clip id
                clipId = Mathf.Clamp(clipId, 0, sprite.anim.clips.Length - 1);
                if (clipId != sprite.clipId)
                {
                    sprite.clipId = clipId;
                    GUI.changed   = true;
                }

                string[] clipNames = new string[sprite.anim.clips.Length];
                for (int i = 0; i < sprite.anim.clips.Length; ++i)
                {
                    clipNames[i] = sprite.anim.clips[i].name;
                }
                int newClipId = EditorGUILayout.Popup("Clip", sprite.clipId, clipNames);
                if (newClipId != sprite.clipId)
                {
                    sprite.clipId = newClipId;
                    // automatically switch to the first frame of the new clip
                    sprite.SwitchCollectionAndSprite(sprite.anim.clips[sprite.clipId].frames[0].spriteCollection,
                                                     sprite.anim.clips[sprite.clipId].frames[0].spriteId);
                }
            }

            // Play automatically
            sprite.playAutomatically = EditorGUILayout.Toggle("Play automatically", sprite.playAutomatically);
            bool oldCreateCollider = sprite.createCollider;
            sprite.createCollider = EditorGUILayout.Toggle("Create collider", sprite.createCollider);
            if (oldCreateCollider != sprite.createCollider)
            {
                sprite.EditMode__CreateCollider();
            }

            if (GUI.changed)
            {
                EditorUtility.SetDirty(sprite);
            }
        }
    }
Exemple #2
0
    public override void OnInspectorGUI()
    {
        spriteUiVisible = EditorGUILayout.Foldout(spriteUiVisible, "Sprite");
        if (spriteUiVisible)
        {
            base.OnInspectorGUI();
        }

        Init();
        if (animLibs == null)
        {
            GUILayout.Label("no libraries found");
            if (GUILayout.Button("Refresh"))
            {
                initialized = false;
                Init();
            }
        }
        else
        {
            tk2dAnimatedSprite sprite = (tk2dAnimatedSprite)target;

            // NOTE ppoirier: Deprecated.
            //EditorGUIUtility.LookLikeInspector();
            EditorGUI.indentLevel = 1;

            if (sprite.anim == null)
            {
                sprite.anim = animLibs[0].GetAsset <tk2dSpriteAnimation>();
                GUI.changed = true;
            }

            // Display animation library
            int    selAnimLib   = 0;
            string selectedGUID = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(sprite.anim));
            for (int i = 0; i < animLibs.Length; ++i)
            {
                if (animLibs[i].assetGUID == selectedGUID)
                {
                    selAnimLib = i;
                    break;
                }
            }

            int newAnimLib = EditorGUILayout.Popup("Anim Lib", selAnimLib, animLibNames);
            if (newAnimLib != selAnimLib)
            {
                sprite.anim   = animLibs[newAnimLib].GetAsset <tk2dSpriteAnimation>();
                sprite.clipId = 0;

                if (sprite.anim.clips.Length > 0)
                {
                    // automatically switch to the first frame of the new clip
                    sprite.SwitchCollectionAndSprite(sprite.anim.clips[sprite.clipId].frames[0].spriteCollection,
                                                     sprite.anim.clips[sprite.clipId].frames[0].spriteId);
                }
            }

            // Everything else
            if (sprite.anim && sprite.anim.clips.Length > 0)
            {
                int clipId = sprite.clipId;

                // Sanity check clip id
                clipId = Mathf.Clamp(clipId, 0, sprite.anim.clips.Length - 1);
                if (clipId != sprite.clipId)
                {
                    sprite.clipId = clipId;
                    GUI.changed   = true;
                }

                string[] clipNames = new string[sprite.anim.clips.Length];
                // fill names (with ids if necessary)
                if (tk2dPreferences.inst.showIds)
                {
                    for (int i = 0; i < sprite.anim.clips.Length; ++i)
                    {
                        if (sprite.anim.clips[i].name != null && sprite.anim.clips[i].name.Length > 0)
                        {
                            clipNames[i] = sprite.anim.clips[i].name + "\t[" + i.ToString() + "]";
                        }
                        else
                        {
                            clipNames[i] = sprite.anim.clips[i].name;
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < sprite.anim.clips.Length; ++i)
                    {
                        clipNames[i] = sprite.anim.clips[i].name;
                    }
                }

                int newClipId = EditorGUILayout.Popup("Clip", sprite.clipId, clipNames);
                if (newClipId != sprite.clipId)
                {
                    sprite.clipId = newClipId;
                    // automatically switch to the first frame of the new clip
                    sprite.SwitchCollectionAndSprite(sprite.anim.clips[sprite.clipId].frames[0].spriteCollection,
                                                     sprite.anim.clips[sprite.clipId].frames[0].spriteId);
                }
            }

            // Play automatically
            sprite.playAutomatically = EditorGUILayout.Toggle("Play automatically", sprite.playAutomatically);
            bool oldCreateCollider = sprite.createCollider;
            sprite.createCollider = EditorGUILayout.Toggle("Create collider", sprite.createCollider);
            if (oldCreateCollider != sprite.createCollider)
            {
                sprite.EditMode__CreateCollider();
            }
            sprite.m_DontUpdateMaterial = EditorGUILayout.Toggle("Don't Update Material", sprite.m_DontUpdateMaterial);

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