public override void OnInspectorGUI() { _ProFlareAtlas = target as ProFlareAtlas; CheckUndo(); // base.OnInspectorGUI(); FlareEditorHelper.DrawGuiDivider(); GUIStyle title = FlareEditorHelper.TitleStyle(); GUIStyle thinButton = FlareEditorHelper.ThinButtonStyle(); GUIStyle enumDropDown = FlareEditorHelper.EnumStyleButton(); GUIStyle redButton = FlareEditorHelper.ThinButtonRedStyle(); EditorGUILayout.LabelField("ProFlare Atlas Editor", title); GUILayout.Space(10f); _ProFlareAtlas.texture = EditorGUILayout.ObjectField("Flare Atlas Texture", _ProFlareAtlas.texture, typeof(Texture2D), false) as Texture2D; if (_ProFlareAtlas.texture == null) { EditorGUILayout.HelpBox("Assign a texture to the atlas.", MessageType.Warning, true); return; } TextAsset ta = EditorGUILayout.ObjectField("Atlas JSON Import", null, typeof(TextAsset), false) as TextAsset; if (ta != null) { FlareJson.LoadSpriteData(_ProFlareAtlas, ta); Updated = true; } FlareEditorHelper.DrawGuiDivider(); EditorGUILayout.LabelField("Atlas Elements", title); GUILayout.Space(6f); EditorGUILayout.BeginHorizontal(); if (_ProFlareAtlas.elementsList.Count < 1) { EditorGUILayout.HelpBox("No Elements in flare atlas", MessageType.Warning, true); } else { _ProFlareAtlas.elementNameList = new string[_ProFlareAtlas.elementsList.Count]; for (int i = 0; i < _ProFlareAtlas.elementNameList.Length; i++) { _ProFlareAtlas.elementNameList[i] = _ProFlareAtlas.elementsList[i].name; } int _ProFlareAtlasElementNumber = EditorGUILayout.Popup(_ProFlareAtlas.elementNumber, _ProFlareAtlas.elementNameList, enumDropDown); if (_ProFlareAtlasElementNumber != _ProFlareAtlas.elementNumber) { _ProFlareAtlas.elementNumber = _ProFlareAtlasElementNumber; renameString = _ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].name; } if (GUILayout.Button("EDIT", thinButton)) { Updated = true; if (_ProFlareAtlas.editElements) { _ProFlareAtlas.editElements = false; } else { _ProFlareAtlas.editElements = true; } } } if (GUILayout.Button("ADD NEW", thinButton)) { _ProFlareAtlas.editElements = true; ProFlareAtlas.Element element = new ProFlareAtlas.Element(); element.name = "New Element " + _ProFlareAtlas.elementsList.Count; renameString = element.name; element.Imported = false; _ProFlareAtlas.elementsList.Add(element); _ProFlareAtlas.elementNumber = _ProFlareAtlas.elementsList.Count - 1; Updated = true; } EditorGUILayout.EndHorizontal(); if (_ProFlareAtlas.elementsList.Count < 1) { return; } EditorGUILayout.BeginVertical("box"); GUILayout.Space(20f); Rect lastRect = GUILayoutUtility.GetLastRect(); Rect outerRect2 = new Rect(lastRect.center.x, 0 + lastRect.yMin, 200, 200); if (_ProFlareAtlas.elementsList.Count > 0) { GUI.DrawTextureWithTexCoords(outerRect2, _ProFlareAtlas.texture, _ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].UV, false); GUILayout.Space(200f); } GUI.enabled = _ProFlareAtlas.editElements; if (_ProFlareAtlas.editElements) { int extra = 0; #if UNITY_4_3 extra = 10; #endif Rect outerRect3 = new Rect(107 + extra, lastRect.yMin, 0.5f, 200); Rect rect = new Rect(0, 0, 1, 1); GUI.DrawTextureWithTexCoords(outerRect3, EditorGUIUtility.whiteTexture, rect, false); Rect outerRect4 = new Rect(7 + extra, 100 + lastRect.yMin, 200, 0.5f); GUI.DrawTextureWithTexCoords(outerRect4, EditorGUIUtility.whiteTexture, rect, true); } GUILayout.BeginHorizontal(); if (!_ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].Imported) { renameString = EditorGUILayout.TextField("Name", renameString); if (GUILayout.Button("RENAME")) { Updated = true; _ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].name = renameString; } } else { EditorGUILayout.LabelField("Name - " + renameString); } GUILayout.EndHorizontal(); EditorGUILayout.Toggle("Imported :", _ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].Imported); float width = EditorGUILayout.Slider("Width", _ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].UV.width, 0f, 1f); float height = EditorGUILayout.Slider("Height", _ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].UV.height, 0f, 1f); float CenterX = EditorGUILayout.Slider("Center X", _ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].UV.center.x, 0f, 1f); float CenterY = EditorGUILayout.Slider("Center Y", _ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].UV.center.y, 0f, 1f); float xMin = _ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].UV.xMin; float yMin = _ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].UV.yMin; Rect newRect = new Rect(xMin, yMin, width, height); newRect.center = new Vector2(CenterX, CenterY); GUILayout.Space(40f); _ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].UV = newRect; if (GUILayout.Button("DELETE ELEMENT", redButton)) { Updated = true; _ProFlareAtlas.elementsList.Remove(_ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber]); _ProFlareAtlas.elementNumber = 0; } EditorGUILayout.EndVertical(); GUI.enabled = true; if (GUI.changed || Updated) { Updated = false; guiChanged = true; EditorUtility.SetDirty(target); } FlareEditorHelper.DrawGuiDivider(); }
/// <summary> /// Parse the specified JSon file, loading sprite information for the specified atlas. /// </summary> public static void LoadSpriteData (ProFlareAtlas atlas, TextAsset asset) { if (asset == null || atlas == null) return; string jsonString = asset.text; Hashtable decodedHash = jsonDecode(jsonString) as Hashtable; if (decodedHash == null) { Debug.LogWarning("Unable to parse Json file: " + asset.name); return; } List<ProFlareAtlas.Element> oldElements = atlas.elementsList; atlas.elementsList = new List<ProFlareAtlas.Element>(); Vector2 TextureScale = Vector2.one; //Find Texture Size Hashtable meta = (Hashtable)decodedHash["meta"]; foreach (System.Collections.DictionaryEntry item in meta) { if(item.Key.ToString() == "size"){ Hashtable sizeTable = (Hashtable)item.Value; TextureScale.x = int.Parse(sizeTable["w"].ToString()); TextureScale.y = int.Parse(sizeTable["h"].ToString()); } } //Debug.LogError(TextureScale); Hashtable frames = (Hashtable)decodedHash["frames"]; foreach (System.Collections.DictionaryEntry item in frames) { ProFlareAtlas.Element newElement = new ProFlareAtlas.Element(); newElement.name = item.Key.ToString(); bool exists = false; // Check to see if this sprite exists foreach (ProFlareAtlas.Element oldSprite in oldElements) { if (oldSprite.name.Equals(newElement.name, StringComparison.OrdinalIgnoreCase)) { exists = true; break; } } if (!exists) { newElement.name = newElement.name.Replace(".png", ""); newElement.name = newElement.name.Replace(".tga", ""); newElement.name = newElement.name.Replace(".psd", ""); newElement.name = newElement.name.Replace(".PSD", ""); } Hashtable table = (Hashtable)item.Value; Hashtable frame = (Hashtable)table["frame"]; int frameX = int.Parse(frame["x"].ToString()); int frameY = int.Parse(frame["y"].ToString()); int frameW = int.Parse(frame["w"].ToString()); int frameH = int.Parse(frame["h"].ToString()); Rect finalUVs = new Rect(frameX, frameY, frameW, frameH); Rect rect = new Rect(frameX, frameY, frameW, frameH); float width = TextureScale.x; float height = TextureScale.y; if (width != 0f && height != 0f) { finalUVs.xMin = rect.xMin / width; finalUVs.xMax = rect.xMax / width; finalUVs.yMin = 1f - rect.yMax / height; finalUVs.yMax = 1f - rect.yMin / height; } newElement.UV = finalUVs; newElement.Imported = true; atlas.elementsList.Add(newElement); } foreach (ProFlareAtlas.Element oldSprite in oldElements) { if (!oldSprite.Imported) { atlas.elementsList.Add(oldSprite); } } // Sort imported sprites alphabetically atlas.elementsList.Sort(CompareSprites); Debug.Log("PROFLARES - Imported " + atlas.elementsList.Count + " Elements"); // Unload the asset asset = null; Resources.UnloadUnusedAssets(); }
/// <summary> /// Parse the specified JSon file, loading sprite information for the specified atlas. /// </summary> public static void LoadSpriteData(ProFlareAtlas atlas, TextAsset asset) { if (asset == null || atlas == null) { return; } string jsonString = asset.text; Hashtable decodedHash = jsonDecode(jsonString) as Hashtable; if (decodedHash == null) { Debug.LogWarning("Unable to parse Json file: " + asset.name); return; } List <ProFlareAtlas.Element> oldElements = atlas.elementsList; atlas.elementsList = new List <ProFlareAtlas.Element>(); Vector2 TextureScale = Vector2.one; //Find Texture Size Hashtable meta = (Hashtable)decodedHash["meta"]; foreach (System.Collections.DictionaryEntry item in meta) { if (item.Key.ToString() == "size") { Hashtable sizeTable = (Hashtable)item.Value; TextureScale.x = int.Parse(sizeTable["w"].ToString()); TextureScale.y = int.Parse(sizeTable["h"].ToString()); } } //Debug.LogError(TextureScale); Hashtable frames = (Hashtable)decodedHash["frames"]; foreach (System.Collections.DictionaryEntry item in frames) { ProFlareAtlas.Element newElement = new ProFlareAtlas.Element(); newElement.name = item.Key.ToString(); bool exists = false; // Check to see if this sprite exists foreach (ProFlareAtlas.Element oldSprite in oldElements) { if (oldSprite.name.Equals(newElement.name, StringComparison.OrdinalIgnoreCase)) { exists = true; break; } } if (!exists) { newElement.name = newElement.name.Replace(".png", ""); newElement.name = newElement.name.Replace(".tga", ""); newElement.name = newElement.name.Replace(".psd", ""); newElement.name = newElement.name.Replace(".PSD", ""); } Hashtable table = (Hashtable)item.Value; Hashtable frame = (Hashtable)table["frame"]; int frameX = int.Parse(frame["x"].ToString()); int frameY = int.Parse(frame["y"].ToString()); int frameW = int.Parse(frame["w"].ToString()); int frameH = int.Parse(frame["h"].ToString()); Rect finalUVs = new Rect(frameX, frameY, frameW, frameH); Rect rect = new Rect(frameX, frameY, frameW, frameH); float width = TextureScale.x; float height = TextureScale.y; if (width != 0f && height != 0f) { finalUVs.xMin = rect.xMin / width; finalUVs.xMax = rect.xMax / width; finalUVs.yMin = 1f - rect.yMax / height; finalUVs.yMax = 1f - rect.yMin / height; } newElement.UV = finalUVs; newElement.Imported = true; atlas.elementsList.Add(newElement); } foreach (ProFlareAtlas.Element oldSprite in oldElements) { if (!oldSprite.Imported) { atlas.elementsList.Add(oldSprite); } } // Sort imported sprites alphabetically atlas.elementsList.Sort(CompareSprites); Debug.Log("PROFLARES - Imported " + atlas.elementsList.Count + " Elements"); // Unload the asset asset = null; Resources.UnloadUnusedAssets(); }
public override void OnInspectorGUI () { _ProFlareAtlas = target as ProFlareAtlas; CheckUndo(); // base.OnInspectorGUI(); FlareEditorHelper.DrawGuiDivider(); GUIStyle title = FlareEditorHelper.TitleStyle(); GUIStyle thinButton = FlareEditorHelper.ThinButtonStyle(); GUIStyle enumDropDown = FlareEditorHelper.EnumStyleButton(); GUIStyle redButton = FlareEditorHelper.ThinButtonRedStyle(); EditorGUILayout.LabelField("ProFlare Atlas Editor",title); GUILayout.Space(10f); _ProFlareAtlas.texture = EditorGUILayout.ObjectField("Flare Atlas Texture", _ProFlareAtlas.texture, typeof(Texture2D), false) as Texture2D; if(_ProFlareAtlas.texture == null){ EditorGUILayout.HelpBox("Assign a texture to the atlas.", MessageType.Warning,true); return; } TextAsset ta = EditorGUILayout.ObjectField("Atlas JSON Import", null, typeof(TextAsset), false) as TextAsset; if (ta != null) { FlareJson.LoadSpriteData(_ProFlareAtlas, ta); Updated = true; } FlareEditorHelper.DrawGuiDivider(); EditorGUILayout.LabelField("Atlas Elements",title); GUILayout.Space(6f); EditorGUILayout.BeginHorizontal(); if(_ProFlareAtlas.elementsList.Count < 1) EditorGUILayout.HelpBox("No Elements in flare atlas", MessageType.Warning,true); else{ _ProFlareAtlas.elementNameList = new string[_ProFlareAtlas.elementsList.Count]; for(int i = 0; i < _ProFlareAtlas.elementNameList.Length; i++) _ProFlareAtlas.elementNameList[i] = _ProFlareAtlas.elementsList[i].name; int _ProFlareAtlasElementNumber = EditorGUILayout.Popup(_ProFlareAtlas.elementNumber, _ProFlareAtlas.elementNameList,enumDropDown); if(_ProFlareAtlasElementNumber != _ProFlareAtlas.elementNumber){ _ProFlareAtlas.elementNumber = _ProFlareAtlasElementNumber; renameString = _ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].name; } if(GUILayout.Button("EDIT",thinButton)){Updated = true; if(_ProFlareAtlas.editElements) _ProFlareAtlas.editElements = false; else _ProFlareAtlas.editElements = true; } } if(GUILayout.Button("ADD NEW",thinButton)){ _ProFlareAtlas.editElements = true; ProFlareAtlas.Element element = new ProFlareAtlas.Element(); element.name = "New Element " + _ProFlareAtlas.elementsList.Count; renameString = element.name; element.Imported = false; _ProFlareAtlas.elementsList.Add(element); _ProFlareAtlas.elementNumber = _ProFlareAtlas.elementsList.Count-1; Updated = true; } EditorGUILayout.EndHorizontal(); if(_ProFlareAtlas.elementsList.Count < 1) return; EditorGUILayout.BeginVertical("box"); GUILayout.Space(20f); Rect lastRect = GUILayoutUtility.GetLastRect(); Rect outerRect2 = new Rect(lastRect.center.x,0+lastRect.yMin,200,200); if(_ProFlareAtlas.elementsList.Count > 0){ GUI.DrawTextureWithTexCoords(outerRect2, _ProFlareAtlas.texture, _ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].UV, false); GUILayout.Space(200f); } GUI.enabled = _ProFlareAtlas.editElements; if(_ProFlareAtlas.editElements){ int extra = 0; #if UNITY_4_3 extra = 10; #endif Rect outerRect3 = new Rect(107+extra,lastRect.yMin,0.5f,200); Rect rect = new Rect(0,0,1,1); GUI.DrawTextureWithTexCoords(outerRect3, EditorGUIUtility.whiteTexture, rect, false); Rect outerRect4 = new Rect(7+extra,100+lastRect.yMin,200,0.5f); GUI.DrawTextureWithTexCoords(outerRect4, EditorGUIUtility.whiteTexture, rect, true); } GUILayout.BeginHorizontal(); if(!_ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].Imported){ renameString = EditorGUILayout.TextField("Name",renameString); if(GUILayout.Button("RENAME")){ Updated = true; _ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].name = renameString; } }else EditorGUILayout.LabelField("Name - "+renameString); GUILayout.EndHorizontal(); EditorGUILayout.Toggle("Imported :", _ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].Imported); float width = EditorGUILayout.Slider("Width", _ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].UV.width,0f,1f); float height = EditorGUILayout.Slider("Height", _ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].UV.height,0f,1f); float CenterX = EditorGUILayout.Slider("Center X", _ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].UV.center.x,0f,1f); float CenterY = EditorGUILayout.Slider("Center Y", _ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].UV.center.y,0f,1f); float xMin = _ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].UV.xMin; float yMin = _ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].UV.yMin; Rect newRect = new Rect(xMin,yMin,width,height); newRect.center = new Vector2(CenterX,CenterY); GUILayout.Space(40f); _ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber].UV = newRect; if(GUILayout.Button("DELETE ELEMENT",redButton)){ Updated = true; _ProFlareAtlas.elementsList.Remove(_ProFlareAtlas.elementsList[_ProFlareAtlas.elementNumber]); _ProFlareAtlas.elementNumber = 0; } EditorGUILayout.EndVertical(); GUI.enabled = true; if (GUI.changed || Updated) { Updated = false; guiChanged = true; EditorUtility.SetDirty (target); } FlareEditorHelper.DrawGuiDivider(); }
/// <summary> /// Sprite comparison function for sorting. /// </summary> static int CompareSprites(ProFlareAtlas.Element a, ProFlareAtlas.Element b) { return(a.name.CompareTo(b.name)); }