c# - Unity c# 触发另一个 GameObject 的方法。更好的方法?

标签 c# unity3d invoke

我有 TCP 客户端 (Unity c#) 和服务器 (WinForms app c#)。我需要我的服务器发送一些 JSON 命令,如下所示:

{ ""ObjName"": ""Cube_2"", ""Method"":""MoveLeft"", ""Delay"":0}

这个特定的命令说找到游戏对象“Cube_2”和触发方法“MoveLeft”。

当我从服务器收到它时,我将它转换成我的 AOSCommand 类:

public class AOSCommand
{
    public string ObjName;
    public string Method;
    public int delay;
}

然后我执行以下操作(我认为这不是最佳解决方案,所以这是一个问题):

private void ProcessCommand(AOSCommand command)
    {
        GameObject cube = GameObject.Find(command.ObjName);
        MonoBehaviour script = cube.GetComponent(command.ObjName.Split(new string[] {"_"}, StringSplitOptions.None)[0]) as MonoBehaviour;
        script.Invoke(command.Method, command.delay);
    }

如何以更好的方式从 AOSCommand.Method 字符串中触发某些方法?

附加到 Cube_2(和 Cube_1 并且可能附加到未知数量的其他对象)的脚本:

using UnityEngine;

public class Cube : MonoBehaviour {

    private GameObject thisObj;

    private void Start()
    {
        thisObj = this.gameObject;
    }

    public void MoveLeft()
    {
        thisObj.transform.Translate(new Vector3(1,0,0));
    }

    public void MoveRight()
    {
        thisObj.transform.Translate(new Vector3(-1, 0, 0));
    }
}

最佳答案

这取决于你认为什么是错误的。

您应该有一个脚本来处理传入数据的解析,这将消除搜索组件的需要,它始终是相同的。

然后你可以用一个字典来代替调用。

所以你的代码片段变成:

private void ProcessCommand(AOSCommand command)
{
    GameObject cube = GameObject.Find(command.ObjName);
    AOSDispatch dispatch = cube.GetComponent<AOSDispatch>()
    if(dispatch == null){ return; } // or debug or exception
    dispatch.Call(command);
}

这是在主接收器上。然后是立方体上的脚本:

public class AOSDispatch : MonoBehaviour
{
    Dictionary<string, Action> dict;
    void Start()
    {
        dict.Add("MoveLeft", MoveLeft);
        dict.Add("MoveRight", MoveRight);
    }
    public void Call(AOSCommand command)
    {
        if(dict.Contains(command.Method) == false){  return; } //Or debug
        // use the delay as well as you wish
        dict[command.Method]();
    }
    private void MoveLeft(){}  
    private void MoveRight(){}
}

这不一定更好,只是我的两分钱。

编辑:有评论提到 json 可以包含脚本类型以了解要使用的脚本。我不会走这条路。 AOSDispatch 将负责消息的发送。

消息说 MoveLeft,AOSDispatch 可以处理信息或转发给运动 Controller :

public class AOSDispatch : MonoBehaviour
{
    [SerializeField] private MoveController moveCtrl = null;
    Dictionary<string, Action> dict;
    void Start()
    {
        dict.Add("MoveLeft", this.moveCtrl.MoveLeft);
        dict.Add("MoveRight", this.moveCtrl.MoveRight);
    }
    public void Call(AOSCommand command)
    {
        if(dict.Contains(command.Method) == false){  return; } //Or debug
        // use the delay as well as you wish
        dict[command.Method]();
    }
}
public class MoveController: MonoBehaviour
{
    private void MoveLeft(){}  
    private void MoveRight(){}
}

好了,消息已转发且干净利落,AOSDispatch 仅执行其应做的工作,即发送 AOS。

二次编辑:

转念一想,这是一个改进的版本。 创建一个 DispatchManager 游戏对象并添加以下脚本:

public class AOSDispatch:MonoBehaviour
{
     private IDictionary<string, AOSController> dict;
     void Awake(){
           this.dict = new Dictionary<string, AOSController>(); 
           AOSController.RaiseCreation += ProcessCreation;
           AOSController.RaiseDestruction += ProcessDestruction;
     }
    void OnDestroy()
    {
           AOSController.RaiseCreation -= ProcessCreation;
           AOSController.RaiseDestruction -= ProcessDestruction;
    }
    private void ProcessCreation(AOSController controller){
         this.dict.Add(controller.name, controller);
    }
    private void ProcessDestruction(AOSController controller){
         AOSController temp= null;
         if(this.dict.TryGetValue(controller.name, out temp) == true){
             this.dict.Remove(name);
        }
    }
    private void ProcessCommand(AOSCommand command)
    {
        AOSController controller = null;
        if(this.dict.TryGetValue(command.ObjName, out controller) == true){
             controller.Call(command);
             return;
        }
    }
}

然后在对象上,您拥有像以前一样转发信息的 AOSController(只是重命名):

public class AOSController: MonoBehaviour
{
    public static event Action<AOSController> RaiseCreation;
    public static event Action<AOSController> RaiseDestruction;
    [SerializeField] private MoveController moveCtrl = null;
    Dictionary<string, Action> dict;
    void Start()
    {
        if(RaiseCreation != null) { RaiseCreation(this); }
        dict.Add("MoveLeft", this.moveCtrl.MoveLeft);
        dict.Add("MoveRight", this.moveCtrl.MoveRight);
    }
    void OnDestroy()
    {
        if(RaiseDestruction != null) { RaiseDestruction(this); }
    }
    public void Call(AOSCommand command)
    {
        if(dict.Contains(command.Method) == false){  return; } //Or debug
        // use the delay as well as you wish
        dict[command.Method]();
    }
}
public class MoveController: MonoBehaviour
{
    private void MoveLeft(){}  
    private void MoveRight(){}
}

唤醒时,Dispatch 注册到来自 AOSController 的静态事件。在 AOSController.Start 中,对象触发事件并将自身传递给 AOSDispatch。那一个将它添加到字典中。销毁时,AOSDispatch 获取事件并删除 AOSController。

现在您有一个集合,它在任何给定时间都包含场景中的所有 AOSController。

因此,您无需执行 GameObject.Find,因为您可以从字典中获取对象(真正快速的过程)。

关于c# - Unity c# 触发另一个 GameObject 的方法。更好的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49299546/

相关文章:

c# - 使用 SemaphoreSlim 限制 TPL 数据流的问题

c# - Unity 将默认命名空间添加到脚本模板?

java - 在Spring AOP中使用@JoinPoint调用Method.invoke()时“对象不是声明类的实例”

c# - 无法从媒体库获取图片

c# - 一个实体应该持续无知吗?

c# - 任务完成后如何销毁谷歌驱动器 token ?

c# - 无法加载,因为已经加载了另一个具有相同文件的 AssetBundle

c# - 如果语句不工作 Unity C#

javascript - 使用参数调用 IIFE "later"

c# - 使用事件处理程序进行 GUI 调用