public void configureObject(tk2dSprite sprite, string resGroup, string spriteName)
    {
        if (!_spriteCollections.ContainsKey(resGroup))
            loadSpriteCollection(resGroup);

        sprite.SwitchCollectionAndSprite(_spriteCollections[resGroup].spriteCollection, _spriteCollections[resGroup].spriteCollection.GetSpriteIdByName(spriteName));
    }
Beispiel #2
0
    public void configureObject(tk2dSprite sprite, string resGroup, string spriteName)
    {
        if (!_spriteCollections.ContainsKey(resGroup))
        {
            loadSpriteCollection(resGroup);
        }

        sprite.SwitchCollectionAndSprite(_spriteCollections[resGroup].spriteCollection, _spriteCollections[resGroup].spriteCollection.GetSpriteIdByName(spriteName));
    }
Beispiel #3
0
    public override void Generate()
    {
        // Grabs Ship information
        int   id         = gameObject.transform.parent.GetComponent <Ship>().GetID();
        int   Size       = gameObject.transform.parent.GetComponent <Ship>().GetSize();
        float ShipHeight = 0;

        GenerateColor();
        for (int index = 0; index < Size; index++)
        {
            // Add gameobject and sprite component
            GameObject temp = new GameObject();
            temp.name             = index.ToString();
            temp.transform.parent = gameObject.transform;
            temp.AddComponent <tk2dSprite>();
            tk2dSprite tempSprite = temp.GetComponent <tk2dSprite>();

            // Find Suitable ID
            id = Utility.Wrap <int>(id, 0, collection.spriteCollection.spriteDefinitions.Length - 1);
            while (collection.spriteCollection.spriteDefinitions[id].name == "")
            {
                id += (int)Random.Range(0, 4);
                id  = Utility.Wrap <int>(id, 0, collection.spriteCollection.Count - 1);
            }

            // Create Sprite
            tempSprite.SwitchCollectionAndSprite(collection.spriteCollection, id);
            tempSprite.Build();


            // Choose Location to position new Sprite
            // Based on the progress of the height of the ship, place a
            // sprite just above the previously placed midsection sprite.
            float randomNumber = 0;
            float heightSpace  = 0;
            if (ShipHeight != 0)
            {
                heightSpace  = ShipHeight + tempSprite.GetBounds().size.y;
                randomNumber = Random.Range(heightSpace - heightSpace / 5, heightSpace - heightSpace / 9);
                ShipHeight  += randomNumber - ShipHeight;
            }
            else
            {
                ShipHeight  += tempSprite.GetBounds().size.y;
                randomNumber = Random.Range(ShipHeight / 5, Mathf.Abs(ShipHeight - ShipHeight / 9));

                temp.transform.localPosition = new Vector3(0, .1f, .025f + index * .025f);
                id += (int)Random.Range(0, 4);
                temp.renderer.material.SetColor("_Color", color);
                continue;
            }
            temp.transform.localPosition += new Vector3(0, randomNumber, .025f + index * .025f);
            id += (int)Random.Range(0, 4);
            temp.renderer.material.SetColor("_Color", color);
        }
        DuplicateToOtherHalf();
    }
Beispiel #4
0
        public void Setup(SpriteFlatData data)
        {
            _data   = data;
            _sprite = gameObject.AddComponent <tk2dSprite>();
            _sprite.SwitchCollectionAndSprite(data.data, 0);
            _sprite.Build();

            _dimensionsInit = new Vector3(data.regionWidth, data.regionHeight, 1f);
            dimensions      = new Vector3(data.width, data.height, 1f);
        }
Beispiel #5
0
        public void Setup(SpriteFlatData data)
        {
            _data = data;
            _sprite = gameObject.AddComponent<tk2dSprite>();
            _sprite.SwitchCollectionAndSprite(data.data, 0);
            _sprite.Build();

            _dimensionsInit = new Vector3(data.regionWidth, data.regionHeight, 1f);
            dimensions = new Vector3(data.width, data.height, 1f);
        }
