gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:47:15 24 4
gpt4 key购买 nike

我有 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com