【问题标题】:Unity, Global Event, Global Listener or GM.Find() in ThreadUnity、全局事件、全局监听器或线程中的 GM.Find()
【发布时间】:2016-07-16 05:53:45
【问题描述】:

我在 C# 中使用 Unity 5.3.4。

我创建了一个全局套接字系统,可以让我在整个游戏过程中保持连接。脚本 (ClientManager) 被放置在一个坚不可摧的 GameObject 中(使用 DontDestroy)。 第一个场景是initialsé。

只是,当我在另一个场景中时,我需要找到一个游戏对象。 问题是我的脚本没有在主线程上执行。因此我不能。我无法在Start() 中执行搜索,因为我不在正确的场景中并且所需的游戏对象不存在。

我需要在所有场景中创建一个共同的 évennements 不同的脚本,或者然后在主线程中执行我的GameObject.Find ()

我创建了一个监听系统。我还可以通过向搜索的脚本添加侦听器来解决我的问题...

你知道怎么做吗?您还有其他解决方案吗?

这是我的脚本:

客户经理:

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>();
    }
}

听众:

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(搜索脚本):

public class rslideController : MonoBehaviour {
   // How to know when the methods are called?
}

【问题讨论】:

标签: c# multithreading events unity3d listener


【解决方案1】:

我通过在我的 Client.cs 中创建一个侦听器列表解决了我的问题。 像这样:

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);
    }
}

还有:

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(){ }
}

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-30
    • 2021-10-17
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-22
    相关资源
    最近更新 更多