예제 #1
0
    public void ImportSprite(SpriteSheetImportTarget target)
    {
        string path = AssetDatabase.GetAssetPath(newTexture);

        TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;

        textureImporter.isReadable    = true;
        textureImporter.filterMode    = FilterMode.Point;
        textureImporter.npotScale     = TextureImporterNPOTScale.None;
        textureImporter.textureFormat = TextureImporterFormat.AutomaticTruecolor;
        AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);

        if (textureImporter.isReadable)
        {
            int       newKey             = RagePixelUtil.RandomKey();
            int       newKey2            = RagePixelUtil.RandomKey();
            Texture2D spritesheetTexture = spriteSheet.atlas.GetTexture("_MainTex") as Texture2D;

            switch (target)
            {
            case SpriteSheetImportTarget.SpriteSheet:
            {
                RagePixelRow row = spriteSheet.AddRow(newKey, importSpriteWidth, importSpriteHeight);
                row.name = newTexture.name;
                RagePixelUtil.ImportRowFromSheetTexture(
                    spriteSheet,
                    row,
                    newTexture,
                    importSpriteWidth, importSpriteHeight,
                    importSpriteTopLeft
                    );
                spriteSheetGUI.currentRowKey = newKey;
                animStripGUI.currentCellKey  = row.cells[0].key;
                break;
            }

            case SpriteSheetImportTarget.NewSprite:
            {
                RagePixelRow row = spriteSheet.AddRow(newKey, newTexture.width, newTexture.height);
                row.InsertCell(0, newKey2).importAssetPath = path;

                spriteSheetGUI.currentRowKey = newKey;
                animStripGUI.currentCellKey  = newKey2;

                RagePixelUtil.RebuildAtlas(spriteSheet, true, "Import texture as new sprite");

                Rect uvs = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).GetCell(animStripGUI.currentCellKey).uv;
                RagePixelUtil.CopyPixels(newTexture, new Rect(0f, 0f, 1f, 1f), spritesheetTexture, uvs);
                break;
            }

            case SpriteSheetImportTarget.NewFrame:
            {
                RagePixelRow row   = spriteSheet.GetRow(spriteSheetGUI.currentRowKey);
                int          index = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).GetIndex(animStripGUI.currentCellKey) + 1;
                row.InsertCell(index, newKey2).importAssetPath = path;

                animStripGUI.currentCellKey = newKey2;

                RagePixelUtil.RebuildAtlas(spriteSheet, true, "Import texture as new frame");

                Rect uvs = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).GetCell(animStripGUI.currentCellKey).uv;
                RagePixelUtil.CopyPixels(newTexture, new Rect(0f, 0f, 1f, 1f), spritesheetTexture, uvs);
                break;
            }

            case SpriteSheetImportTarget.Selected:
            {
                RagePixelCell cell = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).GetCell(animStripGUI.currentCellKey);
                Rect          uvs  = cell.uv;
                RagePixelUtil.CopyPixels(newTexture, new Rect(0f, 0f, 1f, 1f), spritesheetTexture, uvs);
                cell.importAssetPath = path;
                break;
            }
            }

            RagePixelUtil.SaveSpritesheetTextureToDisk(spriteSheet);
            RagePixelUtil.RebuildAtlas(spriteSheet, true, "save after import");

            spriteSheetGUI.isDirty = true;
            animStripGUI.isDirty   = true;

            if (inspector != null)
            {
                inspector.animStripGUI.isDirty   = true;
                inspector.spriteSheetGUI.isDirty = true;
            }
        }
        else
        {
            EditorUtility.DisplayDialog("Texture is not readable", "Set texture type to advanced and read/write as enabled from the import options.", "OK");
        }
    }