protected virtual void Awake() { var photonView = this.GetComponent <PhotonView>(); if (photonView && photonView.ViewID > 0) { NetworkManager.Register(this); } this.AutoSync = this.gameObject.AddComponent <AutoSync>(); this.AutoSync.ObjectToSync = this; this.AutoSync.SyncCompleted += (sender, keys) => AfterSync(keys); this.photonView.Group = 1; }
public (GameObject gameObject, int viewId) RpcInstantiate(string prefabName, Vector3 position, Quaternion rotation, ViewIdAllocationMethod method, int existingViewId) { var gameObject = UnityEngine.Object.Instantiate(Resources.Load <GameObject>(prefabName), position, rotation); gameObject.AddComponent <SpawnedObject>().ResourceName = prefabName; if (method != ViewIdAllocationMethod.Static) { var photonView = gameObject.GetComponent <PhotonView>(); if (photonView) { switch (method) { case ViewIdAllocationMethod.Specific: photonView.ViewID = existingViewId; break; case ViewIdAllocationMethod.Local: PhotonNetwork.AllocateViewID(photonView); existingViewId = photonView.ViewID; break; case ViewIdAllocationMethod.Scene: PhotonNetwork.AllocateSceneViewID(photonView); existingViewId = photonView.ViewID; break; } } else { Debug.LogWarning("== Instantiated object has no PhotonView and will not be networked."); } var networkedObject = gameObject.GetComponent <INetworkedObject>(); if (!(networkedObject == null || networkedObject.Equals(null))) { NetworkManager.Register(networkedObject); } } return(gameObject, existingViewId); }