【发布时间】:2016-12-05 03:56:01
【问题描述】:
我目前正在了解整个网络如何统一运作。在我的代码中,我正在创建一个由多个预制件制成的宇宙飞船。
这一切都始于一个Hardpoint。 Hardpoint 可以保存一个对象,稍后将在循环中实例化该对象。
在PlayerController(起点)我有这段代码来生成第一个对象,驾驶舱:
[Command]
void CmdOnConnect() {
string json = GameObject.Find("TestPlayer").GetComponent<ComponentObject>().ToJSON();
CompressedComponent compressedComponent = JsonUtility.FromJson<CompressedComponent>(json);
gameObject.GetComponent<Hardpoint>().Hold(GameObject.Find("Component Repository").GetComponent<ComponentRepository>().cockpit[compressedComponent.componentNumber]);
gameObject.GetComponent<Hardpoint>().SpawnComponent();
gameObject.GetComponent<Hardpoint>().RollThroughDecompression(compressedComponent);
Camera.main.GetComponent<PlayerCamera>().player = gameObject;
}
接下来是SpawnComponent() 代码,位于Hardpoint 脚本中:
public void SpawnComponent() {
Clear();
CmdSpawn();
}
CmdSpawn,同样位于Hardpoint:
[Command]
public void CmdSpawn()
{
Debug.Log("[COMMAND] Spawning " + holds.name);
heldInstance = Instantiate(holds, transform.position, transform.rotation) as GameObject;
heldInstance.transform.SetParent(transform);
NetworkServer.SpawnWithClientAuthority(heldInstance, transform.root.gameObject);
}
最后是RollThroughDecompression,它只调用了Decompress() 函数:
public void RollThroughDecompression(CompressedComponent c) {
heldInstance.GetComponent<ComponentObject>().Decompress(c);
}
为了不遗漏任何信息,Decompress():
public void Decompress(CompressedComponent c) {
componentType = (Type)Enum.Parse(typeof(Type), c.componentType);
componentNumber = c.componentNumber;
UpdateHardPoints();
GameObject[] typeRepository = GetRepository(componentType);
//update children
int point = 0;
foreach (Transform child in transform)
{
Hardpoint hardpoint = child.GetComponent<Hardpoint>();
if (hardpoint != null) {
if (c.hardpoints[point] != null) {
//get the hardpoint's repository
GameObject[] hardpointRepo = GetRepository((Type)Enum.Parse(typeof(Type), c.hardpoints[point].componentType));
//set the hardpoint to hold this object
hardpoint.Hold(hardpointRepo[c.hardpoints[point].componentNumber]);
hardpoint.SpawnComponent();
hardpoint.RollThroughDecompression(c.hardpoints[point]);
point++;
}
}
}
}
对不起,代码有点混乱/令人困惑,但我一直在试图弄清楚为什么新生成的对象没有client authority,除了生成的第一个对象(可能是因为它是从PlayerController)。我已经被这个问题困扰了好几天了。新生成的对象被设置为本地玩家对象的子对象,甚至在测试时使用NetworkServer.SpawnWithClientAuthority 生成:
Trying to send command for object without authority. 调用 CmdSpawn() 时。
我得到的结果:
如您所见,驾驶舱(第一部分)按预期生成。但安装在Hardpoints 上的零件没有。澄清一下,EmptyHardpoint 就是这样。一个没有孩子的硬点,只是一个带有hardpoint 脚本和playercontroller 附加到它的空游戏对象。驾驶舱预制件还包括img 和hardpoints
【问题讨论】:
标签: unity3d unity5 unity3d-unet