コード例 #1
0
    void DrawInstanceGUI()
    {
        if (GUILayout.Button("Edit"))
        {
            foreach (var v in batcher.batchedSprites)
            {
                GameObject go = new GameObject(v.name);
                go.transform.parent        = batcher.transform;
                go.transform.localPosition = v.position;
                go.transform.localRotation = v.rotation;

                tk2dSprite s = go.AddComponent <tk2dSprite>();
                s.collection = batcher.spriteCollection;
                s.Build();

                s.spriteId = v.spriteId;
                s.EditMode__CreateCollider();                 // needed to recreate the collider after setting spriteId

                s.scale        = v.localScale;
                s.pixelPerfect = v.alwaysPixelPerfect;
                s.color        = v.color;
            }

            batcher.transform.localScale = batcher.scale;

            batcher.batchedSprites = null;
            batcher.Build();
            EditorUtility.SetDirty(target);
        }

        batcher.scale = EditorGUILayout.Vector3Field("Scale", batcher.scale);
    }
コード例 #2
0
    public override void OnInspectorGUI()
    {
        tk2dStaticSpriteBatcher batcher = (tk2dStaticSpriteBatcher)target;

        if (batcher.batchedSprites == null || batcher.batchedSprites.Length == 0)
        {
            if (GUILayout.Button("Commit"))
            {
                List <tk2dSprite>        sprites = new List <tk2dSprite>();
                tk2dSpriteCollectionData scd     = null;

                for (int i = 0; i < batcher.transform.childCount; ++i)
                {
                    Transform  t = batcher.transform.GetChild(i);
                    tk2dSprite s = t.GetComponent <tk2dSprite>();
                    if (s)
                    {
                        if (scd == null)
                        {
                            scd = s.collection;
                        }
                        if (scd != s.collection)
                        {
                            EditorUtility.DisplayDialog("StaticSpriteBatcher", "Error: Multiple sprite collections found", "Ok");
                            return;
                        }

                        if (scd.allowMultipleAtlases)
                        {
                            EditorUtility.DisplayDialog("StaticSpriteBatcher", "Error: Sprite collections with multiple atlases not allowed", "Ok");
                            return;
                        }

                        sprites.Add(s);
                    }
                }

                // sort sprites, smaller to larger z
                sprites.Sort((a, b) => b.transform.localPosition.z.CompareTo(a.transform.localPosition.z));

                batcher.spriteCollection = scd;
                batcher.batchedSprites   = new tk2dBatchedSprite[sprites.Count];
                int currBatchedSprite = 0;
                foreach (var s in sprites)
                {
                    tk2dBatchedSprite bs = new tk2dBatchedSprite();

                    bs.name               = s.gameObject.name;
                    bs.color              = s.color;
                    bs.localScale         = s.scale;
                    bs.position           = s.transform.localPosition;
                    bs.rotation           = s.transform.localRotation;
                    bs.spriteId           = s.spriteId;
                    bs.alwaysPixelPerfect = s.pixelPerfect;

                    batcher.batchedSprites[currBatchedSprite++] = bs;

                    GameObject.DestroyImmediate(s.gameObject);
                }

                batcher.Build();
                EditorUtility.SetDirty(target);
            }
        }
        else
        {
            if (GUILayout.Button("Edit"))
            {
                foreach (var v in batcher.batchedSprites)
                {
                    GameObject go = new GameObject(v.name);
                    go.transform.parent        = batcher.transform;
                    go.transform.localPosition = v.position;
                    go.transform.localRotation = v.rotation;

                    tk2dSprite s = go.AddComponent <tk2dSprite>();
                    s.collection = batcher.spriteCollection;
                    s.Build();

                    s.spriteId = v.spriteId;
                    s.EditMode__CreateCollider();                     // needed to recreate the collider after setting spriteId

                    s.scale        = v.localScale;
                    s.pixelPerfect = v.alwaysPixelPerfect;
                    s.color        = v.color;
                }

                batcher.batchedSprites = null;
                batcher.Build();
                EditorUtility.SetDirty(target);
            }
        }
    }
コード例 #3
0
    void DrawInstanceGUI()
    {
        if (GUILayout.Button("Edit"))
        {
            Vector3    batcherPos      = batcher.transform.position;
            Quaternion batcherRotation = batcher.transform.rotation;
            batcher.transform.position = Vector3.zero;
            batcher.transform.rotation = Quaternion.identity;

            Dictionary <int, Transform> parents  = new Dictionary <int, Transform>();
            List <Transform>            children = new List <Transform>();

            int id = 0;
            foreach (var v in batcher.batchedSprites)
            {
                GameObject go = new GameObject(v.name);

                go.transform.localPosition = v.position;
                go.transform.localRotation = v.rotation;

                if (v.spriteId != -1)
                {
                    tk2dSprite s = go.AddComponent <tk2dSprite>();
                    s.collection = batcher.spriteCollection;
                    s.Build();

                    s.spriteId = v.spriteId;
                    s.EditMode__CreateCollider();                     // needed to recreate the collider after setting spriteId

                    s.scale        = v.localScale;
                    s.pixelPerfect = v.alwaysPixelPerfect;
                    s.color        = v.color;
                }

                parents[id++] = go.transform;
                children.Add(go.transform);
            }

            int idx = 0;
            foreach (var v in batcher.batchedSprites)
            {
                Transform parent = batcher.transform;
                if (v.parentId != -1)
                {
                    parents.TryGetValue(v.parentId, out parent);
                }

                children[idx].parent = parent;
                ++idx;
            }

            batcher.transform.localScale = batcher.scale;

            batcher.batchedSprites = null;
            batcher.Build();
            EditorUtility.SetDirty(target);

            batcher.transform.position = batcherPos;
            batcher.transform.rotation = batcherRotation;
        }

        batcher.scale = EditorGUILayout.Vector3Field("Scale", batcher.scale);
    }
コード例 #4
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().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);
    }
コード例 #5
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().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);
        }
    }