我有TCP客户端(Unity c#)和服务器(WinForms app c#)。我需要我的服务器发送一些JSON命令,如下所示:
{ ""ObjName"": ""Cube_2"", ""Method"":""MoveLeft"", ""Delay"":0}
这个特定的命令说找到GameObject“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));
}
}
答案 0 :(得分:3)
这取决于你认为错误。
您应该有一个脚本来处理传入数据的解析,这样就不需要搜索组件了,它总是一样的。
然后你可以有一个替换调用调用的字典。
所以你的片段变成了:
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可以处理信息或转发给运动控制器:
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(){}
}
在Awake上,Dispatch从AOSController注册到静态事件。在AOSController.Start中,对象触发事件并将自身传递给AOSDispatch。那个将它添加到字典中。在销毁时,AOSDispatch获取事件并删除AOSController。
现在你有一个在任何给定时间包含场景中所有AOSController的集合。
因此,您不需要执行GameObject.Find,因为您可以从字典中获取对象(真正的快速过程)。