예제 #1
0
    public void UpdateSprite(SpriteSheetUpdateTarget target)
    {
        Texture2D spritesheetTexture = spriteSheet.atlas.GetTexture("_MainTex") as Texture2D;

        switch (target)
        {
        case SpriteSheetUpdateTarget.SelectedSprite:
        {
            RagePixelRow row = spriteSheet.GetRow(spriteSheetGUI.currentRowKey);

            foreach (RagePixelCell cell in row.cells)
            {
                UpdateCell(cell, spritesheetTexture);
            }
            break;
        }

        case SpriteSheetUpdateTarget.SelectedFrame:
        {
            UpdateCell(spriteSheet.GetRow(spriteSheetGUI.currentRowKey).GetCell(animStripGUI.currentCellKey), spritesheetTexture);
            break;
        }

        case SpriteSheetUpdateTarget.AllSprites:
        {
            foreach (RagePixelRow row in spriteSheet.rows)
            {
                foreach (RagePixelCell cell in row.cells)
                {
                    UpdateCell(cell, spritesheetTexture);
                }
            }
            break;
        }
        }

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

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

        if (inspector != null)
        {
            inspector.animStripGUI.isDirty   = true;
            inspector.spriteSheetGUI.isDirty = true;
        }
    }
예제 #2
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");
        }
    }