static void DoCreateSpriteObject()
    {
        tk2dSpriteCollectionData sprColl = null;

        if (sprColl == null)
        {
            // try to inherit from other Sprites in scene
            tk2dSprite spr = GameObject.FindObjectOfType(typeof(tk2dSprite)) as tk2dSprite;
            if (spr)
            {
                sprColl = spr.collection;
            }
        }

        if (sprColl == null)
        {
            tk2dSpriteCollectionData[] spriteCollections = tk2dEditorUtility.GetOrCreateIndex().GetSpriteCollectionData();
            foreach (var v in spriteCollections)
            {
                sprColl = v;
            }

            if (sprColl == null)
            {
                EditorUtility.DisplayDialog("Create Sprite", "Unable to create sprite as no SpriteCollections have been found.", "Ok");
                return;
            }
        }

        tk2dSpriteAnimation[] anims = tk2dEditorUtility.GetOrCreateIndex().GetSpriteAnimations();
        tk2dSpriteAnimation   anim  = null;

        foreach (var a in anims)
        {
            if (a != null && a.clips.Length > 0)
            {
                anim = a;
                break;
            }
        }
        if (anim == null)
        {
            EditorUtility.DisplayDialog("Create Animated Sprite", "Unable to create animated sprite as no SpriteAnimations have been found.", "Ok");
            return;
        }

        GameObject         go     = tk2dEditorUtility.CreateGameObjectInScene("AnimatedSprite");
        tk2dAnimatedSprite sprite = go.AddComponent <tk2dAnimatedSprite>();

        sprite.collection = anim.clips[0].frames[0].spriteCollection;
        sprite.Build();

        sprite.spriteId = anim.clips[0].frames[0].spriteId;
        sprite.anim     = anim;
    }
Example #2
0
        public void Setup(SpriteAnimData data)
        {
            _data   = data;
            _sprite = gameObject.AddComponent <tk2dAnimatedSprite>();
            _sprite.SwitchCollectionAndSprite(data.data, 0);
            _sprite.anim = _data.anim;
            _sprite.Build();

            _dimensionsInit = new Vector3(data.regionWidth, data.regionHeight, 1f);
            dimensions      = new Vector3(data.width, data.height, 1f);
        }
    static void DoCreateSpriteObject()
    {
        tk2dSpriteCollectionData sprColl = null;

        if (sprColl == null)
        {
            // try to inherit from other Sprites in scene
            tk2dSprite spr = GameObject.FindObjectOfType(typeof(tk2dSprite)) as tk2dSprite;
            if (spr)
            {
                sprColl = spr.collection;
            }
        }

        if (sprColl == null)
        {
            tk2dSpriteCollectionIndex[] spriteCollections = tk2dEditorUtility.GetOrCreateIndex().GetSpriteCollectionIndex();
            foreach (var v in spriteCollections)
            {
                GameObject scgo = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(v.spriteCollectionDataGUID), typeof(GameObject)) as GameObject;
                var        sc   = scgo.GetComponent <tk2dSpriteCollectionData>();
                if (sc != null && sc.spriteDefinitions != null && sc.spriteDefinitions.Length > 0)
                {
                    sprColl = sc;
                    break;
                }
            }

            if (sprColl == null)
            {
                EditorUtility.DisplayDialog("Create Sprite", "Unable to create sprite as no SpriteCollections have been found.", "Ok");
                return;
            }
        }

        tk2dSpriteAnimation[] anims = tk2dEditorUtility.GetOrCreateIndex().GetSpriteAnimations();
        tk2dSpriteAnimation   anim  = null;

        foreach (var a in anims)
        {
            if (a != null && a.clips != null && a.clips.Length > 0)
            {
                anim = a;
                break;
            }
        }

        if (anim == null)
        {
            EditorUtility.DisplayDialog("Create Animated Sprite", "Unable to create animated sprite as no SpriteAnimations have been found.", "Ok");
            return;
        }

        if (anim.clips[0].frames.Length == 0 || anim.clips[0].frames[0].spriteCollection == null)
        {
            EditorUtility.DisplayDialog("Create Animated Sprite", "Invalid SpriteAnimation has been found.", "Ok");
            return;
        }

        GameObject         go     = tk2dEditorUtility.CreateGameObjectInScene("AnimatedSprite");
        tk2dAnimatedSprite sprite = go.AddComponent <tk2dAnimatedSprite>();

        sprite.collection = anim.clips[0].frames[0].spriteCollection;
        sprite.Build();

        sprite.spriteId = anim.clips[0].frames[0].spriteId;
        sprite.anim     = anim;
    }
Example #4
0
    static void DoCreateSpriteObject()
    {
        tk2dSpriteCollectionData sprColl = null;

        if (sprColl == null)
        {
            // try to inherit from other Sprites in scene
            tk2dSprite spr = GameObject.FindObjectOfType(typeof(tk2dSprite)) as tk2dSprite;
            if (spr)
            {
                sprColl = spr.Collection;
            }
        }

        if (sprColl == null)
        {
            tk2dSpriteCollectionIndex[] spriteCollections = tk2dEditorUtility.GetOrCreateIndex().GetSpriteCollectionIndex();
            foreach (var v in spriteCollections)
            {
                GameObject scgo = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(v.spriteCollectionDataGUID), typeof(GameObject)) as GameObject;
                var        sc   = scgo.GetComponent <tk2dSpriteCollectionData>();
                if (sc != null && sc.spriteDefinitions != null && sc.spriteDefinitions.Length > 0)
                {
                    sprColl = sc;
                    break;
                }
            }

            if (sprColl == null)
            {
                EditorUtility.DisplayDialog("Create Sprite", "Unable to create sprite as no SpriteCollections have been found.", "Ok");
                return;
            }
        }

        tk2dGenericIndexItem[] animIndex = tk2dEditorUtility.GetOrCreateIndex().GetSpriteAnimations();
        tk2dSpriteAnimation    anim      = null;
        int clipId = -1;

        foreach (var animIndexItem in animIndex)
        {
            tk2dSpriteAnimation a = animIndexItem.GetAsset <tk2dSpriteAnimation>();
            if (a != null && a.clips != null && a.clips.Length > 0)
            {
                for (int i = 0; i < a.clips.Length; ++i)
                {
                    if (!a.clips[i].Empty &&
                        a.clips[i].frames[0].spriteCollection != null &&
                        a.clips[i].frames[0].spriteId >= 0)
                    {
                        clipId = i;
                        break;
                    }
                }

                if (clipId != -1)
                {
                    anim = a;
                    break;
                }
            }
        }

        if (anim == null || clipId == -1)
        {
            EditorUtility.DisplayDialog("Create Animated Sprite", "Unable to create animated sprite as no SpriteAnimations have been found.", "Ok");
            return;
        }

        GameObject         go     = tk2dEditorUtility.CreateGameObjectInScene("AnimatedSprite");
        tk2dAnimatedSprite sprite = go.AddComponent <tk2dAnimatedSprite>();

        sprite.SwitchCollectionAndSprite(anim.clips[clipId].frames[0].spriteCollection, anim.clips[clipId].frames[0].spriteId);
        sprite.anim   = anim;
        sprite.clipId = clipId;
        sprite.Build();

        Selection.activeGameObject = go;
        Undo.RegisterCreatedObjectUndo(go, "Create AnimatedSprite");
    }