예제 #1
0
        public static void ConvertNGUIAtlasToUI(UnityEditor.MenuCommand command)
        {
            UIAtlas   atlas       = UnityEditor.Selection.activeGameObject.GetComponent <UIAtlas> ();
            Texture2D spriteSheet = atlas.texture as Texture2D;
            string    assetPath   = UnityEditor.AssetDatabase.GetAssetPath(spriteSheet.GetInstanceID());

            Debug.Log("Getting asset importer at path " + assetPath);
            UnityEditor.TextureImporter importer = UnityEditor.TextureImporter.GetAtPath(assetPath) as UnityEditor.TextureImporter;

            if (atlas == null || spriteSheet == null)
            {
                return;
            }
            List <UnityEditor.SpriteMetaData> spriteData = new List <UnityEditor.SpriteMetaData> ();

            List <string> spriteNames = atlas.GetListOfSprites();
            int           height      = spriteSheet.height;
            int           width       = spriteSheet.width;

            foreach (string spriteName in spriteNames)
            {
                UIAtlas.Sprite sprite = atlas.GetSprite(spriteName);
                //flip the positions
                Rect rect = sprite.outer;
                //Rect rect = new Rect (0, 0, 50, 50);
                rect.y = height - rect.y - rect.height;
                Vector2 pivot = new Vector2(0.5f, 0.5f);                 // = rect.center;
                //pivot.y = height - pivot.y;
                Vector4 border = Vector4.zero;

                /*if (sprite.hasPadding) {
                 *      border.w = sprite.inner.width;
                 *      border.x = sprite.paddingRight;
                 *      border.y = sprite.paddingTop;
                 *      border.z = sprite.paddingBottom;
                 * }*/
                UnityEditor.SpriteMetaData data = new UnityEditor.SpriteMetaData();
                data.border    = border;
                data.pivot     = pivot;
                data.rect      = rect;
                data.name      = spriteName;
                data.alignment = 0;
                Debug.Log("Created sprite " + spriteName + " with rect: "
                          + "xMin" + rect.xMin + ", "
                          + "xMax" + rect.xMax + ", "
                          + "yMin" + rect.yMin + ", "
                          + "yMax" + rect.yMax + "\n"
                          + "pivotx " + pivot.x + ", "
                          + "pivoty " + pivot.y);
                spriteData.Add(data);
            }

            importer.spriteImportMode = UnityEditor.SpriteImportMode.Multiple;
            importer.spritesheet      = spriteData.ToArray();

            //UnityEditor.AssetDatabase.StartAssetEditing ();
            UnityEditor.AssetDatabase.ImportAsset(importer.assetPath);
            //UnityEditor.AssetDatabase.StopAssetEditing ();
        }
예제 #2
0
파일: Tileset.cs 프로젝트: Aye1/Herborist
        public void UpdateTilesetConfigFromAtlasImportSettings()
        {
            if (AtlasTexture != null)
            {
                string assetPath = UnityEditor.AssetDatabase.GetAssetPath(AtlasTexture);
                if (!string.IsNullOrEmpty(assetPath))
                {
                    UnityEditor.TextureImporter textureImporter = UnityEditor.AssetImporter.GetAtPath(assetPath) as UnityEditor.TextureImporter;
                    if (textureImporter != null)
                    {
                        m_pixelsPerUnit = textureImporter.spritePixelsPerUnit;
                        if (textureImporter.textureType == UnityEditor.TextureImporterType.Sprite)
                        {
                            if (textureImporter.spriteImportMode == UnityEditor.SpriteImportMode.Multiple)
                            {
                                List <Tile> tiles = new List <Tile>();
                                if (textureImporter.spritesheet.Length >= 2)
                                {
                                    UnityEditor.SpriteMetaData spr0 = textureImporter.spritesheet[0];
                                    UnityEditor.SpriteMetaData spr1 = textureImporter.spritesheet[1];
                                    TilePxSize     = textureImporter.spritesheet[0].rect.size;
                                    SliceOffset    = spr0.rect.position; SliceOffset.y = AtlasTexture.height - spr0.rect.y - spr0.rect.height;
                                    SlicePadding.x = spr1.rect.x - spr0.rect.xMax;
                                }
                                if (textureImporter.spritesheet.Length >= 2)
                                {
                                    //+++Ask before importing tiles
                                    if (m_tiles.Count == 0 || !UnityEditor.EditorUtility.DisplayDialog("Import Sprite Sheet?", "This texture atlas contains sliced sprites. Do you want to overwrite current tiles with these ones?", "Yes", "No"))
                                    //---
                                    {
                                        m_tilesetHeight = 0;
                                        foreach (UnityEditor.SpriteMetaData spriteData in textureImporter.spritesheet)
                                        {
                                            Rect rUV = new Rect(Vector2.Scale(spriteData.rect.position, AtlasTexture.texelSize), Vector2.Scale(spriteData.rect.size, AtlasTexture.texelSize));
                                            tiles.Add(new Tile()
                                            {
                                                uv = rUV
                                            });
                                            if (tiles.Count >= 2)
                                            {
                                                if (tiles[tiles.Count - 2].uv.y != tiles[tiles.Count - 1].uv.y)
                                                {
                                                    if (m_tilesetHeight == 1)
                                                    {
                                                        m_tilesetWidth = tiles.Count - 1;
                                                        SlicePadding.y = textureImporter.spritesheet[tiles.Count - 2].rect.y - textureImporter.spritesheet[tiles.Count - 1].rect.yMax;
                                                    }
                                                    ++m_tilesetHeight;
                                                }
                                            }
                                            else
                                            {
                                                ++m_tilesetHeight;
                                            }
                                        }
                                        TileRowLength = m_tilesetWidth;
                                        //Copy data from previous tiles
#if UNITY_EDITOR
                                        if (m_tiles != null && m_tiles.Count > 0 && UnityEditor.EditorUtility.DisplayDialog("Keep previous tile properties?", "Keeping previous tile properties will copy the collider and paramters of previous tiles", "Yes", "No"))
#endif
                                        {
                                            for (int i = 0; i < m_tiles.Count && i < tiles.Count; ++i)
                                            {
                                                tiles[i].collData       = m_tiles[i].collData;
                                                tiles[i].paramContainer = m_tiles[i].paramContainer;
                                                tiles[i].prefabData     = m_tiles[i].prefabData;
                                            }
                                        }
                                        m_tiles = tiles;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }