EditorTileSelect.cs 4.69 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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<goColor> colors  = new List<goColor>();
        List<goAlpha> alphas = new List<goAlpha>();

        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<GameObject>(line);  // Load GameObject
                line = sr.ReadLine();

                if (!char.IsDigit(line[0]))  // Has a border
                {
                    border = AssetDatabase.LoadAssetAtPath<GameObject>(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();
        }
    }
}