0

I use Unity 5.3.4 with C#.

I have created a global socket system that allows me to stay connected during all the game. The script (ClientManager) is placed in an indestructible GameObject (with DontDestroy). It is initialsé was the first scene.

Only, I will need to find a GameObject when I am in another scene. The problem is that my script is not execute on the main thread. So I can not. I can not execute my search in Start() because I'm not in the right scene and the desired GameObject is not present.

I would need to create a common évennements different scripts in all the scenes, or then execute my GameObject.Find () in the main thread.

I created a system of listener. I could also solve my problem by adding a listener to the script searched ...

Do you know how to do this? Do you have another solution?

Here are my scripts:

Client Manager :

public class ClientManager : MonoBehaviour, ClientListener {

    private Client client;

    public Client Client {
        get { return client; }
    }

    void Start () {
        this.client = new Client (this);
    }
    // ...

    public void OnFriendDisconnected(string username, int id){
        //...
    }
    public void OnFriendConnected(string username, int id){
        //...
        GameObject gm = GameObject.Find ("r_Slide_view");
        if (gm != null)
            gm.GetComponent<rslideController>().IsOnline(username);
    }
    public void OnReceiveMessage(string message){
        //...
    }
    public void OnAuthenticated(){
        //...
    }
    public void OnRejected(){
        //...
    }
    public void OnDisconnected(){
        //...
    } 
    void OnLevelWasLoaded(int level) {
        GameObject gm = GameObject.Find ("r_Slide_view"); // ERROR - Is not execute in the main thread
        if (gm != null)
            SlideController = gm.GetComponent<rslideController>();
    }
}

Listener :

public interface ClientListener {
    void OnAuthenticated();
    void OnRejected();
    void OnDisconnected();
    void OnFriendDisconnected(string username, int id);
    void OnFriendConnected(string username, int id);
    void OnReceiveMessage(string message);
}

rSlideController (searched script ):

public class rslideController : MonoBehaviour {
   // How to know when the methods are called?
}
Hugo Fouquet
  • 149
  • 1
  • 2
  • 10

1 Answers1

0

I solved my problem by creating a Listener list in my Client.cs. Like this :

public class Client {
    private readonly List<ClientListener> listeners = null;

    public Client(ClientListener listener){
        this.listeners = new List<ClientListener> ();
        this.listeners.Add(listener);
        //...
    }

    private void Call(){
        foreach (var listener in this.listeners) {
            listener.OnReceiveMessage (parameters [0]);
        }
    }

    //...

    public void AddListener(ClientListener listener){
        this.listeners.Add (listener);
    }
    public void RemoveListener(ClientListener listener){
        this.listeners.Remove (listener);
    }
}

And :

public class rslideController : MonoBehaviour, ClientListener {

    private Client client;

    // Use this for initialization
    void Start () {
        this.client = GameObject.Find ("Manager").GetComponent<ClientManager>().Client;
        this.client.AddListener (this);
    }

    void OnDestroy() {
        this.client.RemoveListener (this);
    }

    //...

    public void OnFriendDisconnected(string username, int id){ }
    public void OnFriendConnected(string username, int id){ }
    public void OnReceiveMessage(string message){ }
    public void OnAuthenticated(){ }
    public void OnRejected(){ }
    public void OnDisconnected(){ }
}

Thanks

Hugo Fouquet
  • 149
  • 1
  • 2
  • 10