首页 > 解决方案 > 如何从一个委托调用不同的方法/脚本?

问题描述

我试图理解我的问题。我在 Unity 场景中有一个服务器对象。每次来自 Client 的消息到达时,服务器需要从不同的场景调用不同的方法。

我不希望在服务器脚本中保留对场景中所有交互脚本的引用。我的解决方案是创建一个委托并让交互脚本订阅委托事件。但是我的场景发生了变化,我发现来自不同脚本的两种方法订阅了同一个委托,我只能得到好的结果。

我的解决方案是专门为下一个方法创建另一个委托,但我想知道我可以创建多少个委托,有没有办法创建一个委托,但每次都执行另一个函数(或者它是另一个动作?)

public class Server : MonoBehaviour
{
    public delegate void CommunicationEvent();
    public static event CommunicationEvent OnCommunication;

    public delegate void PortalEvent();
    public static event PortalEvent OnSceneSync;

    private const int MAX_USER = 1;
    private const int PORT = 26000;
    private const int WEB_PORT = 26001;
    private const int BYTE_SIZE = 1024; 

 /*.... */

    private void LightsOn(int cnnId, int recHostId, Net_OnEnterGate oeg)
    {
        OnCommunication?.Invoke();
        SendClient(recHostId, cnnId, oeg);
    }

    private void SceneSyncing(int cnnId, int recHostId, Net_OnSceneSync oss)
    {
        oss.TransNumber = LoadScene.currentSceneNumber;
        OnSceneSync?.Invoke();
        SendClient(cnnId, recHostId, oss);
    }

    private void LoadNewScene(int cnnID, int recHostId, Net_OnSceneLoad osl)
    {
        OnCommunication?.Invoke();
    }

我希望有一个委托 OnCommunication(),并且有不同的脚本订阅它以执行它们独特的方法,而不是执行场景中前一个脚本中的方法。

有可能吗?你会建议什么样的工作流程?

标签: c#unity3dserverdelegatesinvoke

解决方案


我还没有完全理解这个问题,也没有看到你在哪里订阅事件的回调。

我想知道我可以创建多少个代表

→ 只要你喜欢!问题是你需要并且你想要吗?


如果您想为一个事件添加多个回调(这就是标题的声音),您只需使用+=而不是=

public class Example : MonoBehaviour
{
     private void Awake()
     {
         // Note: It is always save to remove callbacks
         //       even if not added yet.
         //       This makes sure they are added only once for this instance
         OnCommunication -= OnServerCommunication;
         OnSceneSync -= OnServerSceneSync;

         OnCommunication += OnServerCommunication;
         OnSceneSync += OnServerSceneSync;
     }

     privtae void OnDestroy()
     {
         // Make sure to always remove callbacks when not needed anymore
         OnCommunication -= OnServerCommunication; 
         OnSceneSync -= OnServerSceneSync;
     }

     private void OnServerCommunication()
     {
         Debug.Log("Communication was invoked", this);
     }

     private void OnServerSceneSync()
     {
         Debug.Log("Scene sync was invoked", this);
     }
}

如果您的问题更像是“我想添加多个回调但始终只执行其中一个”。我建议不要使用events 而是使用类似的东西

public interface IServerCommunicationHandler
{
    void OnServerCommunication();
}

public interface IServerSceneSyncHandler
{
    void OnServerSceneSync();
}

并且在服务器存储监听器中

public class Server : MonoBehaviour
{
    private static readonly List<IServerCommunicationHandler> CommunicationListeners = new List<IServerCommunicationHandler>();
    private static readonly List<IServerSceneSyncHandler> SceneSyncListeners = new List<IServerSceneSyncHandler>();

    public static void AddCommunicationListener(IServerCommunicationHandler listener)
    {
        if (!CommunicationListeners.Contains(listener)) CommunicationListeners.Add(listener);
    }

    public static void RemoveCommunicationListener(IServerCommunicationHandler listener)
    {
        if (CommunicationListeners.Contains(listener)) CommunicationListeners.Remove(listener);
    }

    public static void AddSceneSyncListener(IServerSceneSyncHandler listener)
    {
        if (!SceneSyncListeners.Contains(listener)) SceneSyncListeners.Add(listener);
    }

    public static void RemoveSceneSyncListener(IServerSceneSyncHandler listener)
    {
        if (SceneSyncListeners.Contains(listener)) SceneSyncListeners.Remove(listener);
    }
}

和而不是OnCommunication?Invoke()OnSceneSync?.Invoke()有例如

private void InvokeCommunication()
{
    var listener = CommunicationListeners.Count > 0 ? CommunicationListeners[0] : null;
    if (listener == null) return;

    listener.OnServerCommunication();
    CommunicationListeners.RemoveAt(0);
}

private void InvokeSceneSync()
{
    var listener = SceneSyncListeners.Count > 0 ? SceneSyncListeners[0] : null;
    if (listener == null) return;

    listener.OnServerSceneSync();
    SceneSyncListeners.RemoveAt(0);
}

然后脚本可能看起来像例如

public class Example : MonoBehaviour, IServerCommunicationHandler, IServerSceneSyncHandler
{
    private void Awake()
    {
        Server.AddCommunicationListener(this);
        Server.AddSceneSyncListener(this);
    }

    private void OnDestroy()
    {
        Server.RemoveCommunicationListener(this);
        Server.RemoveSceneSyncListener(this);
    }

    public void OnSeverCommunication()
    {
        Debug.Log("Communication was invoked", this);
    }

    public void OnServerSceneSync()
    {
        Debug.Log("Scene sync was invoked", this);
    }
}

推荐阅读