Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Referencing a List in a new thread only working ingame

Discussion in 'Scripting' started by LittleRainGames, Nov 11, 2017.

  1. LittleRainGames

    LittleRainGames

    Joined:
    Apr 7, 2016
    Posts:
    97
    I'm just fiddling around creating a half assed listener but it only seems to work after I press play and not in the editor.

    Uploaded code if you want to take a look, but anyone know how I can get it to work in the editor?

    Edit: Oh ya, when I was testing in play mode I started the thread from OnEnable instead of when its selected. Even when I remove the while(.... Sleep... in the thread function it does not seem to progress in the editor at all.
     

    Attached Files:

  2. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
  3. LittleRainGames

    LittleRainGames

    Joined:
    Apr 7, 2016
    Posts:
    97
    Ok sorry about that.

    Here is first, just handles calling OnSelect and OnDeselect.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using UnityEditor;
    5. using UnityEngine;
    6.  
    7.  
    8. [ExecuteInEditMode]
    9. public class OnObjectSelectionCaller : MonoBehaviour {
    10.  
    11. #if UNITY_EDITOR
    12.  
    13.     GameObject[] lastSelected = new GameObject[0];
    14.     List<GameObject> checkDeselection = new List<GameObject>();
    15.  
    16.  
    17.     void Update () {
    18.  
    19.         if (Selection.gameObjects.Length > 0)
    20.         {
    21.             if (!Enumerable.SequenceEqual(lastSelected, Selection.gameObjects))
    22.             {
    23.                 foreach (GameObject go in Selection.gameObjects)
    24.                 {
    25.                     go.SendMessage("OnSelected");
    26.                     checkDeselection.Add(go);
    27.                 }
    28.  
    29.                 lastSelected = Selection.gameObjects;
    30.             }
    31.  
    32.         }
    33.  
    34.         if(checkDeselection.Count > 0)
    35.         {
    36.             foreach(GameObject go in checkDeselection)
    37.             {
    38.                 if (!Selection.gameObjects.Contains(go))
    39.                 {
    40.                     go.SendMessage("OnDeselected");
    41.                     checkDeselection.Remove(go);
    42.                 }
    43.             }
    44.         }
    45.      
    46.        
    47.      
    48.     }
    49. #endif
    50.  
    51. }
    52.  

    Here is second script. Which has the multithreading, and the thread is not working in editor, only play.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Threading;
    4. using UnityEngine;
    5. using System;
    6. using System.Linq;
    7. using UnityEditor;
    8.  
    9. [ExecuteInEditMode]
    10. public class MultiThreadedListenerTest : MonoBehaviour {
    11.  
    12.     public delegate void EventHandler();
    13.     public event EventHandler OnFinishThread;
    14.  
    15.     public List<bool> aBool = new List<bool>();
    16.     public List<bool> bBool = new List<bool>();
    17.  
    18.  
    19.     public Thread myThread;
    20.  
    21.     void OnEnable () {
    22.         //StartMyThead(() => { EventCaller(ref aBool, ref bBool); });
    23.  
    24.         if (!Application.isPlaying) return;
    25.  
    26.         OnFinishThread += PrintMessage;
    27.     }
    28.  
    29.     void OnDisable () {
    30.         OnFinishThread -= PrintMessage;
    31.     }
    32.  
    33.  
    34.     void OnSelected()
    35.     {
    36.         Debug.Log("OnSelected()");
    37.         StartMyThead(() => { EventCaller(ref aBool, ref bBool); });
    38.     }
    39.  
    40.  
    41.     void OnDeselected()
    42.     {
    43.         Debug.Log("OnDeselected()");
    44.         myThread.Abort();
    45.         myThread = null;
    46.     }
    47.  
    48.     public void PrintMessage()
    49.     {
    50.         Debug.Log("The Event has been called from the new Thread");
    51.     }
    52.  
    53.     public void StartMyThead(Action function)
    54.     {
    55.         if (myThread == null || !myThread.IsAlive)
    56.         {
    57.             Debug.Log("Start Thread");
    58.             myThread = new Thread(new ThreadStart(function));
    59.             myThread.Start();
    60.         }
    61.  
    62.     }
    63.  
    64.     public void EventCaller(ref List<bool> _a, ref List<bool> _b )
    65.     {
    66.         //while(_a == _b)
    67.         while (Enumerable.SequenceEqual(_a.OrderBy(fElement => fElement), _b.OrderBy(sElement => sElement)))
    68.         {
    69.  
    70.             Thread.Sleep(1);
    71.         }
    72.  
    73.         OnFinishThread();
    74.     }
    75. }
    76.