Hi! My version is 2018.1.3f1(64 bit) and I got the problem “Unity Editor has stopped working” when I click the button “Click”
I try to another computer and I got the problem"ObjectDisposedException: The object was used after being disposed."
Code:
using System.Collections.Generic; using UnityEngine; using System.IO; using System; #if UNITY_EDITOR using UnityEditor; #endif public class Export : MonoBehaviour { Material mat; private Rect windowRect = new Rect(20, 20, 512, 256); // A list of random values to draw private List<float> values; private List<float> values1; private List<double> Time; // The list the drawing function uses... private List<float> drawValues = new List<float>(); private List<float> drawValues1 = new List<float>(); List<double> data_time = new List<double>(); // List of Windows private bool showWindow0 = false; string filename = ""; int TickStart; double time = 0.00; // Use this for initialization void Start () { filename = Application.dataPath + "/test.csv"; TickStart = Environment.TickCount; mat = new Material(Shader.Find("Hidden/Internal-Colored")); // Should check for material but I'll leave that to you.. // Fill a list with ten random values values = new List<float>(); values1 = new List<float>(); Time = new List<double>(); for (int i = 0; i < 10; i++) { values.Add(UnityEngine.Random.value * 200); values1.Add(UnityEngine.Random.value * 100); } } // Update is called once per frame void Update () { time = (Environment.TickCount - TickStart) / 1000.0; values.Add(UnityEngine.Random.value * 200); values1.Add(UnityEngine.Random.value * 100); Time.Add(time); } private void OnGUI() { GUI.color = Color.red; GUI.Label(new Rect(240, 235, 150, 150), "Time: " + time.ToString("0.00")); // Create a GUI.toggle to show graph window showWindow0 = GUI.Toggle(new Rect(10, 30, 100, 20), showWindow0, "Show Graph"); if (showWindow0) { // Set out drawValue list equal to the values list drawValues = values; drawValues1 = values1; data_time = Time; windowRect = GUI.Window(0, windowRect, DrawGraph, ""); } if (GUI.Button(new Rect(0, 10, 80, 20), "Click")) { SaveToFile(); } } void DrawGraph(int windowID) { // Make Window Draggable GUI.DragWindow(new Rect(0, 0, 10000, 10000)); //GUI.color = Color.yellow; //GUI.Label(new Rect(240, 235, 150, 150), "Time"); // Draw the graph in the repaint cycle if (Event.current.type == EventType.Repaint) { GL.PushMatrix(); GL.Clear(true, false, Color.black); mat.SetPass(0); //// Draw a black back ground Quad //GL.Begin(GL.QUADS); //GL.Color(Color.black); //GL.Vertex3(4, 4, 0); //GL.Vertex3(windowRect.width - 4, 4, 0); //GL.Vertex3(windowRect.width - 4, windowRect.height - 4, 0); //GL.Vertex3(4, windowRect.height - 4, 0); //GL.End(); // Draw the lines of the graph GL.Begin(GL.LINES); GL.Color(Color.green); int valueIndex = drawValues.Count - 1; for (int i = (int)windowRect.width - 4; i > 3; i--) { float y1 = 0; float y2 = 0; if (valueIndex > 0) { y2 = drawValues[valueIndex]; y1 = drawValues[valueIndex - 1]; } GL.Vertex3(i, windowRect.height - 4 - y2, 0); GL.Vertex3((i - 1), windowRect.height - 4 - y1, 0); valueIndex -= 1; } GL.End(); GL.Begin(GL.LINES); GL.Color(Color.red); int valueIndex1 = drawValues1.Count - 1; for (int i = (int)windowRect.width - 4; i > 3; i--) { float y1 = 0; float y2 = 0; if (valueIndex1 > 0) { y2 = drawValues1[valueIndex1]; y1 = drawValues1[valueIndex1 - 1]; } GL.Vertex3(i, windowRect.height - 4 - y2, 0); GL.Vertex3((i - 1), windowRect.height - 4 - y1, 0); valueIndex1 -= 1; } GL.End(); GL.PopMatrix(); } } public void SaveToFile() { #if UNITY_EDITOR var folder = Application.streamingAssetsPath; if (!Directory.Exists(folder)) Directory.CreateDirectory(folder); #else var folder = Application.persistentDataPath; #endif var filePath = Path.Combine(folder, "export.csv"); using (var writer = new StreamWriter(filePath, false)) { writer.WriteLine("Left Knee, Left Hip, Time"); writer.Close(); } using (var writer = new StreamWriter(filePath, true)) { foreach (var knee in values) { foreach (var hip in values1) { foreach (var time in data_time) { writer.WriteLine(knee + "," + hip + "," + time); writer.Close(); } } } } Debug.Log($"CSV file written to \"{filePath}\""); #if UNITY_EDITOR AssetDatabase.Refresh(); #endif } }