我目前正在學習這整個網絡的事情是如何工作的統一。在我的代碼中,我創建了一個由多個預製組成的太空船。統一產卵孩子與客戶端權限
這一切都始於一個Hardpoint
。一個Hardpoint
可以容納一個單一的對象,稍後將在循環中實例化。
在PlayerController
(起點)1有這樣的代碼產卵第一個目的,駕駛艙:
[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
不正確。這也是我的第一個猜測,但是應該產生的第一個組件,會產生,其他組件不會。我會在一會兒發佈屏幕截圖。 –
如果你是分別產卵,所以分別分配 –
對不起,看着它錯了。已更新,但不幸的是問題仍然存在。 –