// Draw an image inside inspector // TODO: Get this working. public static void DrawImageInInspector(UPAImage img, Rect window) { for (int x = 0; x < img.width; x++) { for (int y = 0; y < img.height; y++) { // Is the rect visible on screen? if (!window.Contains(new Vector2(img.map[x + y * img.width].rect.x, img.map[x + y * img.width].rect.y)) && !window.Contains(new Vector2(img.map[x + y * img.width].rect.x + img.map[x + y * img.width].rect.width, img.map[x + y * img.width].rect.y + img.map[x + y * img.width].rect.height))) { continue; } Color c = img.map[x + y * img.width].color; float newR = c.a * c.r + (1 - c.a) * bgColor.r; float newG = c.a * c.g + (1 - c.a) * bgColor.g; float newB = c.a * c.b + (1 - c.a) * bgColor.b; Color fC = new Color(newR, newG, newB, 1); EditorGUI.DrawRect(img.map[x + y * img.width].rect, fC); } } }
void OnGUI () { GUILayout.Label ("Image to Export", EditorStyles.boldLabel); exportImg = (UPAImage)EditorGUILayout.ObjectField (exportImg, typeof(UPAImage), false); GUILayout.Label ("Export Settings", EditorStyles.boldLabel); texExtension = (TextureExtension)EditorGUILayout.EnumPopup("Save As:", texExtension); if (texExtension == TextureExtension.JPG) { #if UNITY_4_2 GUILayout.Label ("Error: Export to JPG requires Unity 4.5+"); #elif UNITY_4_3 GUILayout.Label ("Error: Export to JPG requires Unity 4.5+"); #endif GUILayout.Label ("Warning: JPG files will lose transparency."); } texType = (TextureType)EditorGUILayout.EnumPopup("Texture Type:", texType); EditorGUILayout.Space (); if ( GUILayout.Button ("Export", GUILayout.Height(30)) ) { if (exportImg == null) { EditorUtility.DisplayDialog( "Select Image", "You Must Select an Image first!", "Ok"); return; } bool succes = UPASession.ExportImage ( exportImg, texType, texExtension ); if (succes) this.Close(); UPAEditorWindow.window.Repaint(); } }
// DRAWING METHODS // Draw an image inside the editor window public static void DrawImage(UPAImage img) { Rect texPos = img.GetImgRect(); Texture2D bg = new Texture2D(1, 1); bg.SetPixel(0, 0, Color.clear); bg.Apply(); EditorGUI.DrawTextureTransparent(texPos, bg); DestroyImmediate(bg); //Calculate the final image from the layers list Texture2D _result = CalculateBlendedTex(img.layers); //Draw the image _result.SetPixel(1, 1, Color.black); GUI.DrawTexture(texPos, _result); // Draw a grid above the image (y axis first) for (int x = 0; x <= img.width; x += 1) { float posX = texPos.xMin + ((float)texPos.width / (float)img.width) * x - 0.2f; EditorGUI.DrawRect(new Rect(posX, texPos.yMin, 1, texPos.height), gridBGColor); } // Then x axis for (int y = 0; y <= img.height; y += 1) { float posY = texPos.yMin + ((float)texPos.height / (float)img.height) * y - 0.2f; EditorGUI.DrawRect(new Rect(texPos.xMin, posY, texPos.width, 1), gridBGColor); } }
public static void CreateImage(int w, int h) { string path = EditorUtility.SaveFilePanel("Create UPAImage", "Assets/", "Pixel Image.asset", "asset"); if (path == "") { return; } path = FileUtil.GetProjectRelativePath(path); UPAImage img = ScriptableObject.CreateInstance <UPAImage>(); AssetDatabase.CreateAsset(img, path); AssetDatabase.SaveAssets(); img.Init(w, h); EditorUtility.SetDirty(img); UPAEditorWindow.CurrentImg = img; EditorPrefs.SetString("currentImgPath", AssetDatabase.GetAssetPath(img)); if (UPAEditorWindow.window != null) { UPAEditorWindow.window.Repaint(); } else { UPAEditorWindow.Init(); } img.gridSpacing = 10 - Mathf.Abs(img.width - img.height) / 100f; }
public UPAImage parentImg; //编辑器里有对某个layer Copy的功能,如果Copy多个,那么有个原始的layer就是它 // Constructor public UPALayer(UPAImage img) { name = "Layer " + (img.layers.Count + 1); opacity = 1; mode = BlendMode.NORMAL; map = new Color[img.width * img.height]; tex = new Texture2D(img.width, img.height); for (int x = 0; x < img.width; x++) { for (int y = 0; y < img.height; y++) { map[x + y * img.width] = Color.clear; tex.SetPixel(x, y, Color.clear); } } tex.filterMode = FilterMode.Point; tex.Apply(); enabled = true; locked = false; parentImg = img; // Because Unity won't record map (Color[]) as an undo, // we instead register a callback to LoadMapFromTex since undoing textures works fine Undo.undoRedoPerformed += LoadMapFromTex; // subscribe to the undo event }
// DRAWING METHODS // Draw an image inside the editor window public static void DrawImage(UPAImage img) { Rect texPos = img.GetImgRect(); Texture2D bg = new Texture2D (1,1); bg.SetPixel (0,0, Color.clear); bg.Apply(); EditorGUI.DrawTextureTransparent (texPos, bg); DestroyImmediate (bg); // Draw image for (int i = 0; i < img.layers.Count; i++) { if (!img.layers[i].enabled) continue; GUI.DrawTexture (texPos, img.layers[i].tex); } // Draw a grid above the image (y axis first) for (int x = 0; x <= img.width; x += 1) { float posX = texPos.xMin + ( (float)texPos.width / (float)img.width ) * x - 0.2f; EditorGUI.DrawRect (new Rect (posX, texPos.yMin, 1, texPos.height), gridBGColor); } // Then x axis for (int y = 0; y <= img.height; y += 1) { float posY = texPos.yMin + ( (float)texPos.height / (float)img.height ) * y - 0.2f; EditorGUI.DrawRect (new Rect (texPos.xMin, posY, texPos.width, 1), gridBGColor); } }
// DRAWING METHODS // Draw an image inside the editor window public static void DrawImage (UPAImage img) { Rect texPos = img.GetImgRect(); Texture2D bg = new Texture2D (1,1); bg.SetPixel (0,0, Color.clear); bg.Apply(); EditorGUI.DrawTextureTransparent (texPos, bg); DestroyImmediate (bg); //Calculate the final image from the layers list Texture2D _result = CalculateBlendedTex(img.layers); //Draw the image _result.SetPixel(1, 1, Color.black); GUI.DrawTexture(texPos, _result); // Draw a grid above the image (y axis first) for (int x = 0; x <= img.width; x += 1) { float posX = texPos.xMin + ( (float)texPos.width / (float)img.width ) * x - 0.2f; EditorGUI.DrawRect (new Rect (posX, texPos.yMin, 1, texPos.height), gridBGColor); } // Then x axis for (int y = 0; y <= img.height; y += 1) { float posY = texPos.yMin + ( (float)texPos.height / (float)img.height ) * y - 0.2f; EditorGUI.DrawRect (new Rect (texPos.xMin, posY, texPos.width, 1), gridBGColor); } }
public static UPAImage OpenImageAtPath(string path, bool isTemplate) { string variableName; if (isTemplate) { variableName = "templateImgPath"; } else { variableName = "currentImgPath"; } if (path.Length != 0) { UPAImage img = AssetDatabase.LoadAssetAtPath(path, typeof(UPAImage)) as UPAImage; if (img == null) { EditorPrefs.SetString(variableName, ""); return(null); } //TODO: need to change this as i will be juggling lots of images for animations and templates EditorPrefs.SetString(variableName, path); return(img); } return(null); }
public static UPAImage OpenFrameAtPath(string path) { if (path.Length != 0) { UPAImage img = AssetDatabase.LoadAssetAtPath(path, typeof(UPAImage)) as UPAImage; if (img == null) { Debug.Log("image is null!?!"); } else { Debug.Log("image found!"); Debug.Log("number layers = "); Debug.Log(img.layers.Count); } return(img); } return(null); }
static UPAImage CreateAnimationFrame(int w, int h, string path) { //TODO: want to pass in the same path that the image was pulled from, so you don't have to navigate back if (path == "") { return(null); } UPAImage img = CreateUPAImage(w, h); Debug.Log("creating custom image"); Debug.Log(path); path = FileUtil.GetProjectRelativePath(path); Debug.Log(path); AssetDatabase.CreateAsset(img, path); AssetDatabase.SaveAssets(); EditorUtility.SetDirty(img); return(img); }
// Constructor public UPALayer(UPAImage img) { name = "Layer " + (img.layers.Count + 1); opacity = 1; map = new Color[img.width * img.height]; tex = new Texture2D (img.width, img.height); for (int x = 0; x < img.width; x++) { for (int y = 0; y < img.height; y++) { map[x + y * img.width] = Color.clear; tex.SetPixel (x,y, Color.clear); } } tex.filterMode = FilterMode.Point; tex.Apply (); enabled = true; parentImg = img; // Because Unity won't record map (Color[]) as an undo, // we instead register a callback to LoadMapFromTex since undoing textures works fine Undo.undoRedoPerformed += LoadMapFromTex; // subscribe to the undo event }
public override void OnInspectorGUI() { UPAImage img = (UPAImage)target; GUILayout.BeginArea(new Rect(5, 53, Screen.width - 10, Screen.height)); if (GUILayout.Button("Open", GUILayout.Height(40))) { UPAEditorWindow.CurrentImg = UPASession.OpenImageByAsset(img); if (UPAEditorWindow.window != null) { UPAEditorWindow.window.Repaint(); } } if (GUILayout.Button("Export", GUILayout.Height(40))) { UPAExportWindow.Init(img); } GUILayout.EndArea(); Texture2D preview = UPASession.PreviewImage(img); float ratio = preview.width / preview.height; EditorGUI.DrawTextureTransparent(new Rect(5, 150, Screen.width - 10, (Screen.width - 10) * ratio), preview, ScaleMode.ScaleToFit, 0); DestroyImmediate(preview); //UPADrawer.DrawImageInInspector ( img, new Rect (50,0, Screen.width, Screen.height) ); }
public override void OnInspectorGUI() { UPAImage img = (UPAImage)target; GUILayout.BeginArea(new Rect(5, 53, Screen.width - 10, Screen.height)); if (GUILayout.Button("Open", GUILayout.Height(40))) { UPAEditorWindow.CurrentImg = UPASession.OpenImageByAsset(img); if (UPAEditorWindow.window != null) { UPAEditorWindow.window.Repaint(); } } if (GUILayout.Button("Export", GUILayout.Height(40))) { UPAExportWindow.Init(img); } GUILayout.EndArea(); //Make sure the textures are loaded img.LoadAllTexsFromMaps(); float ratio = (float)img.width / (float)img.height; EditorGUI.DrawTextureTransparent(new Rect(5, 150, Screen.width - 10, (Screen.width - 10) * ratio), img.GetFinalImage(true), ScaleMode.ScaleToFit, 0); }
void changeFrame(int direction) { if (animation == null || animation.Count == 0) { return; } if (animationIndex + direction < 0) { animationIndex = animation.Count - 1; } else if (animationIndex + direction >= animation.Count) { animationIndex = 0; } else { animationIndex += direction; } int selectedLayer = CurrentImg.selectedLayer; CurrentImg = animation[animationIndex]; CurrentImg.selectedLayer = selectedLayer; }
// DRAWING METHODS // Draw an image inside the editor window public static void DrawImage(UPAImage img) { Rect texPos = img.GetImgRect(); Texture2D bg = new Texture2D(1, 1); bg.SetPixel(0, 0, Color.clear); bg.Apply(); EditorGUI.DrawTextureTransparent(texPos, bg); DestroyImmediate(bg); // Draw image for (int i = 0; i < img.layers.Count; i++) { if (!img.layers[i].enabled) { continue; } GUI.DrawTexture(texPos, img.layers[i].tex); } // Draw a grid above the image (y axis first) for (int x = 0; x <= img.width; x += 1) { float posX = texPos.xMin + ((float)texPos.width / (float)img.width) * x - 0.2f; EditorGUI.DrawRect(new Rect(posX, texPos.yMin, 1, texPos.height), gridBGColor); } // Then x axis for (int y = 0; y <= img.height; y += 1) { float posY = texPos.yMin + ((float)texPos.height / (float)img.height) * y - 0.2f; EditorGUI.DrawRect(new Rect(texPos.xMin, posY, texPos.width, 1), gridBGColor); } }
public static bool ExportImage(UPAImage img, TextureType type, TextureExtension extension) { string path = EditorUtility.SaveFilePanel( "Export image as " + extension.ToString(), "Assets/", img.name + "." + extension.ToString().ToLower(), extension.ToString().ToLower()); if (path.Length == 0) return false; byte[] bytes; if (extension == TextureExtension.PNG) { // Encode texture into PNG bytes = img.GetFinalImage(true).EncodeToPNG(); } else { // Encode texture into JPG #if UNITY_4_2 bytes = img.GetFinalImage(true).EncodeToPNG(); #elif UNITY_4_3 bytes = img.GetFinalImage(true).EncodeToPNG(); #elif UNITY_4_5 bytes = img.GetFinalImage(true).EncodeToJPG(); #else bytes = img.GetFinalImage(true).EncodeToJPG(); #endif } path = FileUtil.GetProjectRelativePath(path); //Write to a file in the project folder File.WriteAllBytes(path, bytes); AssetDatabase.Refresh(); TextureImporter texImp = AssetImporter.GetAtPath(path) as TextureImporter; if (type == TextureType.texture) texImp.textureType = TextureImporterType.Image; else if (type == TextureType.sprite) { texImp.textureType = TextureImporterType.Sprite; #if UNITY_4_2 texImp.spritePixelsToUnits = 10; #elif UNITY_4_3 texImp.spritePixelsToUnits = 10; #elif UNITY_4_5 texImp.spritePixelsToUnits = 10; #else texImp.spritePixelsPerUnit = 10; #endif } texImp.filterMode = FilterMode.Point; texImp.textureFormat = TextureImporterFormat.AutomaticTruecolor; AssetDatabase.ImportAsset(path); return true; }
public static UPAImage CreateUPAImage(int w, int h) { UPAImage img = ScriptableObject.CreateInstance <UPAImage>(); img.Init(w, h); img.gridSpacing = 10 - Mathf.Abs(img.width - img.height) / 100f; return(img); }
public static void Init(UPAImage img) { // Get existing open window or if none, make new one window = (UPAExportWindow)EditorWindow.GetWindow (typeof (UPAExportWindow)); window.title = "Export Image"; window.position = new Rect(Screen.width/2 + 260/2f,Screen.height/2 - 80, 260, 170); window.ShowPopup(); window.exportImg = img; }
public static void Init() { // Get existing open window or if none, make new one window = (UPAEditorWindow)EditorWindow.GetWindow (typeof (UPAEditorWindow)); window.title = "Pixel Art Editor"; string path = EditorPrefs.GetString ("currentImgPath", ""); if (path.Length != 0) CurrentImg = UPASession.OpenImageAtPath (path); }
public static void Init(UPAImage img) { // Get existing open window or if none, make new one window = (UPAExportWindow)EditorWindow.GetWindow(typeof(UPAExportWindow)); window.title = "Export Image"; window.position = new Rect(Screen.width / 2 + 260 / 2f, Screen.height / 2 - 80, 260, 170); window.ShowPopup(); window.exportImg = img; }
public static UPAImage OpenImageByAsset (UPAImage img) { if (img == null) { Debug.LogWarning ("Image is null. Returning null."); EditorPrefs.SetString ("currentImgPath", ""); return null; } string path = AssetDatabase.GetAssetPath (img); EditorPrefs.SetString ("currentImgPath", path); return img; }
public static void Init() { // Get existing open window or if none, make new one window = (UPAEditorWindow)EditorWindow.GetWindow(typeof(UPAEditorWindow)); window.title = "Pixel Art Editor"; string path = EditorPrefs.GetString("currentImgPath", ""); if (path.Length != 0) { CurrentImg = UPASession.OpenImageAtPath(path); } }
static UPAImage customCreateImage(int w, int h, bool isTemplate) { //TODO: want to pass in the same path that the image was pulled from, so you don't have to navigate back string path = EditorUtility.SaveFilePanel("Create UPAImage", "Assets/", "Pixel Image.asset", "asset"); if (path == "") { return(null); } Debug.Log("creating custom image"); Debug.Log(path); path = FileUtil.GetProjectRelativePath(path); Debug.Log(path); UPAImage img = ScriptableObject.CreateInstance <UPAImage>(); AssetDatabase.CreateAsset(img, path); AssetDatabase.SaveAssets(); img.Init(w, h); EditorUtility.SetDirty(img); if (isTemplate) { EditorPrefs.SetString("templateImgPath", AssetDatabase.GetAssetPath(img)); UPAEditorWindow.TemplateImage = img; } else { EditorPrefs.SetString("currentImgPath", AssetDatabase.GetAssetPath(img)); UPAEditorWindow.CurrentImg = img; } if (UPAEditorWindow.window != null) { UPAEditorWindow.window.Repaint(); } else { UPAEditorWindow.Init(); } img.gridSpacing = 10 - Mathf.Abs(img.width - img.height) / 100f; return(img); }
public static UPAImage OpenImageByAsset(UPAImage img) { if (img == null) { Debug.LogWarning("Image is null. Returning null."); EditorPrefs.SetString("currentImgPath", ""); return(null); } string path = AssetDatabase.GetAssetPath(img); EditorPrefs.SetString("currentImgPath", path); return(img); }
public static UPAImage OpenImage() { //打开操作系统的文件框来选择导入的图片或者.asset数据 string path = EditorUtility.OpenFilePanel( "Find an Image (.asset | .png | .jpg)", "Assets/", "Image Files;*.asset;*.jpg;*.png"); if (path.Length != 0) { // Check if the loaded file is an Asset or Image //如果是数据,直接反序列化即可 if (path.EndsWith(".asset")) { path = FileUtil.GetProjectRelativePath(path); UPAImage img = AssetDatabase.LoadAssetAtPath(path, typeof(UPAImage)) as UPAImage; //*.asset数据本身就是序列化的UPAImage EditorPrefs.SetString("currentImgPath", path); return(img); } //如果是图片,那么需要根据图片来构造UPAImage数据结构 else { // Load Texture from file Texture2D tex = LoadImageFromFile(path); // Create a new Image with textures dimensions UPAImage img = CreateImage(tex.width, tex.height); // Set pixel colors if (img.layers != null && img.layers.Count > 0) { img.layers[0].tex = tex; img.layers[0].tex.filterMode = FilterMode.Point; img.layers[0].tex.Apply(); for (int x = 0; x < img.width; x++) { for (int y = 0; y < img.height; y++) { //map逻辑上是一个一维数组,可用二维的方式来访问。 //一个图像按照左到右,上到下和map数组对应。 //这样就可以根据屏幕的触摸坐标来直接获取map数组对应的像素 img.layers[0].map[x + y * tex.width] = tex.GetPixel(x, tex.height - 1 - y); } } } } } return(null); }
public static void Init () { // Get existing open window or if none, make new one window = (UPAEditorWindow)EditorWindow.GetWindow (typeof (UPAEditorWindow)); #if UNITY_4_3 window.title = "Pixel Art Editor"; #elif UNITY_4_6 window.title = "Pixel Art Editor"; #else window.titleContent = new GUIContent ("Pixel Art Editor"); #endif string path = EditorPrefs.GetString ("currentImgPath", ""); if (path.Length != 0) CurrentImg = UPASession.OpenImageAtPath (path); }
public static UPAImage OpenImage() { string path = EditorUtility.OpenFilePanel( "Find a UPAImage (.asset)", "Assets/", "asset"); if (path.Length != 0) { path = FileUtil.GetProjectRelativePath(path); UPAImage img = AssetDatabase.LoadAssetAtPath(path, typeof(UPAImage)) as UPAImage; EditorPrefs.SetString("currentImgPath", path); return(img); } return(null); }
public static UPAImage OpenImageAtPath(string path) { if (path.Length != 0) { UPAImage img = AssetDatabase.LoadAssetAtPath(path, typeof(UPAImage)) as UPAImage; if (img == null) { EditorPrefs.SetString("currentImgPath", ""); return(null); } EditorPrefs.SetString("currentImgPath", path); return(img); } return(null); }
public static Texture2D PreviewImage(UPAImage img) { Texture2D tex = new Texture2D(img.width, img.height, TextureFormat.RGBA32, false); for (int x = 0; x < img.width; x++) { for (int y = 0; y < img.height; y++) { tex.SetPixel(x, img.height - y - 1, img.map[x + y * img.width].color); } } tex.Apply(); tex.filterMode = FilterMode.Point; return(tex); }
public static UPAImage OpenImage() { string path = EditorUtility.OpenFilePanel( "Find an Image (.asset | .png | .jpg)", "Assets/", "Image Files;*.asset;*.jpg;*.png"); Debug.Log("path running"); if (path.Length != 0) { // Check if the loaded file is an Asset or Image if (path.EndsWith(".asset")) { Debug.Log("is asset"); Debug.Log(path); path = FileUtil.GetProjectRelativePath(path); UPAImage img = AssetDatabase.LoadAssetAtPath(path, typeof(UPAImage)) as UPAImage; EditorPrefs.SetString("currentImgPath", path); return(img); } else { // Load Texture from file Texture2D tex = LoadImageFromFile(path); // Create a new Image with textures dimensions UPAImage img = CreateImage(tex.width, tex.height); // Set pixel colors img.layers[0].tex = tex; img.layers[0].tex.filterMode = FilterMode.Point; img.layers[0].tex.Apply(); for (int x = 0; x < img.width; x++) { for (int y = 0; y < img.height; y++) { img.layers[0].map[x + y * tex.width] = tex.GetPixel(x, tex.height - 1 - y); } } } } return(null); }
public static void Init() { // Get existing open window or if none, make new one window = (UPAEditorWindow)EditorWindow.GetWindow(typeof(UPAEditorWindow)); #if UNITY_4_3 window.title = "Pixel Art Editor"; #elif UNITY_4_6 window.title = "Pixel Art Editor"; #else window.titleContent = new GUIContent("Pixel Art Editor"); #endif string path = EditorPrefs.GetString("currentImgPath", ""); if (path.Length != 0) { CurrentImg = UPASession.OpenImageAtPath(path); } }
// Create clone of other UPALayer public UPALayer(UPALayer original) { name = original.name + " - Clone"; opacity = 1; mode = original.mode; map = (Color[]) original.map.Clone(); tex = new Texture2D (original.parentImg.width, original.parentImg.height); tex.SetPixels (original.tex.GetPixels ()); tex.filterMode = FilterMode.Point; tex.Apply (); enabled = true; locked = original.locked; parentImg = original.parentImg; // Because Unity won't record map (Color[]) as an undo, // we instead register a callback to LoadMapFromTex since undoing textures works fine Undo.undoRedoPerformed += LoadMapFromTex; // subscribe to the undo event }
public static void Init() { selectedTool = UPATool.Map; EditorPrefs.SetString("templateImgPath", ""); EditorPrefs.SetString("currentAnimationPath", ""); // Get existing open window or if none, make new one window = (UPAEditorWindow)EditorWindow.GetWindow(typeof(UPAEditorWindow)); #if UNITY_4_3 window.title = "Pixel Art Editor"; #elif UNITY_4_6 window.title = "Pixel Art Editor"; #else window.titleContent = new GUIContent("Pixel Art Editor"); #endif string path = EditorPrefs.GetString("currentAnimationPath", ""); Debug.Log(path); string templatePath = EditorPrefs.GetString("templateImgPath", ""); Debug.Log(templatePath); if (path.Length != 0) { Debug.Log("opening image at path"); animation = UPASession.OpenAnimationsFromFolder(false, path); //CurrentImg.initilizeAlphas(); animationIndex = 0; } if (templatePath.Length != 0) { Debug.Log("opening image at path"); TemplateImage = UPASession.OpenImageAtPath(templatePath, true); //TemplateImage.initilizeAlphas(); TemplateImage.loopThroughImage(); } }
// Create clone of other UPALayer public UPALayer(UPALayer original) { name = original.name + " - Clone"; opacity = 1; mode = original.mode; map = (Color[])original.map.Clone(); tex = new Texture2D(original.parentImg.width, original.parentImg.height); tex.SetPixels(original.tex.GetPixels()); tex.filterMode = FilterMode.Point; tex.Apply(); enabled = true; locked = original.locked; parentImg = original.parentImg; // Because Unity won't record map (Color[]) as an undo, // we instead register a callback to LoadMapFromTex since undoing textures works fine Undo.undoRedoPerformed += LoadMapFromTex; // subscribe to the undo event }
void OnGUI() { GUILayout.Label("Image to Export", EditorStyles.boldLabel); exportImg = (UPAImage)EditorGUILayout.ObjectField(exportImg, typeof(UPAImage), false); GUILayout.Label("Export Settings", EditorStyles.boldLabel); texExtension = (TextureExtension)EditorGUILayout.EnumPopup("Save As:", texExtension); if (texExtension == TextureExtension.JPG) { #if UNITY_4_2 GUILayout.Label("Error: Export to JPG requires Unity 4.5+"); #elif UNITY_4_3 GUILayout.Label("Error: Export to JPG requires Unity 4.5+"); #endif GUILayout.Label("Warning: JPG files will lose transparency."); } texType = (TextureType)EditorGUILayout.EnumPopup("Texture Type:", texType); EditorGUILayout.Space(); if (GUILayout.Button("Export", GUILayout.Height(30))) { if (exportImg == null) { EditorUtility.DisplayDialog( "Select Image", "You Must Select an Image first!", "Ok"); return; } bool succes = UPASession.ExportImage(exportImg, texType, texExtension); if (succes) { this.Close(); } UPAEditorWindow.window.Repaint(); } }
// DRAWING METHODS // Draw an image inside a window // Return true if image rects need to be updated. public static bool DrawImage(UPAImage img, Rect window) { bool updateRects = false; for (int x = 0; x < img.width; x++) { for (int y = 0; y < img.height; y++) { Vector2 rSize = new Vector2(img.map[x + y * img.width].rect.width, img.map[x + y * img.width].rect.height); if (rSize == Vector2.zero) { updateRects = true; continue; } // Is the rect visible on screen? if (!window.Contains(new Vector2(img.map[x + y * img.width].rect.x, img.map[x + y * img.width].rect.y)) && !window.Contains(new Vector2(img.map[x + y * img.width].rect.x + img.map[x + y * img.width].rect.width, img.map[x + y * img.width].rect.y + img.map[x + y * img.width].rect.height))) { continue; } Color c = img.map[x + y * img.width].color; float newR = c.a * c.r + (1 - c.a) * bgColor.r; float newG = c.a * c.g + (1 - c.a) * bgColor.g; float newB = c.a * c.b + (1 - c.a) * bgColor.b; Color fC = new Color(newR, newG, newB, 1); EditorGUI.DrawRect(img.map[x + y * img.width].rect, fC); } } return(updateRects); }
// Create clone of other UPALayer public UPALayer(UPALayer original) { name = original.name; opacity = 1; mode = original.mode; map = (Color[])original.map.Clone(); tex = new Texture2D(original.parentImg.width, original.parentImg.height); Texture2D parentTex = original.tex; if (parentTex != null) { //TODO: may be bad to turn this off tex.SetPixels(original.tex.GetPixels()); } else { Debug.LogError("no parent tex!!"); } tex.filterMode = FilterMode.Point; tex.Apply(); enabled = true; locked = original.locked; parentImg = original.parentImg; colorMapDictionary = original.colorMapDictionary; // Because Unity won't record map (Color[]) as an undo, // we instead register a callback to LoadMapFromTex since undoing textures works fine Undo.undoRedoPerformed += LoadMapFromTex; // subscribe to the undo event }
public static bool ExportImage(UPAImage img, TextureType type, TextureExtension extension) { string path = EditorUtility.SaveFilePanel( "Export image as " + extension.ToString(), "Assets/", img.name + "." + extension.ToString().ToLower(), extension.ToString().ToLower()); if (path.Length == 0) { return(false); } byte[] bytes; if (extension == TextureExtension.PNG) { // Encode texture into PNG bytes = img.GetFinalImage(true).EncodeToPNG(); } else { // Encode texture into JPG #if UNITY_4_2 bytes = img.GetFinalImage(true).EncodeToPNG(); #elif UNITY_4_3 bytes = img.GetFinalImage(true).EncodeToPNG(); #elif UNITY_4_5 bytes = img.GetFinalImage(true).EncodeToJPG(); #else bytes = img.GetFinalImage(true).EncodeToJPG(); #endif } path = FileUtil.GetProjectRelativePath(path); //Write to a file in the project folder File.WriteAllBytes(path, bytes); AssetDatabase.Refresh(); TextureImporter texImp = AssetImporter.GetAtPath(path) as TextureImporter; if (type == TextureType.texture) { texImp.textureType = TextureImporterType.Default; } else if (type == TextureType.sprite) { texImp.textureType = TextureImporterType.Sprite; #if UNITY_4_2 texImp.spritePixelsToUnits = 10; #elif UNITY_4_3 texImp.spritePixelsToUnits = 10; #elif UNITY_4_5 texImp.spritePixelsToUnits = 10; #else texImp.spritePixelsPerUnit = 10; #endif } texImp.filterMode = FilterMode.Point; texImp.textureFormat = TextureImporterFormat.AutomaticTruecolor; AssetDatabase.ImportAsset(path); return(true); }
// Draw the settings toolbar public static void DrawToolbar(Rect window, Vector2 mousePos) { // Draw toolbar bg EditorGUI.DrawRect(new Rect(0, 0, window.width, 40), toolbarColor); if (GUI.Button(new Rect(5, 4, 50, 30), "New")) { UPAImageCreationWindow.Init(); } if (GUI.Button(new Rect(60, 4, 50, 30), "Open")) { CurrentImg = UPASession.OpenImage(); if (CurrentImg == null) { return; } } if (GUI.Button(new Rect(115, 4, 50, 30), "Export")) { UPAExportWindow.Init(CurrentImg); } if (GUI.Button(new Rect(179, 6, 25, 25), "+")) { CurrentImg.gridSpacing *= 1.2f; } if (GUI.Button(new Rect(209, 6, 25, 25), "-")) { CurrentImg.gridSpacing *= 0.8f; CurrentImg.gridSpacing -= 2; } CurrentImg.selectedColor = EditorGUI.ColorField(new Rect(250, 7, 70, 25), CurrentImg.selectedColor); EditorGUI.DrawRect(new Rect(303, 7, 20, 25), toolbarColor); //bgColor = EditorGUI.ColorField (new Rect (400, 4, 70, 25), bgColor); GUI.backgroundColor = Color.white; if (CurrentImg.tool == UPATool.PaintBrush) { GUI.backgroundColor = new Color(0.7f, 0.7f, 0.7f); } if (GUI.Button(new Rect(320, 4, 60, 30), "Paint")) { CurrentImg.tool = UPATool.PaintBrush; } GUI.backgroundColor = Color.white; if (CurrentImg.tool == UPATool.BoxBrush) { GUI.backgroundColor = new Color(0.7f, 0.7f, 0.7f); } if (GUI.Button(new Rect(450, 4, 60, 30), "Box Fill")) { EditorUtility.DisplayDialog( "In Development", "This feature is currently being developed.", "Get it done please"); //tool = UPATool.BoxBrush; } GUI.backgroundColor = Color.white; if (CurrentImg.tool == UPATool.Eraser) { GUI.backgroundColor = new Color(0.7f, 0.7f, 0.7f); } if (GUI.Button(new Rect(385, 4, 60, 30), "Erase")) { CurrentImg.tool = UPATool.Eraser; } GUI.backgroundColor = Color.white; style.normal.textColor = new Color(0.7f, 0.7f, 0.7f); style.fontSize = 12; style.fontStyle = FontStyle.Normal; GUI.Label(new Rect(525, 11, 150, 30), "Use WASD to navigate.", style); if (GUI.Button(new Rect(684, 4, 55, 30), "Center")) { CurrentImg.gridOffsetX = 0; CurrentImg.gridOffsetY = 0; } CurrentImg.gridBGIndex = GUI.Toolbar(new Rect(743, 4, 130, 30), CurrentImg.gridBGIndex, gridBGStrings); if (CurrentImg.gridBGIndex == 0) { gridBGColor = Color.black; } else if (CurrentImg.gridBGIndex == 1) { gridBGColor = Color.white; } else { gridBGColor = Color.clear; } Vector2 pixelCoordinate = CurrentImg.GetReadablePixelCoordinate(mousePos); GUI.Label(new Rect(880, 11, 100, 30), "(" + (int)pixelCoordinate.x + "," + (int)pixelCoordinate.y + ")", style); if (CurrentImg.tool == UPATool.ColorPicker) { style.fontStyle = FontStyle.Bold; style.fontSize = 15; GUI.Label(new Rect(window.width / 2f - 140, 60, 100, 30), "Click on a pixel to choose a color.", style); } }
// Draw the Pixel Art Editor. // This includes both toolbar and painting area. // TODO: Add comments void OnGUI() { if (window == null) { Init(); } if (CurrentImg == null) { string curImgPath = EditorPrefs.GetString("currentImgPath", ""); if (curImgPath.Length != 0) { CurrentImg = UPASession.OpenImageAtPath(curImgPath); return; } if (GUI.Button(new Rect(window.position.width / 2f - 140, window.position.height / 2f - 25, 130, 50), "New Image")) { UPAImageCreationWindow.Init(); } if (GUI.Button(new Rect(window.position.width / 2f + 10, window.position.height / 2f - 25, 130, 50), "Open Image")) { CurrentImg = UPASession.OpenImage(); return; } return; } // Init the textures correctly, won't cost performance if nothing to load CurrentImg.LoadAllTexsFromMaps(); EditorGUI.DrawRect(window.position, new Color32(30, 30, 30, 255)); #region Event handling Event e = Event.current; //Init event handler //Capture mouse position Vector2 mousePos = e.mousePosition; // If key is pressed if (e.button == 0) { // Mouse buttons if (e.isMouse && mousePos.y > 40 && e.type != EventType.mouseUp) { if (!UPADrawer.GetLayerPanelRect(window.position).Contains(mousePos)) { if (tool == UPATool.Eraser) { CurrentImg.SetPixelByPos(Color.clear, mousePos, CurrentImg.selectedLayer); } else if (tool == UPATool.PaintBrush) { CurrentImg.SetPixelByPos(selectedColor, mousePos, CurrentImg.selectedLayer); } else if (tool == UPATool.BoxBrush) { Debug.Log("TODO: Add Box Brush tool."); } else if (tool == UPATool.ColorPicker) { Vector2 pCoord = CurrentImg.GetPixelCoordinate(mousePos); Color? newColor = CurrentImg.GetBlendedPixel((int)pCoord.x, (int)pCoord.y); if (newColor != null && newColor != Color.clear) { selectedColor = (Color)newColor; } tool = lastTool; } } } // Key down if (e.type == EventType.keyDown) { if (e.keyCode == KeyCode.W) { gridOffsetY += 20f; } if (e.keyCode == KeyCode.S) { gridOffsetY -= 20f; } if (e.keyCode == KeyCode.A) { gridOffsetX += 20f; } if (e.keyCode == KeyCode.D) { gridOffsetX -= 20f; } if (e.keyCode == KeyCode.Alpha1) { tool = UPATool.PaintBrush; } if (e.keyCode == KeyCode.Alpha2) { tool = UPATool.Eraser; } if (e.keyCode == KeyCode.P) { lastTool = tool; tool = UPATool.ColorPicker; } if (e.keyCode == KeyCode.UpArrow) { gridSpacing *= 1.2f; } if (e.keyCode == KeyCode.DownArrow) { gridSpacing *= 0.8f; gridSpacing -= 2; } } if (e.control) { if (lastTool == UPATool.Empty) { lastTool = tool; tool = UPATool.Eraser; } } else if (e.type == EventType.keyUp && e.keyCode == KeyCode.LeftControl) { if (lastTool != UPATool.Empty) { tool = lastTool; lastTool = UPATool.Empty; } } } // TODO: Better way of doing this? // Why does it behave so weirdly with my mac tablet. if (e.type == EventType.scrollWheel) { gridSpacing -= e.delta.y; } #endregion // DRAW IMAGE UPADrawer.DrawImage(CurrentImg); UPADrawer.DrawToolbar(window.position, mousePos); UPADrawer.DrawLayerPanel(window.position); e.Use(); // Release event handler }
// Draw the Pixel Art Editor. // This includes both toolbar and painting area. // TODO: Add comments void OnGUI () { if (window == null) Init (); if (CurrentImg == null) { string curImgPath = EditorPrefs.GetString ("currentImgPath", ""); if (curImgPath.Length != 0) { CurrentImg = UPASession.OpenImageAtPath (curImgPath); return; } if ( GUI.Button (new Rect (window.position.width / 2f - 140, window.position.height /2f - 25, 130, 50), "New Image") ) { UPAImageCreationWindow.Init (); } if ( GUI.Button (new Rect (window.position.width / 2f + 10, window.position.height /2f - 25, 130, 50), "Open Image") ) { CurrentImg = UPASession.OpenImage (); return; } return; } // Init the textures correctly, won't cost performance if nothing to load CurrentImg.LoadAllTexsFromMaps(); EditorGUI.DrawRect (window.position, new Color32 (30,30,30,255)); #region Event handling Event e = Event.current; //Init event handler //Capture mouse position Vector2 mousePos = e.mousePosition; // If key is pressed if (e.button == 0) { // Mouse buttons if (e.isMouse && mousePos.y > 40 && e.type != EventType.mouseUp) { if (!UPADrawer.GetLayerPanelRect (window.position).Contains (mousePos)) { if (tool == UPATool.Eraser) CurrentImg.SetPixelByPos (Color.clear, mousePos, CurrentImg.selectedLayer); else if (tool == UPATool.PaintBrush) CurrentImg.SetPixelByPos (selectedColor, mousePos, CurrentImg.selectedLayer); else if (tool == UPATool.BoxBrush) Debug.Log ("TODO: Add Box Brush tool."); else if (tool == UPATool.ColorPicker){ Vector2 pCoord = CurrentImg.GetPixelCoordinate (mousePos); Color? newColor = CurrentImg.GetBlendedPixel( (int)pCoord.x, (int)pCoord.y ); if (newColor != null && newColor != Color.clear){ selectedColor = (Color)newColor; } tool = lastTool; } } } // Key down if (e.type == EventType.keyDown) { if (e.keyCode == KeyCode.W) { gridOffsetY += 20f; } if (e.keyCode == KeyCode.S) { gridOffsetY -= 20f; } if (e.keyCode == KeyCode.A) { gridOffsetX += 20f; } if (e.keyCode == KeyCode.D) { gridOffsetX -= 20f; } if (e.keyCode == KeyCode.Alpha1) { tool = UPATool.PaintBrush; } if (e.keyCode == KeyCode.Alpha2) { tool = UPATool.Eraser; } if (e.keyCode == KeyCode.P) { lastTool = tool; tool = UPATool.ColorPicker; } if (e.keyCode == KeyCode.UpArrow) { gridSpacing *= 1.2f; } if (e.keyCode == KeyCode.DownArrow) { gridSpacing *= 0.8f; gridSpacing -= 2; } } if (e.control) { if (lastTool == UPATool.Empty) { lastTool = tool; tool = UPATool.Eraser; } } else if (e.type == EventType.keyUp && e.keyCode == KeyCode.LeftControl) { if (lastTool != UPATool.Empty) { tool = lastTool; lastTool = UPATool.Empty; } } } // TODO: Better way of doing this? // Why does it behave so weirdly with my mac tablet. if (e.type == EventType.scrollWheel) { gridSpacing -= e.delta.y; } #endregion // DRAW IMAGE UPADrawer.DrawImage ( CurrentImg ); UPADrawer.DrawToolbar (window.position, mousePos); UPADrawer.DrawLayerPanel ( window.position ); e.Use(); // Release event handler }
// Draw the settings toolbar public static void DrawToolbar (Rect window, Vector2 mousePos) { // Draw toolbar bg EditorGUI.DrawRect ( new Rect (0,0, window.width, 40), toolbarColor ); if ( GUI.Button (new Rect (5, 4, 50, 30), "New") ) { UPAImageCreationWindow.Init (); } if ( GUI.Button (new Rect (60, 4, 50, 30), "Open") ) { CurrentImg = UPASession.OpenImage (); if (CurrentImg == null) return; } if ( GUI.Button (new Rect (115, 4, 50, 30), "Export") ) { UPAExportWindow.Init(CurrentImg); } if (GUI.Button (new Rect (179, 6, 25, 25), "+")) { CurrentImg.gridSpacing *= 1.2f; } if (GUI.Button (new Rect (209, 6, 25, 25), "-")) { CurrentImg.gridSpacing *= 0.8f; CurrentImg.gridSpacing -= 2; } CurrentImg.selectedColor = EditorGUI.ColorField (new Rect (250, 7, 70, 25), CurrentImg.selectedColor); EditorGUI.DrawRect ( new Rect (303, 7, 20, 25), toolbarColor ); //bgColor = EditorGUI.ColorField (new Rect (400, 4, 70, 25), bgColor); GUI.backgroundColor = Color.white; if (CurrentImg.tool == UPATool.PaintBrush) GUI.backgroundColor = new Color (0.7f, 0.7f, 0.7f); if (GUI.Button (new Rect (320, 4, 60, 30), "Paint")) { CurrentImg.tool = UPATool.PaintBrush; } GUI.backgroundColor = Color.white; if (CurrentImg.tool == UPATool.BoxBrush) GUI.backgroundColor = new Color (0.7f, 0.7f, 0.7f); if (GUI.Button (new Rect (450, 4, 60, 30), "Box Fill")) { EditorUtility.DisplayDialog( "In Development", "This feature is currently being developed.", "Get it done please"); //tool = UPATool.BoxBrush; } GUI.backgroundColor = Color.white; if (CurrentImg.tool == UPATool.Eraser) GUI.backgroundColor = new Color (0.7f, 0.7f, 0.7f); if (GUI.Button (new Rect (385, 4, 60, 30), "Erase")) { CurrentImg.tool = UPATool.Eraser; } GUI.backgroundColor = Color.white; style.normal.textColor = new Color (0.7f, 0.7f, 0.7f); style.fontSize = 12; style.fontStyle = FontStyle.Normal; GUI.Label (new Rect (525, 11, 150, 30), "Use WASD to navigate.", style); if (GUI.Button (new Rect (684, 4, 55, 30), "Center")) { CurrentImg.gridOffsetX = 0; CurrentImg.gridOffsetY = 0; } CurrentImg.gridBGIndex = GUI.Toolbar (new Rect (743, 4, 130, 30), CurrentImg.gridBGIndex, gridBGStrings); if (CurrentImg.gridBGIndex == 0) { gridBGColor = Color.black; } else if (CurrentImg.gridBGIndex == 1) { gridBGColor = Color.white; } else { gridBGColor = Color.clear; } Vector2 pixelCoordinate = CurrentImg.GetReadablePixelCoordinate (mousePos); GUI.Label (new Rect (880, 11, 100, 30), "(" + (int)pixelCoordinate.x + "," + (int)pixelCoordinate.y + ")", style); if (CurrentImg.tool == UPATool.ColorPicker) { style.fontStyle = FontStyle.Bold; style.fontSize = 15; GUI.Label (new Rect (window.width/2f - 140, 60, 100, 30), "Click on a pixel to choose a color.", style); } }