私は現在、このネットワーク全体が一体となってどのように機能するかを学んでいます。私のコードでは、複数のプレハブから作られた宇宙船を作成しています。
すべては 1 つの から始まり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