Ejemplo n.º 1
0
    public static void ImportRowFromSheetTexture(
        RagePixelSpriteSheet spritesheet,
        RagePixelRow destRow,
        Texture2D tex,
        int importSpriteWidth, int importSpriteHeight,
        bool importSpriteTopLeft
        )
    {
        int framesWide = tex.width / importSpriteWidth;
        int framesHigh = tex.height / importSpriteHeight;
        int cellCount  = framesWide * framesHigh;

        destRow.Clear();
        //loop to allocate cell space
        for (int i = 0; i < cellCount; i++)
        {
            destRow.InsertCell(i, RagePixelUtil.RandomKey());
        }
        RebuildAtlas(spritesheet, true, "Import row from spritesheet");
        float importUVPerFrameW = 1.0f / framesWide;
        float importUVPerFrameH = 1.0f / framesHigh;

        Texture2D spritesheetTexture = spritesheet.atlas.GetTexture("_MainTex") as Texture2D;

        //loop to copy texture to UVs
        for (int i = 0; i < cellCount; i++)
        {
            int  y         = i / framesWide;
            int  x         = i - (y * framesWide);
            Rect importUVs = new Rect(x * importUVPerFrameW, (importSpriteTopLeft ? (framesHigh - 1 - y) : y) * importUVPerFrameH, importUVPerFrameW, importUVPerFrameH);
            Rect uvs       = destRow.cells[i].uv;
            RagePixelUtil.CopyPixels(tex, importUVs, spritesheetTexture, uvs);
        }
    }
Ejemplo n.º 2
0
    public void UpdateCell(RagePixelCell cell, Texture2D spritesheetTexture)
    {
        if (cell.importAssetPath != "")
        {
            Texture2D textureAsset = AssetDatabase.LoadAssetAtPath(cell.importAssetPath, typeof(Texture2D)) as Texture2D;

            if (textureAsset)
            {
                RagePixelUtil.CopyPixels(textureAsset, new Rect(0f, 0f, 1f, 1f), spritesheetTexture, cell.uv);
            }
            else
            {
                Debug.LogWarning("Frame has reference to '" + cell.importAssetPath + "' but it does not exist");
            }
        }
    }
Ejemplo n.º 3
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");
        }
    }