using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; using System.IO.Ports; using System; using System.Threading; using System.Linq; using UnityEditor.Experimental.GraphView; public class SetActive : MonoBehaviour { private Thread thread1; public List objects; private void Awake() { ThreadWork.objectsDictionary = new Dictionary(); ThreadWork.objectsActivation = new Dictionary(); for (int i = 0; i < objects.Count; i++) { ThreadWork.objectsDictionary.Add(objects[i].name, objects[i]); ThreadWork.objectsActivation.Add(objects[i].name, false); } } // Use this for initialization void Start() { //zoek voor de juiste COM (check in arduino code welke het is en pas de COM aan naar de gene die dr staat) ThreadWork.m_SerialPort = new SerialPort("COM7", 9600); //arduino openen thread1 = new Thread(ThreadWork.DoWork); thread1.Start(); //StartCoroutine(ReadAsync()); } void Update() { int didit = 0; /*for (int i = 0;i < objects.Count;i++) { ThreadWork.objectsDictionary[objects[i].name].SetActive(ThreadWork.objectsActivation[objects[i].name]); }*/ foreach (string placement in ThreadWork.objectsDictionary.Keys) { didit++; Debug.LogError(ThreadWork.objectsDictionary[placement].name); Debug.LogError(ThreadWork.objectsActivation[placement]); ThreadWork.objectsDictionary[placement].SetActive(ThreadWork.objectsActivation[placement]); } } private void OnDestroy() { ThreadWork.m_SerialPort.Close(); thread1.Abort(); } } public class ThreadWork { public static bool interrupted; public static SerialPort m_SerialPort; public static Dictionary objectsDictionary = new Dictionary(); public static Dictionary objectsActivation = new Dictionary(); public static void DoWork() { m_SerialPort.Open(); m_SerialPort.DiscardInBuffer(); while (true) { String read = m_SerialPort.ReadLine(); string[] values = read.Split(','); List thingies = new List(); foreach (string value in values) { string[] smh = value.Split(':'); foreach (string s in smh) { thingies.Add(s); } } for (int i = 0;i < thingies.Count -1;i+= 2) { string currentPlacement = thingies[i]; int value; int.TryParse(thingies[i + 1],out value); //Debug.Log("placement: " + currentPlacement); //Debug.Log("value: " + value); if(value == 0) { Debug.LogError("And i set it to true"); objectsActivation[currentPlacement] = true; Debug.LogError("Value after i set it to true fr fr" + objectsActivation[currentPlacement]); } else { Debug.LogError("And i set it to false"); objectsActivation[currentPlacement] = false; Debug.LogError("Value after i set it to false fr fr" + objectsActivation[currentPlacement]); } } //Debug.LogError(read + " string value distance"); //float value = float.Parse(read); //Debug.LogError(value + " float value distance"); //if (read == "true") //{ // interrupted = true; // Debug.Log("1"); //} //else if (read == "false") //{ // interrupted = false; // Thread.Sleep(5); //} //Debug.LogError(Serial.println(sonar.ping_cm())); } } }