using UnityEngine; using UnityEditor; using System.Collections; using System.Collections.Generic; using System.IO; using System; [System.Serializable] public class goColor { public GameObject go; public GameObject border; public Color32 rgb; public goColor(GameObject go, Color32 rgb, GameObject border = null) { this.go = go; this.border = border; this.rgb = rgb; } public goColor(GameObject go, byte r, byte g, byte b, GameObject border = null) { this.go = go; this.border = border; this.rgb = new Color32(r, g, b, 0); } } [System.Serializable] public class goAlpha { public GameObject go; public int aPercent; public goAlpha(GameObject go, int val) { this.go = go; aPercent = val; } public static byte percentToByte(int val) { return BitConverter.GetBytes( Mathf.RoundToInt( 255 * ((float)(((((val < 100) ? val : 100) > 0) ? val : 0)) / 100)))[0]; } public static int byteToPercent(byte val) { return val / 255 * 100; } } public class EditorTileSelect : MonoBehaviour { public string levelName; public goColor[] rgbMap; public goAlpha[] aMap; public void save() { string path = Application.dataPath + "/Resources/Levels/" + levelName + ".txt"; if (!File.Exists(path)) { File.Create(path); } StreamWriter sw = new StreamWriter(path); for (int i = 0; i < rgbMap.Length; i++) // Write RGB values { //Debug.Log(AssetDatabase.GetAssetPath(gos[i])); sw.WriteLine(AssetDatabase.GetAssetPath(rgbMap[i].go)); if(rgbMap[i].border != null) sw.WriteLine(AssetDatabase.GetAssetPath(rgbMap[i].border)); sw.WriteLine(rgbMap[i].rgb.r); sw.WriteLine(rgbMap[i].rgb.g); sw.WriteLine(rgbMap[i].rgb.b); } if(aMap.Length > 0) { sw.WriteLine(":"); for (int i = 0; i < aMap.Length; i++) { sw.WriteLine(AssetDatabase.GetAssetPath(aMap[i].go)); sw.WriteLine(goAlpha.percentToByte(aMap[i].aPercent)); } } sw.Close(); } public void load() { List colors = new List(); List alphas = new List(); StreamReader sr; string path = Application.dataPath + "/Resources/Levels/" + levelName + ".txt"; try { GameObject go; GameObject border; byte r, g, b; string line; bool doingAlphas = false; sr = new StreamReader(path); while (!sr.EndOfStream) // Read values until EOS { line = sr.ReadLine(); if (line == ":") { doingAlphas = true; line = sr.ReadLine(); } go = AssetDatabase.LoadAssetAtPath(line); // Load GameObject line = sr.ReadLine(); if (!char.IsDigit(line[0])) // Has a border { border = AssetDatabase.LoadAssetAtPath(line); line = sr.ReadLine(); } else border = null; if (!doingAlphas) // Reading RGB values { byte.TryParse(line, out r); // Read red value line = sr.ReadLine(); byte.TryParse(line, out g); // Read green value line = sr.ReadLine(); byte.TryParse(line, out b); // Read blue value //Debug.Log(r + ", " + g + ", " + b + " | " + go.name); colors.Add(new goColor(go, r, g, b, border)); }else // Reading alpha values { byte.TryParse(line, out r); // Read alpha value (store in r because it wont be used any more) //Debug.Log(r + " | " + go.name); alphas.Add(new goAlpha(go, r)); } } sr.Close(); } catch (System.Exception) { throw; } rgbMap = colors.ToArray(); aMap = alphas.ToArray(); } } [CustomEditor (typeof(EditorTileSelect))] public class EditorTileSelectEditor : Editor { public override void OnInspectorGUI() { base.OnInspectorGUI(); if(GUILayout.Button("Save")) { ((EditorTileSelect)target).save(); } if(GUILayout.Button("Load")) { ((EditorTileSelect)target).load(); } } }