Beispiel #6
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 && !sc.managedSpriteCollection)
                {
                    sprColl = sc;
                    break;
                }
            }

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

        GameObject go     = tk2dEditorUtility.CreateGameObjectInScene("Sprite");
        tk2dSprite sprite = go.AddComponent <tk2dSprite>();

        sprite.SwitchCollectionAndSprite(sprColl, sprColl.FirstValidDefinitionIndex);
        sprite.renderer.material = sprColl.FirstValidDefinition.material;
        sprite.Build();

        Selection.activeGameObject = go;
        Undo.RegisterCreatedObjectUndo(go, "Create Sprite");
    }
Beispiel #7
0
    public override void Generate()
    {
        // Grabs Ship information
        int id              = gameObject.transform.parent.GetComponent <Ship>().GetID();
        int Size            = gameObject.transform.parent.GetComponent <Ship>().GetSize();
        int extentsionCount = (int)Random.Range(1, Size);

        GenerateColor();
        for (int index = 0; index < extentsionCount; index++)
        {
            // Add gameobject and sprite component
            GameObject temp = new GameObject();
            temp.name             = index.ToString();
            temp.transform.parent = gameObject.transform;
            temp.AddComponent <tk2dSprite>();
            tk2dSprite tempSprite = temp.GetComponent <tk2dSprite>();

            // Find Suitable ID
            id = Utility.Wrap <int>(id, 0, collection.spriteCollection.spriteDefinitions.Length - 1);
            while (collection.spriteCollection.spriteDefinitions[id].name == "")
            {
                id += (int)Random.Range(0, 4);
                id  = Utility.Wrap <int>(id, 0, collection.spriteCollection.Count - 1);
            }

            // Create Sprite
            tempSprite.SwitchCollectionAndSprite(collection.spriteCollection, id);
            tempSprite.Build();


            // Choose Location to position new Sprite
            // Place in the middle of selected Last Midsection or Extension
            string placement = (Random.Range(0f, 1f) > .5f) ? "Extension" : "MidSection";

            int       count      = gameObject.transform.parent.FindChild(placement).GetChildCount();
            Transform midSection = gameObject.transform.parent.FindChild(placement).GetChild(count - 1);
            Vector3   spacing    = midSection.GetComponent <tk2dSprite>().GetBounds().size / 3;
            spacing.y = -spacing.y;
            spacing.z = .05f;
            temp.transform.localPosition = midSection.position + spacing;
            temp.renderer.material.SetColor("_Color", color);
        }
        DuplicateToOtherHalf();
    }
    protected void DrawSpriteEditorGUI(tk2dSprite sprite)
    {
        // maybe cache this if its too slow later
        if (generatorCache.all == null || generatorCache.current != sprite.collection)
        {
            generatorCache.all = tk2dEditorUtility.GetOrCreateIndex().GetSpriteCollectionIndex();
            if (generatorCache.all != null)
            {
                string guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(sprite.collection));

                for (int i = 0; i < generatorCache.all.Length; ++i)
                {
                    if (generatorCache.all[i].spriteCollectionDataGUID == guid)
                    {
                        generatorCache.current = sprite.collection;
                        generatorCache.currentGUID = guid;
                        break;
                    }
                }
            }
        }

        if (generatorCache.all == null)
        {
            EditorGUILayout.LabelField("Collection", "Error");
        }
        else
        {
            string[] collNames = new string[generatorCache.all.Length];
            int selIndex = -1;
            for (int i = 0; i < generatorCache.all.Length; ++i)
            {
                collNames[i] = generatorCache.all[i].name;
                if (generatorCache.all[i].spriteCollectionDataGUID == generatorCache.currentGUID)
                    selIndex = i;
            }

            int newIndex = EditorGUILayout.Popup("Collection", (selIndex != -1) ? selIndex : 0, collNames);
            if (newIndex != selIndex)
            {
                generatorCache.currentGUID = generatorCache.all[newIndex].spriteCollectionDataGUID;
                GameObject go = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(generatorCache.currentGUID), typeof(GameObject)) as GameObject;
                tk2dSpriteCollectionData data = go.GetComponent<tk2dSpriteCollectionData>();
                if (data != null)
                {
                    generatorCache.current =  data;
                    int newId = (sprite.spriteId >= generatorCache.current.Count)?0:sprite.spriteId;

                    sprite.SwitchCollectionAndSprite(generatorCache.current, newId);
                    sprite.EditMode__CreateCollider();
                }
            }
        }

        if (sprite.collection)
        {
            // sanity check sprite id
            if (sprite.spriteId < 0 || sprite.spriteId >= sprite.collection.Count)
            {
                sprite.spriteId = 0;
                sprite.EditMode__CreateCollider();
            }

            int newSpriteId = sprite.spriteId;

            if (generatorCache.current)
            {
                newSpriteId = tk2dEditorUtility.SpriteSelectorPopup("Sprite", sprite.spriteId, generatorCache.current);

                if (tk2dPreferences.inst.displayTextureThumbs)
                {
                    if (generatorCache.current.version < 1)
                    {
                        GUILayout.Label("No thumbnail data.\nPlease rebuild Sprite Collection.");
                    }
                    else
                    {
                        var tex = tk2dSpriteThumbnailCache.GetThumbnailTexture(generatorCache.current, sprite.spriteId);
                        if (tex)
                        {
                            float w = tex.width;
                            float h = tex.height;
                            float maxSize = 128.0f;
                            if (w > maxSize)
                            {
                                h = h / w * maxSize;
                                w = maxSize;
                            }

                            Rect r = GUILayoutUtility.GetRect(w, h);
                            GUI.DrawTexture(r, tex, ScaleMode.ScaleToFit);
                            //GUILayout.Box(tex, GUILayout.Width(w), GUILayout.Height(h));
                        }
                    }
                }
            }
            else
            {
                newSpriteId = EditorGUILayout.IntSlider(sprite.spriteId, 0, sprite.collection.Count - 1);
            }

            if (newSpriteId != sprite.spriteId)
            {
                sprite.spriteId = newSpriteId;
                sprite.EditMode__CreateCollider();
                GUI.changed = true;
            }

            sprite.color = EditorGUILayout.ColorField("Color", sprite.color);
            Vector3 newScale = EditorGUILayout.Vector3Field("Scale", sprite.scale);
            if (newScale != sprite.scale)
            {
                sprite.scale = newScale;
                sprite.EditMode__CreateCollider();
            }

            EditorGUILayout.BeginHorizontal();

            if (GUILayout.Button("HFlip"))
            {
                Vector3 s = sprite.scale;
                s.x *= -1.0f;
                sprite.scale = s;
                GUI.changed = true;
            }
            if (GUILayout.Button("VFlip"))
            {
                Vector3 s = sprite.scale;
                s.y *= -1.0f;
                sprite.scale = s;
                GUI.changed = true;
            }
            if (GUILayout.Button("Reset Scale" ))
            {
                Vector3 s = sprite.scale;
                s.x = Mathf.Sign(s.x);
                s.y = Mathf.Sign(s.y);
                s.z = Mathf.Sign(s.z);
                sprite.scale = s;
                GUI.changed = true;
            }

            if ( GUILayout.Button("Make Pixel Perfect", GUILayout.ExpandWidth(true) ))
            {
                if (tk2dPixelPerfectHelper.inst) tk2dPixelPerfectHelper.inst.Setup();
                sprite.MakePixelPerfect();
                GUI.changed = true;
            }

            sprite.pixelPerfect = GUILayout.Toggle(sprite.pixelPerfect, "Always", GUILayout.Width(60.0f));
            EditorGUILayout.EndHorizontal();
        }
        else
        {
            EditorGUILayout.IntSlider("Need a collection bound", 0, 0, 1);
        }

        if (GUI.changed)
            EditorUtility.SetDirty(sprite);
    }
