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); } }
public static RagePixelSpriteSheet CreateNewSpritesheet() { RagePixelSpriteSheet spriteSheet = ScriptableObject.CreateInstance("RagePixelSpriteSheet") as RagePixelSpriteSheet; int cnt = 1; string path = defaultSpritesheetPath + assetDatabaseDirSeparatorChar + "spritesheet_" + cnt.ToString() + ".asset"; while (File.Exists(path)) { cnt++; path = defaultSpritesheetPath + assetDatabaseDirSeparatorChar + "spritesheet_" + cnt.ToString() + ".asset"; } spriteSheet.name = "spritesheet_" + cnt.ToString(); spriteSheet.atlas = CreateNewAtlas(defaultAtlasSize, spriteSheet.name); if (!Directory.Exists(defaultSpritesheetPath)) { Directory.CreateDirectory(defaultSpritesheetPath); } RagePixelRow row = spriteSheet.AddRow(RandomKey(), defaultSpriteSize, defaultSpriteSize); int newKey = RandomKey(); row.InsertCell(0, newKey); row.cells[0].uv = new Rect(0f, 0f, 1f, 1f); AssetDatabase.CreateAsset(spriteSheet, path); //spriteSheet.hideFlags = HideFlags.DontSave; AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); return(spriteSheet); }
public static void SaveSpritesheetTextureToDisk(RagePixelSpriteSheet spriteSheet) { RagePixelUtil.saveAtlasPng( Path.GetDirectoryName(AssetDatabase.GetAssetPath(spriteSheet)), spriteSheet.name, spriteSheet.atlas.GetTexture("_MainTex") as Texture2D ); }
public RagePixelRow GetCurrentRow() { if (Application.isPlaying) { if (lastRowCacheKey == currentRowKey && lastRowSpriteSheetCache.Equals(spriteSheet)) { return lastRowCache; } else { lastRowCache = spriteSheet.GetRow(currentRowKey); lastRowCacheKey = currentRowKey; lastRowSpriteSheetCache = spriteSheet; return lastRowCache; } } else { return spriteSheet.GetRow(currentRowKey); } }
public RagePixelCell GetCurrentCell() { if (Application.isPlaying) { if (lastCellCacheKey == currentCellKey && lastCellSpriteSheetCache.Equals(spriteSheet)) { return lastCellCache; } else { lastCellCache = GetCurrentRow().GetCell(currentCellKey); lastCellCacheKey = currentCellKey; lastCellSpriteSheetCache = spriteSheet; return lastCellCache; } } else { return GetCurrentRow().GetCell(currentCellKey); } }
void Awake() { lastRowSpriteSheetCache = null; lastCellSpriteSheetCache = null; lastRowCache = null; lastCellCache = null; lastCellCacheKey = 0; lastRowCacheKey = 0; meshIsDirty = true; vertexColorsAreDirty = true; if (!Application.isPlaying) { MeshFilter meshFilter = null; MeshRenderer meshRenderer = null; meshRenderer = gameObject.GetComponent("MeshRenderer") as MeshRenderer; if (meshRenderer == null) { meshRenderer = gameObject.AddComponent("MeshRenderer") as MeshRenderer; } meshFilter = gameObject.GetComponent("MeshFilter") as MeshFilter; if (meshFilter == null) { meshFilter = gameObject.AddComponent("MeshFilter") as MeshFilter; } if (meshFilter.sharedMesh != null) { RagePixelSprite[] ragePixelSprites = GameObject.FindObjectsOfType(typeof(RagePixelSprite)) as RagePixelSprite[]; foreach (RagePixelSprite ragePixelSprite in ragePixelSprites) { MeshFilter otherMeshFilter = ragePixelSprite.GetComponent(typeof(MeshFilter)) as MeshFilter; if (otherMeshFilter != null) { if (otherMeshFilter.sharedMesh == meshFilter.sharedMesh && otherMeshFilter != meshFilter) { meshFilter.mesh = new Mesh(); toBeRefreshed = true; } } } } if (meshFilter.sharedMesh == null) { meshFilter.sharedMesh = new Mesh(); toBeRefreshed = true; } } else { meshIsDirty = true; refreshMesh(); } }
public static void RebuildAtlas(RagePixelSpriteSheet spriteSheet, bool atlasIsGrowing, string caller = "nobody") { int frameCount = spriteSheet.GetTotalCellCount(); Texture2D[] textures = new Texture2D[frameCount]; RagePixelCell[] cells = new RagePixelCell[frameCount]; int index = 0; EditorUtility.SetDirty(spriteSheet); Texture2D sourceTexture = spriteSheet.atlas.GetTexture("_MainTex") as Texture2D; if (sourceTexture != null) { foreach (RagePixelRow row in spriteSheet.rows) { foreach (RagePixelCell cell in row.cells) { textures[index] = new Texture2D(row.newPixelSizeX, row.newPixelSizeY); if (cell.uv.width > 0f && cell.uv.height > 0f) { clearPixels(textures[index]); CopyPixels(sourceTexture, cell.uv, textures[index], new Rect(0f, 0f, 1f, 1f)); } else { clearPixels(textures[index]); } cells[index] = cell; index++; } } } Texture2D newAtlasTexture = new Texture2D(defaultAtlasSize, defaultAtlasSize); int atlasPadding = defaultAtlasPadding; if (textures.Length == 1) { atlasPadding = 0; } Rect[] newUvs = newAtlasTexture.PackTextures(textures, atlasPadding, 4096); if (newUvs != null) { float totalAreaOld = 0f; float totalAreaNew = 0f; for (int i = 0; i < cells.Length; i++) { totalAreaOld += cells[i].uv.height * cells[i].uv.width; } for (int i = 0; i < newUvs.Length; i++) { totalAreaNew += newUvs[i].height * newUvs[i].width; } // Checking if the PackTextures() is going to crap all over spritesheet, when going over max texture size if (!(atlasIsGrowing && totalAreaNew < totalAreaOld && sourceTexture.width * sourceTexture.height >= newAtlasTexture.width * newAtlasTexture.height)) { for (int i = 0; i < newUvs.Length && i < cells.Length; i++) { cells[i].uv = newUvs[i]; } saveAtlasPng(defaultAtlasPath, spriteSheet.atlas.name, newAtlasTexture); for (int i = 0; i < textures.Length; i++) { Object.DestroyImmediate(textures[i]); } Object.DestroyImmediate(newAtlasTexture); RagePixelSprite[] sprites = Resources.FindObjectsOfTypeAll(typeof(RagePixelSprite)) as RagePixelSprite[]; EditorUtility.SetDirty(spriteSheet); EditorUtility.SetDirty(spriteSheet.atlas); EditorUtility.SetDirty(spriteSheet.atlas.GetTexture("_MainTex")); foreach (RagePixelRow row in spriteSheet.rows) { row.pixelSizeX = row.newPixelSizeX; row.pixelSizeY = row.newPixelSizeY; } foreach (RagePixelSprite sprite in sprites) { EditorUtility.SetDirty(sprite); sprite.checkKeyIntegrity(); sprite.SnapToIntegerPosition(); sprite.refreshMesh(); } AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } else { Debug.Log("ERROR: Too much data for max texture size (Spritesheet: " + spriteSheet.name + ")"); } } else { Debug.Log("ERROR: Atlas PackTextures() failed (Spritesheet: " + spriteSheet.name + ")"); } }
public void OnGUI() { int y = 2; int x = 5; spriteSheet = (RagePixelSpriteSheet)EditorGUI.ObjectField( new Rect(x, y, Screen.width - x * 2, 16), "Sprite sheet", spriteSheet, typeof(RagePixelSpriteSheet), false ); y += 20; if (spriteSheet != null) { if (spriteSheet != spriteSheetGUI.spriteSheet) { spriteSheetGUI.spriteSheet = spriteSheet; spriteSheetGUI.currentRowKey = spriteSheet.rows[0].key; animStripGUI.currentCellKey = spriteSheet.rows[0].cells[0].key; } spriteSheetGUI.positionX = x; spriteSheetGUI.positionY = y; animStripGUI.positionX = x; animStripGUI.positionY = spriteSheetGUI.positionY + spriteSheetGUI.pixelHeight + 5; GUI.color = RagePixelGUIIcons.greenButtonColor; if (GUI.Button(new Rect(Screen.width - 38f * 2 - 5, spriteSheetGUI.positionY + spriteSheetGUI.pixelHeight - 32f, 38f, 32f), "NEW")) { int index = spriteSheet.GetIndex(spriteSheetGUI.currentRowKey); RagePixelRow row = spriteSheet.AddRow( RagePixelUtil.RandomKey(), index + 1, spriteSheet.GetRow(spriteSheetGUI.currentRowKey).pixelSizeX, spriteSheet.GetRow(spriteSheetGUI.currentRowKey).pixelSizeY ); RagePixelCell cell = row.InsertCell(0, RagePixelUtil.RandomKey()); spriteSheetGUI.currentRowKey = row.key; animStripGUI.currentCellKey = cell.key; RagePixelUtil.RebuildAtlas(spriteSheet, true, "AddRow"); } GUI.color = RagePixelGUIIcons.redButtonColor; if (GUI.Button(new Rect(Screen.width - 38f - 5, spriteSheetGUI.positionY + spriteSheetGUI.pixelHeight - 32f, 38f, 32f), "DEL")) { if (spriteSheet.rows.Length > 1) { if (EditorUtility.DisplayDialog("Delete selected sprite?", "Are you sure?", "Delete", "Cancel")) { int index = spriteSheet.GetIndex(spriteSheetGUI.currentRowKey); spriteSheet.RemoveRowByKey(spriteSheetGUI.currentRowKey); int newKey = spriteSheet.rows[Mathf.Clamp(index, 0, spriteSheet.rows.Length - 1)].key; if (selectedSprite != null) { if (selectedSprite.currentRowKey == spriteSheetGUI.currentRowKey) { selectedSprite.meshIsDirty = true; selectedSprite.currentRowKey = newKey; selectedSprite.pixelSizeX = selectedSprite.GetCurrentRow().pixelSizeX; selectedSprite.pixelSizeY = selectedSprite.GetCurrentRow().pixelSizeY; } } spriteSheetGUI.currentRowKey = newKey; animStripGUI.currentCellKey = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).cells[0].key; RagePixelUtil.RebuildAtlas(spriteSheet, false, "DeleteRow"); if (inspector != null) { inspector.spriteSheetGUI.isDirty = true; inspector.animStripGUI.isDirty = true; } } } else { EditorUtility.DisplayDialog("Cannot delete", "Cannot delete the last sprite.", "OK"); } } y += spriteSheetGUI.pixelHeight + animStripGUI.pixelHeight + 10; GUI.color = RagePixelGUIIcons.greenButtonColor; if (GUI.Button(new Rect(Screen.width - 38f * 2 - 5, animStripGUI.positionY + animStripGUI.pixelHeight - 32f, 38f, 32f), "NEW")) { int index = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).GetIndex(animStripGUI.currentCellKey) + 1; RagePixelCell cell = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).InsertCell(index, RagePixelUtil.RandomKey()); animStripGUI.currentCellKey = cell.key; RagePixelUtil.RebuildAtlas(spriteSheet, true, "AddCell"); } GUI.color = RagePixelGUIIcons.redButtonColor; if (GUI.Button(new Rect(Screen.width - 38f - 5, animStripGUI.positionY + animStripGUI.pixelHeight - 32f, 38f, 32f), "DEL")) { if (spriteSheet.GetRow(spriteSheetGUI.currentRowKey).cells.Length > 1) { if (EditorUtility.DisplayDialog("Delete selected animation frame?", "Are you sure?", "Delete", "Cancel")) { int index = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).GetIndex(animStripGUI.currentCellKey); spriteSheet.GetRow(spriteSheetGUI.currentRowKey).RemoveCellByKey(animStripGUI.currentCellKey); int newKey = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).cells[Mathf.Clamp(index, 0, spriteSheet.GetRow(spriteSheetGUI.currentRowKey).cells.Length - 1)].key; if (selectedSprite != null) { if (selectedSprite.currentCellKey == animStripGUI.currentCellKey) { selectedSprite.meshIsDirty = true; selectedSprite.currentCellKey = newKey; selectedSprite.pixelSizeX = selectedSprite.GetCurrentRow().pixelSizeX; selectedSprite.pixelSizeY = selectedSprite.GetCurrentRow().pixelSizeY; } } animStripGUI.currentCellKey = newKey; RagePixelUtil.RebuildAtlas(spriteSheet, true, "DeleteCell"); if (inspector != null) { inspector.spriteSheetGUI.isDirty = true; inspector.animStripGUI.isDirty = true; } } } else { EditorUtility.DisplayDialog("Cannot delete", "Cannot delete the last animation frame.", "OK"); } } GUI.color = Color.white; spriteSheet.GetRow(spriteSheetGUI.currentRowKey).name = EditorGUI.TextField( new Rect(x, y, Mathf.Min(350, Screen.width - x * 2), 16), "Sprite Name", spriteSheet.GetRow(spriteSheetGUI.currentRowKey).name); y += 20; EditorGUI.LabelField( new Rect(x, y, Screen.width - x * 2, 16), "Frame Index", spriteSheet.GetRow(spriteSheetGUI.currentRowKey).GetIndex(animStripGUI.currentCellKey).ToString() + " (" + (spriteSheet.GetRow(spriteSheetGUI.currentRowKey).cells.Length - 1).ToString()+")"); y += 20; spriteSheet.GetRow(spriteSheetGUI.currentRowKey).GetCell(animStripGUI.currentCellKey).delay = EditorGUI.IntField( new Rect(x, y, Mathf.Min(200, Screen.width - x * 2), 16), "Frame Time", (int)spriteSheet.GetRow(spriteSheetGUI.currentRowKey).GetCell(animStripGUI.currentCellKey).delay); y += 20; GUILayout.Space(y + 20); int namedAnimationsFoldoutHeight = 0; showNamedAnimationsFoldout = EditorGUI.Foldout(new Rect(x, y, Screen.width - x * 2, 20), showNamedAnimationsFoldout, "Named animations"); y += 20; if (showNamedAnimationsFoldout) { GUI.color = Color.gray; x = 5; GUI.Label(new Rect(x, y, 170, 16), "Name"); x += 175; GUI.Label(new Rect(x, y, 40, 16), "Start"); x += 45; GUI.Label(new Rect(x, y, 40, 16), "End"); x += 45; GUI.Label(new Rect(x, y, 130, 16), "Type"); GUI.color = Color.white; y += 20; RagePixelAnimation[] animations = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).animations; for (int animIndex = 0; animIndex < animations.Length; animIndex++) { x = 5; animations[animIndex].name = EditorGUI.TextField(new Rect(x, y, 170, 16), animations[animIndex].name); x += 175; animations[animIndex].startIndex = EditorGUI.IntField(new Rect(x, y, 40, 16), animations[animIndex].startIndex); x += 45; animations[animIndex].endIndex = EditorGUI.IntField(new Rect(x, y, 40, 16), animations[animIndex].endIndex); x += 45; animations[animIndex].mode = (RagePixelSprite.AnimationMode)EditorGUI.EnumPopup(new Rect(x, y, 130, 16), animations[animIndex].mode); x += 135; if(GUI.Button(new Rect(x, y, 60, 16), "Delete")) { spriteSheet.GetRow(spriteSheetGUI.currentRowKey).RemoveAnimation(animIndex); } y += 20; namedAnimationsFoldoutHeight += 20; } x = 5; if (GUI.Button(new Rect(x, y, 50, 16), "Add")) { spriteSheet.GetRow(spriteSheetGUI.currentRowKey).AddAnimation(); } y += 20; namedAnimationsFoldoutHeight += 20; GUILayout.Space(namedAnimationsFoldoutHeight + 26); y += 6; } x = 5; showImportFoldout = EditorGUI.Foldout(new Rect(x, y, Screen.width - x * 2, 16), showImportFoldout, "Import"); if (showImportFoldout) { GUILayout.BeginHorizontal(); newTexture = (Texture2D)EditorGUILayout.ObjectField(" ", newTexture, typeof(Texture2D), false); GUILayout.BeginVertical(); if (GUI.Button(new Rect(x + 240f, y, 180f, 19f), "Import to selected frame")) { if (newTexture != null) { ImportSprite(SpriteSheetImportTarget.Selected); } } y += 21; if (GUI.Button(new Rect(x + 240f, y, 180f, 19f), "Import as new frame")) { if (newTexture != null) { ImportSprite(SpriteSheetImportTarget.NewFrame); } } y += 21; if (GUI.Button(new Rect(x + 240f, y, 180f, 19f), "Import as new sprite")) { if (newTexture != null) { ImportSprite(SpriteSheetImportTarget.NewSprite); } } GUILayout.EndVertical(); GUILayout.EndHorizontal(); } int oldRowKey = spriteSheetGUI.currentRowKey; animStripGUI.HandleGUIEvent(Event.current); spriteSheetGUI.HandleGUIEvent(Event.current); if(oldRowKey != spriteSheetGUI.currentRowKey) { animStripGUI.currentCellKey = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).cells[0].key; } if (animStripGUI.isDirty || spriteSheetGUI.isDirty) { Repaint(); if (inspector != null) { inspector.spriteSheetGUI.isDirty = true; inspector.animStripGUI.isDirty = true; inspector.Repaint(); } if (selectedSprite != null) { selectedSprite.meshIsDirty = true; selectedSprite.refreshMesh(); } } spriteSheetGUI.maxWidth = scenePixelWidth - 38 * 2 - 10 - spriteSheetGUI.positionX; animStripGUI.maxWidth = scenePixelWidth - 38 * 2 - 10 - animStripGUI.positionX; EditorGUI.DrawPreviewTexture(spriteSheetGUI.bounds, spriteSheetGUI.spriteSheetTexture); EditorGUI.DrawPreviewTexture(animStripGUI.bounds, animStripGUI.animStripTexture); } if (GUI.changed) { EditorUtility.SetDirty(spriteSheet); } }
public int GetAtlasCellCount(RagePixelSpriteSheet[] spriteSheets, Material atlas) { int count = 0; foreach (RagePixelSpriteSheet sheet in spriteSheets) { if (sheet.atlas.Equals(atlas)) { foreach (RagePixelRow row in sheet.rows) { count += row.cells.Length; } } } return count; }
public void OnGUI() { int y = 2; int x = 5; spriteSheet = (RagePixelSpriteSheet)EditorGUI.ObjectField( new Rect(x, y, Screen.width - x * 2, 16), "Sprite sheet", spriteSheet, typeof(RagePixelSpriteSheet), false ); y += 20; if (spriteSheet != null) { if (spriteSheet != spriteSheetGUI.spriteSheet) { spriteSheetGUI.spriteSheet = spriteSheet; spriteSheetGUI.currentRowKey = spriteSheet.rows[0].key; copySpriteSheetGUI.spriteSheet = spriteSheet; copySpriteSheetGUI.currentRowKey = spriteSheet.rows[0].key; animStripGUI.currentCellKey = spriteSheet.rows[0].cells[0].key; } spriteSheetGUI.positionX = x; spriteSheetGUI.positionY = y; animStripGUI.positionX = x; animStripGUI.positionY = spriteSheetGUI.positionY + spriteSheetGUI.pixelHeight + 5; GUI.color = RagePixelGUIIcons.greenButtonColor; if (GUI.Button(new Rect(Screen.width - 38f * 2 - 5, spriteSheetGUI.positionY + spriteSheetGUI.pixelHeight - 32f, 38f, 32f), "NEW")) { int index = spriteSheet.GetIndex(spriteSheetGUI.currentRowKey); RagePixelRow row = spriteSheet.AddRow( RagePixelUtil.RandomKey(), index + 1, spriteSheet.GetRow(spriteSheetGUI.currentRowKey).pixelSizeX, spriteSheet.GetRow(spriteSheetGUI.currentRowKey).pixelSizeY ); RagePixelCell cell = row.InsertCell(0, RagePixelUtil.RandomKey()); spriteSheetGUI.currentRowKey = row.key; animStripGUI.currentCellKey = cell.key; RagePixelUtil.RebuildAtlas(spriteSheet, true, "AddRow"); } GUI.color = RagePixelGUIIcons.redButtonColor; if (GUI.Button(new Rect(Screen.width - 38f - 5, spriteSheetGUI.positionY + spriteSheetGUI.pixelHeight - 32f, 38f, 32f), "DEL")) { if (spriteSheet.rows.Length > 1) { if (EditorUtility.DisplayDialog("Delete selected sprite?", "Are you sure?", "Delete", "Cancel")) { int index = spriteSheet.GetIndex(spriteSheetGUI.currentRowKey); spriteSheet.RemoveRowByKey(spriteSheetGUI.currentRowKey); int newKey = spriteSheet.rows[Mathf.Clamp(index, 0, spriteSheet.rows.Length - 1)].key; if (selectedSprite != null) { if (selectedSprite.currentRowKey == spriteSheetGUI.currentRowKey) { selectedSprite.meshIsDirty = true; selectedSprite.currentRowKey = newKey; selectedSprite.pixelSizeX = selectedSprite.GetCurrentRow().pixelSizeX; selectedSprite.pixelSizeY = selectedSprite.GetCurrentRow().pixelSizeY; } } spriteSheetGUI.currentRowKey = newKey; animStripGUI.currentCellKey = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).cells[0].key; RagePixelUtil.RebuildAtlas(spriteSheet, false, "DeleteRow"); if (inspector != null) { inspector.spriteSheetGUI.isDirty = true; inspector.animStripGUI.isDirty = true; } } } else { EditorUtility.DisplayDialog("Cannot delete", "Cannot delete the last sprite.", "OK"); } } y += spriteSheetGUI.pixelHeight + animStripGUI.pixelHeight + 10; GUI.color = RagePixelGUIIcons.greenButtonColor; if (GUI.Button(new Rect(Screen.width - 38f * 2 - 5, animStripGUI.positionY + animStripGUI.pixelHeight - 32f, 38f, 32f), "NEW")) { int index = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).GetIndex(animStripGUI.currentCellKey) + 1; RagePixelCell cell = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).InsertCell(index, RagePixelUtil.RandomKey()); animStripGUI.currentCellKey = cell.key; RagePixelUtil.RebuildAtlas(spriteSheet, true, "AddCell"); } GUI.color = RagePixelGUIIcons.redButtonColor; if (GUI.Button(new Rect(Screen.width - 38f - 5, animStripGUI.positionY + animStripGUI.pixelHeight - 32f, 38f, 32f), "DEL")) { if (spriteSheet.GetRow(spriteSheetGUI.currentRowKey).cells.Length > 1) { if (EditorUtility.DisplayDialog("Delete selected animation frame?", "Are you sure?", "Delete", "Cancel")) { int index = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).GetIndex(animStripGUI.currentCellKey); spriteSheet.GetRow(spriteSheetGUI.currentRowKey).RemoveCellByKey(animStripGUI.currentCellKey); int newKey = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).cells[Mathf.Clamp(index, 0, spriteSheet.GetRow(spriteSheetGUI.currentRowKey).cells.Length - 1)].key; if (selectedSprite != null) { if (selectedSprite.currentCellKey == animStripGUI.currentCellKey) { selectedSprite.meshIsDirty = true; selectedSprite.currentCellKey = newKey; selectedSprite.pixelSizeX = selectedSprite.GetCurrentRow().pixelSizeX; selectedSprite.pixelSizeY = selectedSprite.GetCurrentRow().pixelSizeY; } } animStripGUI.currentCellKey = newKey; RagePixelUtil.RebuildAtlas(spriteSheet, true, "DeleteCell"); if (inspector != null) { inspector.spriteSheetGUI.isDirty = true; inspector.animStripGUI.isDirty = true; } } } else { EditorUtility.DisplayDialog("Cannot delete", "Cannot delete the last animation frame.", "OK"); } } GUI.color = Color.white; if (spriteSheet.GetRow(spriteSheetGUI.currentRowKey).name == null) { spriteSheet.GetRow(spriteSheetGUI.currentRowKey).name = ""; } spriteSheet.GetRow(spriteSheetGUI.currentRowKey).name = EditorGUI.TextField( new Rect(x, y, Mathf.Min(350, Screen.width - x * 2), 16), "Sprite Name", spriteSheet.GetRow(spriteSheetGUI.currentRowKey).name); y += 20; RagePixelCell selectedCell = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).GetCell(animStripGUI.currentCellKey); EditorGUI.LabelField( new Rect(x, y, Screen.width - x * 2, 16), "Frame Index", spriteSheet.GetRow(spriteSheetGUI.currentRowKey).GetIndex(animStripGUI.currentCellKey).ToString() + " (" + (spriteSheet.GetRow(spriteSheetGUI.currentRowKey).cells.Length - 1).ToString() + ")" + (selectedCell.importAssetPath == "" ? "" : (" - " + selectedCell.importAssetPath))); y += 20; spriteSheet.GetRow(spriteSheetGUI.currentRowKey).GetCell(animStripGUI.currentCellKey).delay = EditorGUI.IntField( new Rect(x, y, Mathf.Min(200, Screen.width - x * 2), 16), "Frame Time", (int)spriteSheet.GetRow(spriteSheetGUI.currentRowKey).GetCell(animStripGUI.currentCellKey).delay); y += 20; GUILayout.Space(y + 20); int rangeAnimationsFoldoutHeight = 0; showRangeAnimationsFoldout = EditorGUI.Foldout(new Rect(x, y, Screen.width - x * 2, 20), showRangeAnimationsFoldout, "Range animations"); y += 20; if (showRangeAnimationsFoldout) { GUI.color = Color.gray; x = 5; GUI.Label(new Rect(x, y, 170, 16), "Name"); x += 175; GUI.Label(new Rect(x, y, 40, 16), "Start"); x += 45; GUI.Label(new Rect(x, y, 40, 16), "End"); x += 45; GUI.Label(new Rect(x, y, 130, 16), "Type"); GUI.color = Color.white; y += 20; RagePixelAnimation[] animations = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).animations; for (int animIndex = 0; animIndex < animations.Length; animIndex++) { if (animations[animIndex].frameMode != RagePixelSprite.FrameMode.Range) { continue; } x = 5; animations[animIndex].name = EditorGUI.TextField(new Rect(x, y, 170, 16), animations[animIndex].name); x += 175; animations[animIndex].startIndex = EditorGUI.IntField(new Rect(x, y, 40, 16), animations[animIndex].startIndex); x += 45; animations[animIndex].endIndex = EditorGUI.IntField(new Rect(x, y, 40, 16), animations[animIndex].endIndex); x += 45; animations[animIndex].mode = (RagePixelSprite.AnimationMode)EditorGUI.EnumPopup(new Rect(x, y, 130, 16), animations[animIndex].mode); x += 135; if (GUI.Button(new Rect(x, y, 60, 16), "Delete")) { spriteSheet.GetRow(spriteSheetGUI.currentRowKey).RemoveAnimation(animIndex); } y += 20; rangeAnimationsFoldoutHeight += 20; } x = 5; if (GUI.Button(new Rect(x, y, 50, 16), "Add")) { RagePixelAnimation anim = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).AddAnimation(); anim.frameMode = RagePixelSprite.FrameMode.Range; } y += 20; rangeAnimationsFoldoutHeight += 20; GUILayout.Space(rangeAnimationsFoldoutHeight + 26); y += 6; } int sequenceAnimationsFoldoutHeight = 0; showSequenceAnimationsFoldout = EditorGUI.Foldout(new Rect(x, y, Screen.width - x * 2, 20), showSequenceAnimationsFoldout, "Sequence animations"); y += 20; if (showSequenceAnimationsFoldout) { GUI.color = Color.gray; x = 5; GUI.Label(new Rect(x, y, 170, 16), "Name"); x += 175; GUI.Label(new Rect(x, y, 40, 16), "Type"); x += 135; GUI.Label(new Rect(x, y, 60, 16), "Frames"); GUI.color = Color.white; y += 20; RagePixelAnimation[] animations = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).animations; for (int animIndex = 0; animIndex < animations.Length; animIndex++) { if (animations[animIndex].frameMode != RagePixelSprite.FrameMode.Sequence) { continue; } x = 5; animations[animIndex].name = EditorGUI.TextField(new Rect(x, y, 170, 16), animations[animIndex].name); x += 175; animations[animIndex].mode = (RagePixelSprite.AnimationMode)EditorGUI.EnumPopup(new Rect(x, y, 130, 16), animations[animIndex].mode); x += 135; //frames if (animations[animIndex].frames == null || animations[animIndex].frames.Length == 0) { animations[animIndex].frames = new int[] { 0 }; } for (int i = 0; i < animations[animIndex].frames.Length; i++) { animations[animIndex].frames[i] = EditorGUI.IntField(new Rect(x, y, 24, 16), animations[animIndex].frames[i]); x += 28; } if (GUI.Button(new Rect(x, y, 20, 16), "-")) { //reduce length by 1 Array.Resize(ref animations[animIndex].frames, animations[animIndex].frames.Length - 1); } x += 22; if (GUI.Button(new Rect(x, y, 20, 16), "+")) { //increase length by 1 Array.Resize(ref animations[animIndex].frames, animations[animIndex].frames.Length + 1); } x += 28; if (GUI.Button(new Rect(x, y, 60, 16), "Delete")) { spriteSheet.GetRow(spriteSheetGUI.currentRowKey).RemoveAnimation(animIndex); } y += 20; sequenceAnimationsFoldoutHeight += 20; } x = 5; if (GUI.Button(new Rect(x, y, 50, 16), "Add")) { RagePixelAnimation anim = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).AddAnimation(); anim.frameMode = RagePixelSprite.FrameMode.Sequence; } y += 20; sequenceAnimationsFoldoutHeight += 20; GUILayout.Space(sequenceAnimationsFoldoutHeight + 26); y += 6; } x = 5; showCopyAnimationsFoldout = EditorGUI.Foldout(new Rect(x, y, Screen.width - x * 2, 16), showCopyAnimationsFoldout, "Copy Animations"); if (showCopyAnimationsFoldout) { y += 20; GUI.Label(new Rect(x, y, Screen.width - x * 2, 16), "Other Sprite"); y += 20; copySpriteSheetGUI.positionX = x; copySpriteSheetGUI.positionY = y; y += copySpriteSheetGUI.pixelHeight + 10; GUI.enabled = spriteSheetGUI.currentRowKey != copySpriteSheetGUI.currentRowKey; if (GUI.Button(new Rect(x, y, 180f, 19f), "Copy From Other")) { RagePixelRow thisRow = spriteSheet.GetRow(spriteSheetGUI.currentRowKey); RagePixelRow otherRow = spriteSheet.GetRow(copySpriteSheetGUI.currentRowKey); thisRow.CopyAnimationsFrom(otherRow); } if (GUI.Button(new Rect(x + 190, y, 180f, 19f), "Copy To Other")) { RagePixelRow thisRow = spriteSheet.GetRow(spriteSheetGUI.currentRowKey); RagePixelRow otherRow = spriteSheet.GetRow(copySpriteSheetGUI.currentRowKey); otherRow.CopyAnimationsFrom(thisRow); } GUI.enabled = true; y += 21; } y += 20; showImportFoldout = EditorGUI.Foldout(new Rect(x, y, Screen.width - x * 2, 16), showImportFoldout, "Import"); if (showImportFoldout) { GUILayout.Space(24); GUILayout.BeginHorizontal(); newTexture = (Texture2D)EditorGUILayout.ObjectField(" ", newTexture, typeof(Texture2D), false); GUILayout.BeginVertical(); y += 10; if (GUI.Button(new Rect(x + 240f, y, 180f, 19f), "Import to selected frame")) { if (newTexture != null) { ImportSprite(SpriteSheetImportTarget.Selected); } } y += 21; if (GUI.Button(new Rect(x + 240f, y, 180f, 19f), "Import as new frame")) { if (newTexture != null) { ImportSprite(SpriteSheetImportTarget.NewFrame); } } y += 21; if (GUI.Button(new Rect(x + 240f, y, 180f, 19f), "Import as new sprite")) { if (newTexture != null) { ImportSprite(SpriteSheetImportTarget.NewSprite); } } y += 21; if (GUI.Button(new Rect(x + 240f, y, 180f, 19f), "Import spritesheet")) { if (newTexture != null) { ImportSprite(SpriteSheetImportTarget.SpriteSheet); } } //sprite width, sprite height y += 21; importSpriteWidth = EditorGUI.IntField(new Rect(x + 240f, y, 180f, 19f), "Frame Width", importSpriteWidth); y += 21; importSpriteHeight = EditorGUI.IntField(new Rect(x + 240f, y, 180f, 19f), "Frame Height", importSpriteHeight); y += 21; importSpriteTopLeft = EditorGUI.Toggle(new Rect(x + 240f, y, 180f, 19f), "First Frame at Top Left", importSpriteTopLeft); GUILayout.EndVertical(); GUILayout.EndHorizontal(); } // Update references to sprites y += 20; showUpdateFoldout = EditorGUI.Foldout(new Rect(x, y, Screen.width - x * 2, 16), showUpdateFoldout, "Update"); if (showUpdateFoldout) { y += 20; GUILayout.BeginVertical(); if (GUI.Button(new Rect(5, y, 180f, 19f), "Update Selected Frame")) { UpdateSprite(SpriteSheetUpdateTarget.SelectedFrame); } /* * if(GUI.Button(new Rect(190, y, 180f, 19f), "Save Frame to Source")) * { * if(newTexture != null) * { * * } * } */ y += 21; if (GUI.Button(new Rect(5, y, 180f, 19f), "Update Selected Sprite")) { UpdateSprite(SpriteSheetUpdateTarget.SelectedSprite); } y += 21; if (GUI.Button(new Rect(5, y, 180f, 19f), "Update All Sprites")) { UpdateSprite(SpriteSheetUpdateTarget.AllSprites); } GUILayout.EndVertical(); } int oldRowKey = spriteSheetGUI.currentRowKey; animStripGUI.HandleGUIEvent(Event.current); spriteSheetGUI.HandleGUIEvent(Event.current); if (oldRowKey != spriteSheetGUI.currentRowKey) { animStripGUI.currentCellKey = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).cells[0].key; } if (animStripGUI.isDirty || spriteSheetGUI.isDirty) { Repaint(); if (inspector != null) { inspector.spriteSheetGUI.isDirty = true; inspector.animStripGUI.isDirty = true; inspector.Repaint(); } if (selectedSprite != null) { selectedSprite.meshIsDirty = true; selectedSprite.refreshMesh(); } } spriteSheetGUI.maxWidth = scenePixelWidth - 38 * 2 - 10 - spriteSheetGUI.positionX; animStripGUI.maxWidth = scenePixelWidth - 38 * 2 - 10 - animStripGUI.positionX; EditorGUI.DrawPreviewTexture(spriteSheetGUI.bounds, spriteSheetGUI.spriteSheetTexture); EditorGUI.DrawPreviewTexture(animStripGUI.bounds, animStripGUI.animStripTexture); if (showCopyAnimationsFoldout) { copySpriteSheetGUI.HandleGUIEvent(Event.current); copySpriteSheetGUI.maxWidth = scenePixelWidth - 38 * 2 - 10 - copySpriteSheetGUI.positionX; EditorGUI.DrawPreviewTexture(copySpriteSheetGUI.bounds, copySpriteSheetGUI.spriteSheetTexture); } } if (GUI.changed) { EditorUtility.SetDirty(spriteSheet); } }
public void OnGUI() { int y = 2; int x = 5; spriteSheet = (RagePixelSpriteSheet)EditorGUI.ObjectField( new Rect(x, y, Screen.width - x * 2, 16), "Sprite sheet", spriteSheet, typeof(RagePixelSpriteSheet), false ); y += 20; if (spriteSheet != null) { if (spriteSheet != spriteSheetGUI.spriteSheet) { spriteSheetGUI.spriteSheet = spriteSheet; spriteSheetGUI.currentRowKey = spriteSheet.rows[0].key; animStripGUI.currentCellKey = spriteSheet.rows[0].cells[0].key; } spriteSheetGUI.positionX = x; spriteSheetGUI.positionY = y; animStripGUI.positionX = x; animStripGUI.positionY = spriteSheetGUI.positionY + spriteSheetGUI.pixelHeight + 5; GUI.color = RagePixelGUIIcons.greenButtonColor; if (GUI.Button(new Rect(Screen.width - 38f * 2 - 5, spriteSheetGUI.positionY + spriteSheetGUI.pixelHeight - 32f, 38f, 32f), "NEW")) { int index = spriteSheet.GetIndex(spriteSheetGUI.currentRowKey); RagePixelRow row = spriteSheet.AddRow( RagePixelUtil.RandomKey(), index + 1, spriteSheet.GetRow(spriteSheetGUI.currentRowKey).pixelSizeX, spriteSheet.GetRow(spriteSheetGUI.currentRowKey).pixelSizeY ); RagePixelCell cell = row.InsertCell(0, RagePixelUtil.RandomKey()); spriteSheetGUI.currentRowKey = row.key; animStripGUI.currentCellKey = cell.key; RagePixelUtil.RebuildAtlas(spriteSheet, true, "AddRow"); } GUI.color = RagePixelGUIIcons.redButtonColor; if (GUI.Button(new Rect(Screen.width - 38f - 5, spriteSheetGUI.positionY + spriteSheetGUI.pixelHeight - 32f, 38f, 32f), "DEL")) { if (spriteSheet.rows.Length > 1) { if (EditorUtility.DisplayDialog("Delete selected sprite?", "Are you sure?", "Delete", "Cancel")) { int index = spriteSheet.GetIndex(spriteSheetGUI.currentRowKey); spriteSheet.RemoveRowByKey(spriteSheetGUI.currentRowKey); int newKey = spriteSheet.rows[Mathf.Clamp(index, 0, spriteSheet.rows.Length - 1)].key; if (selectedSprite != null) { if (selectedSprite.currentRowKey == spriteSheetGUI.currentRowKey) { selectedSprite.meshIsDirty = true; selectedSprite.currentRowKey = newKey; selectedSprite.pixelSizeX = selectedSprite.GetCurrentRow().pixelSizeX; selectedSprite.pixelSizeY = selectedSprite.GetCurrentRow().pixelSizeY; } } spriteSheetGUI.currentRowKey = newKey; animStripGUI.currentCellKey = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).cells[0].key; RagePixelUtil.RebuildAtlas(spriteSheet, false, "DeleteRow"); if (inspector != null) { inspector.spriteSheetGUI.isDirty = true; inspector.animStripGUI.isDirty = true; } } } else { EditorUtility.DisplayDialog("Cannot delete", "Cannot delete the last sprite.", "OK"); } } y += spriteSheetGUI.pixelHeight + animStripGUI.pixelHeight + 10; GUI.color = RagePixelGUIIcons.greenButtonColor; if (GUI.Button(new Rect(Screen.width - 38f * 2 - 5, animStripGUI.positionY + animStripGUI.pixelHeight - 32f, 38f, 32f), "NEW")) { int index = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).GetIndex(animStripGUI.currentCellKey) + 1; RagePixelCell cell = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).InsertCell(index, RagePixelUtil.RandomKey()); animStripGUI.currentCellKey = cell.key; RagePixelUtil.RebuildAtlas(spriteSheet, true, "AddCell"); } GUI.color = RagePixelGUIIcons.redButtonColor; if (GUI.Button(new Rect(Screen.width - 38f - 5, animStripGUI.positionY + animStripGUI.pixelHeight - 32f, 38f, 32f), "DEL")) { if (spriteSheet.GetRow(spriteSheetGUI.currentRowKey).cells.Length > 1) { if (EditorUtility.DisplayDialog("Delete selected animation frame?", "Are you sure?", "Delete", "Cancel")) { int index = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).GetIndex(animStripGUI.currentCellKey); spriteSheet.GetRow(spriteSheetGUI.currentRowKey).RemoveCellByKey(animStripGUI.currentCellKey); int newKey = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).cells[Mathf.Clamp(index, 0, spriteSheet.GetRow(spriteSheetGUI.currentRowKey).cells.Length - 1)].key; if (selectedSprite != null) { if (selectedSprite.currentCellKey == animStripGUI.currentCellKey) { selectedSprite.meshIsDirty = true; selectedSprite.currentCellKey = newKey; selectedSprite.pixelSizeX = selectedSprite.GetCurrentRow().pixelSizeX; selectedSprite.pixelSizeY = selectedSprite.GetCurrentRow().pixelSizeY; } } animStripGUI.currentCellKey = newKey; RagePixelUtil.RebuildAtlas(spriteSheet, true, "DeleteCell"); if (inspector != null) { inspector.spriteSheetGUI.isDirty = true; inspector.animStripGUI.isDirty = true; } } } else { EditorUtility.DisplayDialog("Cannot delete", "Cannot delete the last animation frame.", "OK"); } } GUI.color = Color.white; spriteSheet.GetRow(spriteSheetGUI.currentRowKey).name = EditorGUI.TextField( new Rect(x, y, Mathf.Min(350, Screen.width - x * 2), 16), "Sprite Name", spriteSheet.GetRow(spriteSheetGUI.currentRowKey).name); y += 20; EditorGUI.LabelField( new Rect(x, y, Screen.width - x * 2, 16), "Frame Index", spriteSheet.GetRow(spriteSheetGUI.currentRowKey).GetIndex(animStripGUI.currentCellKey).ToString() + " (" + (spriteSheet.GetRow(spriteSheetGUI.currentRowKey).cells.Length - 1).ToString() + ")"); y += 20; spriteSheet.GetRow(spriteSheetGUI.currentRowKey).GetCell(animStripGUI.currentCellKey).delay = EditorGUI.IntField( new Rect(x, y, Mathf.Min(200, Screen.width - x * 2), 16), "Frame Time", (int)spriteSheet.GetRow(spriteSheetGUI.currentRowKey).GetCell(animStripGUI.currentCellKey).delay); y += 20; GUILayout.Space(y + 20); int namedAnimationsFoldoutHeight = 0; showNamedAnimationsFoldout = EditorGUI.Foldout(new Rect(x, y, Screen.width - x * 2, 20), showNamedAnimationsFoldout, "Named animations"); y += 20; if (showNamedAnimationsFoldout) { GUI.color = Color.gray; x = 5; GUI.Label(new Rect(x, y, 170, 16), "Name"); x += 175; GUI.Label(new Rect(x, y, 40, 16), "Start"); x += 45; GUI.Label(new Rect(x, y, 40, 16), "End"); x += 45; GUI.Label(new Rect(x, y, 130, 16), "Type"); GUI.color = Color.white; y += 20; RagePixelAnimation[] animations = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).animations; for (int animIndex = 0; animIndex < animations.Length; animIndex++) { x = 5; animations[animIndex].name = EditorGUI.TextField(new Rect(x, y, 170, 16), animations[animIndex].name); x += 175; animations[animIndex].startIndex = EditorGUI.IntField(new Rect(x, y, 40, 16), animations[animIndex].startIndex); x += 45; animations[animIndex].endIndex = EditorGUI.IntField(new Rect(x, y, 40, 16), animations[animIndex].endIndex); x += 45; animations[animIndex].mode = (RagePixelSprite.AnimationMode)EditorGUI.EnumPopup(new Rect(x, y, 130, 16), animations[animIndex].mode); x += 135; if (GUI.Button(new Rect(x, y, 60, 16), "Delete")) { spriteSheet.GetRow(spriteSheetGUI.currentRowKey).RemoveAnimation(animIndex); } y += 20; namedAnimationsFoldoutHeight += 20; } x = 5; if (GUI.Button(new Rect(x, y, 50, 16), "Add")) { spriteSheet.GetRow(spriteSheetGUI.currentRowKey).AddAnimation(); } y += 20; namedAnimationsFoldoutHeight += 20; GUILayout.Space(namedAnimationsFoldoutHeight + 26); y += 6; } x = 5; showImportFoldout = EditorGUI.Foldout(new Rect(x, y, Screen.width - x * 2, 16), showImportFoldout, "Import"); if (showImportFoldout) { GUILayout.BeginHorizontal(); newTexture = (Texture2D)EditorGUILayout.ObjectField(" ", newTexture, typeof(Texture2D), false); GUILayout.BeginVertical(); if (GUI.Button(new Rect(x + 240f, y, 180f, 19f), "Import to selected frame")) { if (newTexture != null) { ImportSprite(SpriteSheetImportTarget.Selected); } } y += 21; if (GUI.Button(new Rect(x + 240f, y, 180f, 19f), "Import as new frame")) { if (newTexture != null) { ImportSprite(SpriteSheetImportTarget.NewFrame); } } y += 21; if (GUI.Button(new Rect(x + 240f, y, 180f, 19f), "Import as new sprite")) { if (newTexture != null) { ImportSprite(SpriteSheetImportTarget.NewSprite); } } GUILayout.EndVertical(); GUILayout.EndHorizontal(); } int oldRowKey = spriteSheetGUI.currentRowKey; animStripGUI.HandleGUIEvent(Event.current); spriteSheetGUI.HandleGUIEvent(Event.current); if (oldRowKey != spriteSheetGUI.currentRowKey) { animStripGUI.currentCellKey = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).cells[0].key; } if (animStripGUI.isDirty || spriteSheetGUI.isDirty) { Repaint(); if (inspector != null) { inspector.spriteSheetGUI.isDirty = true; inspector.animStripGUI.isDirty = true; inspector.Repaint(); } if (selectedSprite != null) { selectedSprite.meshIsDirty = true; selectedSprite.refreshMesh(); } } spriteSheetGUI.maxWidth = scenePixelWidth - 38 * 2 - 10 - spriteSheetGUI.positionX; animStripGUI.maxWidth = scenePixelWidth - 38 * 2 - 10 - animStripGUI.positionX; EditorGUI.DrawPreviewTexture(spriteSheetGUI.bounds, spriteSheetGUI.spriteSheetTexture); EditorGUI.DrawPreviewTexture(animStripGUI.bounds, animStripGUI.animStripTexture); } if (GUI.changed) { EditorUtility.SetDirty(spriteSheet); } }
public static void RebuildAtlas(RagePixelSpriteSheet spriteSheet, bool atlasIsGrowing, string caller = "nobody") { int frameCount = spriteSheet.GetTotalCellCount(); Texture2D[] textures = new Texture2D[frameCount]; RagePixelCell[] cells = new RagePixelCell[frameCount]; int index = 0; EditorUtility.SetDirty(spriteSheet); Texture2D sourceTexture = spriteSheet.atlas.GetTexture("_MainTex") as Texture2D; if (sourceTexture != null) { foreach (RagePixelRow row in spriteSheet.rows) { foreach (RagePixelCell cell in row.cells) { textures[index] = new Texture2D(row.newPixelSizeX, row.newPixelSizeY); if (cell.uv.width > 0f && cell.uv.height > 0f) { clearPixels(textures[index]); CopyPixels(sourceTexture, cell.uv, textures[index], new Rect(0f, 0f, 1f, 1f)); } else { clearPixels(textures[index]); } cells[index] = cell; index++; } } } Texture2D newAtlasTexture = new Texture2D(defaultAtlasSize, defaultAtlasSize); int atlasPadding = defaultAtlasPadding; if (textures.Length == 1) { atlasPadding = 0; } Rect[] newUvs = newAtlasTexture.PackTextures(textures, atlasPadding, 4096); if (newUvs != null) { float totalAreaOld = 0f; float totalAreaNew = 0f; for (int i = 0; i < cells.Length; i++) { totalAreaOld += cells[i].uv.height * cells[i].uv.width; } for (int i = 0; i < newUvs.Length; i++) { totalAreaNew += newUvs[i].height * newUvs[i].width; } // Checking if the PackTextures() is going to crap all over spritesheet, when going over max texture size if (!(atlasIsGrowing && totalAreaNew < totalAreaOld && sourceTexture.width * sourceTexture.height >= newAtlasTexture.width * newAtlasTexture.height)) { for (int i = 0; i < newUvs.Length && i < cells.Length; i++) { cells[i].uv = newUvs[i]; } saveAtlasPng(defaultAtlasPath, spriteSheet.atlas.name, newAtlasTexture); for (int i = 0; i < textures.Length; i++) { Object.DestroyImmediate(textures[i]); } Object.DestroyImmediate(newAtlasTexture); RagePixelSprite[] sprites = Resources.FindObjectsOfTypeAll(typeof(RagePixelSprite)) as RagePixelSprite[]; EditorUtility.SetDirty(spriteSheet); EditorUtility.SetDirty(spriteSheet.atlas); EditorUtility.SetDirty(spriteSheet.atlas.GetTexture("_MainTex")); foreach (RagePixelRow row in spriteSheet.rows) { row.pixelSizeX = row.newPixelSizeX; row.pixelSizeY = row.newPixelSizeY; } foreach (RagePixelSprite sprite in sprites) { EditorUtility.SetDirty(sprite); sprite.checkKeyIntegrity(); sprite.SnapToIntegerPosition(); sprite.refreshMesh(); } AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } else { Debug.LogError("ERROR: Too much data for max texture size (Spritesheet: " + spriteSheet.name + ")"); } } else { Debug.LogError("ERROR: Atlas PackTextures() failed (Spritesheet: " + spriteSheet.name + ")"); } }
public void OnGUI() { int y = 2; int x = 5; spriteSheet = (RagePixelSpriteSheet)EditorGUI.ObjectField( new Rect(x, y, Screen.width - x * 2, 16), "Sprite sheet", spriteSheet, typeof(RagePixelSpriteSheet), false ); y += 20; if(spriteSheet != null) { if(spriteSheet != spriteSheetGUI.spriteSheet) { spriteSheetGUI.spriteSheet = spriteSheet; spriteSheetGUI.currentRowKey = spriteSheet.rows[0].key; copySpriteSheetGUI.spriteSheet = spriteSheet; copySpriteSheetGUI.currentRowKey = spriteSheet.rows[0].key; animStripGUI.currentCellKey = spriteSheet.rows[0].cells[0].key; } spriteSheetGUI.positionX = x; spriteSheetGUI.positionY = y; animStripGUI.positionX = x; animStripGUI.positionY = spriteSheetGUI.positionY + spriteSheetGUI.pixelHeight + 5; GUI.color = RagePixelGUIIcons.greenButtonColor; if(GUI.Button(new Rect(Screen.width - 38f * 2 - 5, spriteSheetGUI.positionY + spriteSheetGUI.pixelHeight - 32f, 38f, 32f), "NEW")) { int index = spriteSheet.GetIndex(spriteSheetGUI.currentRowKey); RagePixelRow row = spriteSheet.AddRow( RagePixelUtil.RandomKey(), index + 1, spriteSheet.GetRow(spriteSheetGUI.currentRowKey).pixelSizeX, spriteSheet.GetRow(spriteSheetGUI.currentRowKey).pixelSizeY ); RagePixelCell cell = row.InsertCell(0, RagePixelUtil.RandomKey()); spriteSheetGUI.currentRowKey = row.key; animStripGUI.currentCellKey = cell.key; RagePixelUtil.RebuildAtlas(spriteSheet, true, "AddRow"); } GUI.color = RagePixelGUIIcons.redButtonColor; if(GUI.Button(new Rect(Screen.width - 38f - 5, spriteSheetGUI.positionY + spriteSheetGUI.pixelHeight - 32f, 38f, 32f), "DEL")) { if(spriteSheet.rows.Length > 1) { if(EditorUtility.DisplayDialog("Delete selected sprite?", "Are you sure?", "Delete", "Cancel")) { int index = spriteSheet.GetIndex(spriteSheetGUI.currentRowKey); spriteSheet.RemoveRowByKey(spriteSheetGUI.currentRowKey); int newKey = spriteSheet.rows[Mathf.Clamp(index, 0, spriteSheet.rows.Length - 1)].key; if(selectedSprite != null) { if(selectedSprite.currentRowKey == spriteSheetGUI.currentRowKey) { selectedSprite.meshIsDirty = true; selectedSprite.currentRowKey = newKey; selectedSprite.pixelSizeX = selectedSprite.GetCurrentRow().pixelSizeX; selectedSprite.pixelSizeY = selectedSprite.GetCurrentRow().pixelSizeY; } } spriteSheetGUI.currentRowKey = newKey; animStripGUI.currentCellKey = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).cells[0].key; RagePixelUtil.RebuildAtlas(spriteSheet, false, "DeleteRow"); if(inspector != null) { inspector.spriteSheetGUI.isDirty = true; inspector.animStripGUI.isDirty = true; } } } else { EditorUtility.DisplayDialog("Cannot delete", "Cannot delete the last sprite.", "OK"); } } y += spriteSheetGUI.pixelHeight + animStripGUI.pixelHeight + 10; GUI.color = RagePixelGUIIcons.greenButtonColor; if(GUI.Button(new Rect(Screen.width - 38f * 2 - 5, animStripGUI.positionY + animStripGUI.pixelHeight - 32f, 38f, 32f), "NEW")) { int index = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).GetIndex(animStripGUI.currentCellKey) + 1; RagePixelCell cell = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).InsertCell(index, RagePixelUtil.RandomKey()); animStripGUI.currentCellKey = cell.key; RagePixelUtil.RebuildAtlas(spriteSheet, true, "AddCell"); } GUI.color = RagePixelGUIIcons.redButtonColor; if(GUI.Button(new Rect(Screen.width - 38f - 5, animStripGUI.positionY + animStripGUI.pixelHeight - 32f, 38f, 32f), "DEL")) { if(spriteSheet.GetRow(spriteSheetGUI.currentRowKey).cells.Length > 1) { if(EditorUtility.DisplayDialog("Delete selected animation frame?", "Are you sure?", "Delete", "Cancel")) { int index = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).GetIndex(animStripGUI.currentCellKey); spriteSheet.GetRow(spriteSheetGUI.currentRowKey).RemoveCellByKey(animStripGUI.currentCellKey); int newKey = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).cells[Mathf.Clamp(index, 0, spriteSheet.GetRow(spriteSheetGUI.currentRowKey).cells.Length - 1)].key; if(selectedSprite != null) { if(selectedSprite.currentCellKey == animStripGUI.currentCellKey) { selectedSprite.meshIsDirty = true; selectedSprite.currentCellKey = newKey; selectedSprite.pixelSizeX = selectedSprite.GetCurrentRow().pixelSizeX; selectedSprite.pixelSizeY = selectedSprite.GetCurrentRow().pixelSizeY; } } animStripGUI.currentCellKey = newKey; RagePixelUtil.RebuildAtlas(spriteSheet, true, "DeleteCell"); if(inspector != null) { inspector.spriteSheetGUI.isDirty = true; inspector.animStripGUI.isDirty = true; } } } else { EditorUtility.DisplayDialog("Cannot delete", "Cannot delete the last animation frame.", "OK"); } } GUI.color = Color.white; if(spriteSheet.GetRow(spriteSheetGUI.currentRowKey).name == null) { spriteSheet.GetRow(spriteSheetGUI.currentRowKey).name = ""; } spriteSheet.GetRow(spriteSheetGUI.currentRowKey).name = EditorGUI.TextField( new Rect(x, y, Mathf.Min(350, Screen.width - x * 2), 16), "Sprite Name", spriteSheet.GetRow(spriteSheetGUI.currentRowKey).name); y += 20; RagePixelCell selectedCell = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).GetCell(animStripGUI.currentCellKey); EditorGUI.LabelField( new Rect(x, y, Screen.width - x * 2, 16), "Frame Index", spriteSheet.GetRow(spriteSheetGUI.currentRowKey).GetIndex(animStripGUI.currentCellKey).ToString() + " (" + (spriteSheet.GetRow(spriteSheetGUI.currentRowKey).cells.Length - 1).ToString() + ")" + (selectedCell.importAssetPath == "" ? "" : (" - " + selectedCell.importAssetPath))); y += 20; spriteSheet.GetRow(spriteSheetGUI.currentRowKey).GetCell(animStripGUI.currentCellKey).delay = EditorGUI.IntField( new Rect(x, y, Mathf.Min(200, Screen.width - x * 2), 16), "Frame Time", (int)spriteSheet.GetRow(spriteSheetGUI.currentRowKey).GetCell(animStripGUI.currentCellKey).delay); y += 20; GUILayout.Space(y + 20); int rangeAnimationsFoldoutHeight = 0; showRangeAnimationsFoldout = EditorGUI.Foldout(new Rect(x, y, Screen.width - x * 2, 20), showRangeAnimationsFoldout, "Range animations"); y += 20; if(showRangeAnimationsFoldout) { GUI.color = Color.gray; x = 5; GUI.Label(new Rect(x, y, 170, 16), "Name"); x += 175; GUI.Label(new Rect(x, y, 40, 16), "Start"); x += 45; GUI.Label(new Rect(x, y, 40, 16), "End"); x += 45; GUI.Label(new Rect(x, y, 130, 16), "Type"); GUI.color = Color.white; y += 20; RagePixelAnimation[] animations = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).animations; for(int animIndex = 0; animIndex < animations.Length; animIndex++) { if(animations[animIndex].frameMode != RagePixelSprite.FrameMode.Range) { continue; } x = 5; animations[animIndex].name = EditorGUI.TextField(new Rect(x, y, 170, 16), animations[animIndex].name); x += 175; animations[animIndex].startIndex = EditorGUI.IntField(new Rect(x, y, 40, 16), animations[animIndex].startIndex); x += 45; animations[animIndex].endIndex = EditorGUI.IntField(new Rect(x, y, 40, 16), animations[animIndex].endIndex); x += 45; animations[animIndex].mode = (RagePixelSprite.AnimationMode)EditorGUI.EnumPopup(new Rect(x, y, 130, 16), animations[animIndex].mode); x += 135; if(GUI.Button(new Rect(x, y, 60, 16), "Delete")) { spriteSheet.GetRow(spriteSheetGUI.currentRowKey).RemoveAnimation(animIndex); } y += 20; rangeAnimationsFoldoutHeight += 20; } x = 5; if(GUI.Button(new Rect(x, y, 50, 16), "Add")) { RagePixelAnimation anim = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).AddAnimation(); anim.frameMode = RagePixelSprite.FrameMode.Range; } y += 20; rangeAnimationsFoldoutHeight += 20; GUILayout.Space(rangeAnimationsFoldoutHeight + 26); y += 6; } int sequenceAnimationsFoldoutHeight = 0; showSequenceAnimationsFoldout = EditorGUI.Foldout(new Rect(x, y, Screen.width - x * 2, 20), showSequenceAnimationsFoldout, "Sequence animations"); y += 20; if(showSequenceAnimationsFoldout) { GUI.color = Color.gray; x = 5; GUI.Label(new Rect(x, y, 170, 16), "Name"); x += 175; GUI.Label(new Rect(x, y, 40, 16), "Type"); x += 135; GUI.Label(new Rect(x, y, 60, 16), "Frames"); GUI.color = Color.white; y += 20; RagePixelAnimation[] animations = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).animations; for(int animIndex = 0; animIndex < animations.Length; animIndex++) { if(animations[animIndex].frameMode != RagePixelSprite.FrameMode.Sequence) { continue; } x = 5; animations[animIndex].name = EditorGUI.TextField(new Rect(x, y, 170, 16), animations[animIndex].name); x += 175; animations[animIndex].mode = (RagePixelSprite.AnimationMode)EditorGUI.EnumPopup(new Rect(x, y, 130, 16), animations[animIndex].mode); x += 135; //frames if(animations[animIndex].frames == null || animations[animIndex].frames.Length == 0) { animations[animIndex].frames = new int[]{0}; } for(int i = 0; i < animations[animIndex].frames.Length; i++) { animations[animIndex].frames[i] = EditorGUI.IntField(new Rect(x, y, 24, 16), animations[animIndex].frames[i]); x += 28; } if(GUI.Button(new Rect(x, y, 20, 16), "-")) { //reduce length by 1 Array.Resize(ref animations[animIndex].frames, animations[animIndex].frames.Length - 1); } x += 22; if(GUI.Button(new Rect(x, y, 20, 16), "+")) { //increase length by 1 Array.Resize(ref animations[animIndex].frames, animations[animIndex].frames.Length + 1); } x += 28; if(GUI.Button(new Rect(x, y, 60, 16), "Delete")) { spriteSheet.GetRow(spriteSheetGUI.currentRowKey).RemoveAnimation(animIndex); } y += 20; sequenceAnimationsFoldoutHeight += 20; } x = 5; if(GUI.Button(new Rect(x, y, 50, 16), "Add")) { RagePixelAnimation anim = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).AddAnimation(); anim.frameMode = RagePixelSprite.FrameMode.Sequence; } y += 20; sequenceAnimationsFoldoutHeight += 20; GUILayout.Space(sequenceAnimationsFoldoutHeight + 26); y += 6; } x = 5; showCopyAnimationsFoldout = EditorGUI.Foldout(new Rect(x, y, Screen.width - x * 2, 16), showCopyAnimationsFoldout, "Copy Animations"); if(showCopyAnimationsFoldout) { y += 20; GUI.Label(new Rect(x, y, Screen.width - x * 2, 16), "Other Sprite"); y += 20; copySpriteSheetGUI.positionX = x; copySpriteSheetGUI.positionY = y; y += copySpriteSheetGUI.pixelHeight + 10; GUI.enabled = spriteSheetGUI.currentRowKey != copySpriteSheetGUI.currentRowKey; if(GUI.Button(new Rect(x, y, 180f, 19f), "Copy From Other")) { RagePixelRow thisRow = spriteSheet.GetRow(spriteSheetGUI.currentRowKey); RagePixelRow otherRow = spriteSheet.GetRow(copySpriteSheetGUI.currentRowKey); thisRow.CopyAnimationsFrom(otherRow); } if(GUI.Button(new Rect(x + 190, y, 180f, 19f), "Copy To Other")) { RagePixelRow thisRow = spriteSheet.GetRow(spriteSheetGUI.currentRowKey); RagePixelRow otherRow = spriteSheet.GetRow(copySpriteSheetGUI.currentRowKey); otherRow.CopyAnimationsFrom(thisRow); } GUI.enabled = true; y += 21; } y += 20; showImportFoldout = EditorGUI.Foldout(new Rect(x, y, Screen.width - x * 2, 16), showImportFoldout, "Import"); if(showImportFoldout) { GUILayout.Space(24); GUILayout.BeginHorizontal(); newTexture = (Texture2D)EditorGUILayout.ObjectField(" ", newTexture, typeof(Texture2D), false); GUILayout.BeginVertical(); y += 10; if(GUI.Button(new Rect(x + 240f, y, 180f, 19f), "Import to selected frame")) { if(newTexture != null) { ImportSprite(SpriteSheetImportTarget.Selected); } } y += 21; if(GUI.Button(new Rect(x + 240f, y, 180f, 19f), "Import as new frame")) { if(newTexture != null) { ImportSprite(SpriteSheetImportTarget.NewFrame); } } y += 21; if(GUI.Button(new Rect(x + 240f, y, 180f, 19f), "Import as new sprite")) { if(newTexture != null) { ImportSprite(SpriteSheetImportTarget.NewSprite); } } y += 21; if(GUI.Button(new Rect(x + 240f, y, 180f, 19f), "Import spritesheet")) { if(newTexture != null) { ImportSprite(SpriteSheetImportTarget.SpriteSheet); } } //sprite width, sprite height y += 21; importSpriteWidth = EditorGUI.IntField(new Rect(x + 240f, y, 180f, 19f), "Frame Width", importSpriteWidth); y += 21; importSpriteHeight = EditorGUI.IntField(new Rect(x + 240f, y, 180f, 19f), "Frame Height", importSpriteHeight); y += 21; importSpriteTopLeft = EditorGUI.Toggle(new Rect(x + 240f, y, 180f, 19f), "First Frame at Top Left", importSpriteTopLeft); GUILayout.EndVertical(); GUILayout.EndHorizontal(); } // Update references to sprites y += 20; showUpdateFoldout = EditorGUI.Foldout(new Rect(x, y, Screen.width - x * 2, 16), showUpdateFoldout, "Update"); if (showUpdateFoldout) { y += 20; GUILayout.BeginVertical(); if(GUI.Button(new Rect(5, y, 180f, 19f), "Update Selected Frame")) { UpdateSprite(SpriteSheetUpdateTarget.SelectedFrame); } /* if(GUI.Button(new Rect(190, y, 180f, 19f), "Save Frame to Source")) { if(newTexture != null) { } } */ y += 21; if(GUI.Button(new Rect(5, y, 180f, 19f), "Update Selected Sprite")) { UpdateSprite(SpriteSheetUpdateTarget.SelectedSprite); } y += 21; if(GUI.Button(new Rect(5, y, 180f, 19f), "Update All Sprites")) { UpdateSprite(SpriteSheetUpdateTarget.AllSprites); } GUILayout.EndVertical(); } int oldRowKey = spriteSheetGUI.currentRowKey; animStripGUI.HandleGUIEvent(Event.current); spriteSheetGUI.HandleGUIEvent(Event.current); if(oldRowKey != spriteSheetGUI.currentRowKey) { animStripGUI.currentCellKey = spriteSheet.GetRow(spriteSheetGUI.currentRowKey).cells[0].key; } if(animStripGUI.isDirty || spriteSheetGUI.isDirty) { Repaint(); if(inspector != null) { inspector.spriteSheetGUI.isDirty = true; inspector.animStripGUI.isDirty = true; inspector.Repaint(); } if(selectedSprite != null) { selectedSprite.meshIsDirty = true; selectedSprite.refreshMesh(); } } spriteSheetGUI.maxWidth = scenePixelWidth - 38 * 2 - 10 - spriteSheetGUI.positionX; animStripGUI.maxWidth = scenePixelWidth - 38 * 2 - 10 - animStripGUI.positionX; EditorGUI.DrawPreviewTexture(spriteSheetGUI.bounds, spriteSheetGUI.spriteSheetTexture); EditorGUI.DrawPreviewTexture(animStripGUI.bounds, animStripGUI.animStripTexture); if(showCopyAnimationsFoldout) { copySpriteSheetGUI.HandleGUIEvent(Event.current); copySpriteSheetGUI.maxWidth = scenePixelWidth - 38 * 2 - 10 - copySpriteSheetGUI.positionX; EditorGUI.DrawPreviewTexture(copySpriteSheetGUI.bounds, copySpriteSheetGUI.spriteSheetTexture); } } if(GUI.changed) { EditorUtility.SetDirty(spriteSheet); } }