- Home /
How to use StreamReader in my case
This is my code. ill post screenshots about my problem and output. i wish you could help me
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
using System.IO;
public class LevelEditor : EditorWindow
{
int width;
int height;
int[][] map;
Color[] colors = new Color[] { Color.red, Color.yellow, Color.blue, Color.magenta, Color.green, Color.cyan, Color.gray, Color.black, Color.white};
// string[] colorNames = new string[]{"Red", "Yellow", "Blue", "Magenta", "Green", "Cyan", "Gray", "Black", "white"};
Color colors1 = Color.red;
string fileName = "map.txt";
int[] ButtonColorIndexs;
int ButtonCounter=0;
bool buttonClicked = false;
private static Vector2 TestScrolling;
[MenuItem("Window/Level Editor")]
public static void ShowWindow()
{
EditorWindow.GetWindow(typeof(LevelEditor));
}
void OnGUI()
{
TestScrolling = GUILayout.BeginScrollView (TestScrolling);
{
if(buttonClicked)
GUI.color = Color.red;
GUILayout.BeginVertical();
{
width = EditorGUILayout.IntField(width);
height = EditorGUILayout.IntField(height);
if (GUILayout.Button("Generate" ,GUILayout.Width(1350), GUILayout.Height(40)))
{
UpdateArray(width, height);
ButtonColorIndexs = new int[(width * height)+1];
}
if (map != null)
{
foreach (var y in Enumerable.Range(0, map.Length))
{
GUILayout.BeginHorizontal();
{
foreach (var x in Enumerable.Range(0, map[y].Length))
{
GUI.color = colors[map[y][x]];
if (GUILayout.Button(string.Format("{2}", x, y, map[y][x]), GUILayout.Height(50)))
{
map[y][x] = (map[y][x] + 1) % colors.Length;
}
}
}
GUILayout.EndHorizontal();
}
GUI.color = colors1;
GUILayout.BeginHorizontal();
{
fileName = GUILayout.TextField(fileName);
if (GUILayout.Button("Save"))
{
OnSaveButtonClicked();
}
if (GUILayout.Button("Load"))
{
OnLoadClicked();
}
}
GUILayout.EndHorizontal();
}
}
GUILayout.EndVertical();
}
GUILayout.EndScrollView ();
}
void OnSaveButtonClicked()
{
var text = "";
foreach (var y in Enumerable.Range(0, map.Length))
{
foreach (var x in Enumerable.Range(0, map[y].Length))
{
text += map[y][x] + ",";
}
text += "\n";
}
SaveFile(string.Format("Assets/Resources/Maps/{0}", fileName), text);
}
void OnLoadClicked()
{
TextAsset map = (TextAsset)Resources.Load ("Assets/Resources/Maps");
}
void SaveFile(string path, string text)
{
using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write))
{
using (var writer = new StreamWriter(fs))
{
writer.Write(text);
}
}
}
void UpdateArray(int width, int height)
{
map = new int[height][];
foreach (var y in Enumerable.Range(0, height))
{
map[y] = new int[width];
}
}
}
Answer by Arin_Jisoo · Jan 05, 2018 at 03:11 AM
so this is the output. its a costume editor. so when the user press the SAVE button. the string value and its position will be save in assets/Resources/$$anonymous$$aps ..... so my problem is. when the user change the color and value of button and press the LOAD button. it will show the color and value of button that has been saved
so how i can load it. that will include the changes of color and number ?
Your answer
![](https://koobas.hobune.stream/wayback/20220612145853im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name "Game Foundation" does not exist in the current context 1 Answer
Multiple Cars not working 1 Answer
Coding help: How to use xml serialization 1 Answer
[Solved] Built in GUI continuously Loads through different Options. 1 Answer
How do I save and load the state of the GameObjects with PlayerPrefs? 0 Answers