Beispiel #9
0
    protected void DrawSpriteEditorGUI(tk2dSprite sprite)
    {
        // maybe cache this if its too slow later
        if (generatorCache.all == null || generatorCache.current != sprite.collection)
        {
            generatorCache.all = tk2dEditorUtility.GetOrCreateIndex().GetSpriteCollectionData();
            if (generatorCache.all != null)
            {
                for (int i = 0; i < generatorCache.all.Length; ++i)
                {
                    if (generatorCache.all[i] == sprite.collection)
                    {
                        generatorCache.current = generatorCache.all[i];
                        break;
                    }
                }
            }
        }

        if (generatorCache.all == null)
        {
            EditorGUILayout.LabelField("Collection", "Error");
        }
        else
        {
            string[] collNames = new string[generatorCache.all.Length];
            int      selIndex  = -1;
            for (int i = 0; i < generatorCache.all.Length; ++i)
            {
                collNames[i] = generatorCache.all[i].spriteCollectionName;
                if (generatorCache.all[i] == generatorCache.current)
                {
                    selIndex = i;
                }
            }

            int newIndex = EditorGUILayout.Popup("Collection", (selIndex != -1) ? selIndex : 0, collNames);
            if (newIndex != selIndex)
            {
                generatorCache.current = generatorCache.all[newIndex];
                int newId = (sprite.spriteId >= generatorCache.current.Count)?0:sprite.spriteId;
                sprite.SwitchCollectionAndSprite(generatorCache.current, newId);
            }
        }

        if (sprite.collection)
        {
            // sanity check sprite id
            if (sprite.spriteId < 0 || sprite.spriteId >= sprite.collection.Count)
            {
                sprite.spriteId = 0;
            }

            int newSpriteId = sprite.spriteId;

            if (generatorCache.current)
            {
                string[] spriteNames = new string[generatorCache.current.spriteDefinitions.Length];
                for (int i = 0; i < generatorCache.current.spriteDefinitions.Length; ++i)
                {
                    spriteNames[i] = generatorCache.current.spriteDefinitions[i].name;
                }

                newSpriteId = EditorGUILayout.Popup("Sprite", sprite.spriteId, spriteNames);

                if (generatorCache.current.version < 1)
                {
                    GUILayout.Label("No thumbnail data.\nPlease rebuild Sprite Collection.");
                }
                else
                {
                    var tex = tk2dSpriteThumbnailCache.GetThumbnailTexture(generatorCache.current, sprite.spriteId);
                    if (tex)
                    {
                        float w       = tex.width;
                        float h       = tex.height;
                        float maxSize = 128.0f;
                        if (w > maxSize)
                        {
                            h = h / w * maxSize;
                            w = maxSize;
                        }

                        Rect r = GUILayoutUtility.GetRect(w, h);
                        GUI.DrawTexture(r, tex, ScaleMode.ScaleToFit);
                        //GUILayout.Box(tex, GUILayout.Width(w), GUILayout.Height(h));
                    }
                }
            }
            else
            {
                newSpriteId = EditorGUILayout.IntSlider(sprite.spriteId, 0, sprite.collection.Count - 1);
            }

            if (newSpriteId != sprite.spriteId)
            {
                sprite.spriteId = newSpriteId;
                GUI.changed     = true;
            }

            sprite.color = EditorGUILayout.ColorField("Color", sprite.color);
            sprite.scale = EditorGUILayout.Vector3Field("Scale", sprite.scale);

            EditorGUILayout.BeginHorizontal();

            if (GUILayout.Button("HFlip"))
            {
                Vector3 s = sprite.scale;
                s.x         *= -1.0f;
                sprite.scale = s;
                GUI.changed  = true;
            }
            if (GUILayout.Button("VFlip"))
            {
                Vector3 s = sprite.scale;
                s.y         *= -1.0f;
                sprite.scale = s;
                GUI.changed  = true;
            }
            if (GUILayout.Button("Reset Scale"))
            {
                Vector3 s = sprite.scale;
                s.x          = Mathf.Sign(s.x);
                s.y          = Mathf.Sign(s.y);
                s.z          = Mathf.Sign(s.z);
                sprite.scale = s;
                GUI.changed  = true;
            }

            if (GUILayout.Button("Make Pixel Perfect", GUILayout.ExpandWidth(true)))
            {
                if (tk2dPixelPerfectHelper.inst)
                {
                    tk2dPixelPerfectHelper.inst.Setup();
                }
                sprite.MakePixelPerfect();
                GUI.changed = true;
            }

            sprite.pixelPerfect = GUILayout.Toggle(sprite.pixelPerfect, "Always", GUILayout.Width(60.0f));
            EditorGUILayout.EndHorizontal();
        }
        else
        {
            EditorGUILayout.IntSlider("Need a collection bound", 0, 0, 1);
        }

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