InstantiateSceneObject
How do I use Instantiate Scene Object
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: Demo2DJumpAndRun.cs
Copy
6 void OnJoinedRoom()
7 {
8 if( PhotonNetwork.isMasterClient == false )
9 {
10 return;
11 }
12
13 PhotonNetwork.InstantiateSceneObject( "Physics Box", new Vector3( -4.5f, 5.5f, 0 ), Quaternion.identity, 0, null );
14 PhotonNetwork.InstantiateSceneObject( "Physics Box", new Vector3( -4.5f, 4.5f, 0 ), Quaternion.identity, 0, null );
15 PhotonNetwork.InstantiateSceneObject( "Physics Box", new Vector3( -4.5f, 3.5f, 0 ), Quaternion.identity, 0, null );
16
17 PhotonNetwork.InstantiateSceneObject( "Physics Box", new Vector3( 4.5f, 5.5f, 0 ), Quaternion.identity, 0, null );
18 PhotonNetwork.InstantiateSceneObject( "Physics Box", new Vector3( 4.5f, 4.5f, 0 ), Quaternion.identity, 0, null );
19 PhotonNetwork.InstantiateSceneObject( "Physics Box", new Vector3( 4.5f, 3.5f, 0 ), Quaternion.identity, 0, null );
20 }
File name: InstantiateCube.cs
Copy
12 void OnClick()
13 {
14 if (PhotonNetwork.connectionStateDetailed != PeerState.Joined)
15 {
16 // only use PhotonNetwork.Instantiate while in a room.
17 return;
18 }
19
20 switch (InstantiateType)
21 {
22 case 0:
23 PhotonNetwork.Instantiate(Prefab.name, this.transform.position + 3*Vector3.up, Quaternion.identity, 0);
24 break;
25 case 1:
26 PhotonNetwork.InstantiateSceneObject(Prefab.name, InputToEvent.inputHitPos + new Vector3(0, 5f, 0), Quaternion.identity, 0, null);
27 break;
28 }
29 }
File name: OnJoinInstantiate.cs
Copy
13 public void OnJoinedRoom()
14 {
15 Vector3 pos = Vector3.zero;
16 pos.x += PhotonNetwork.player.ID;
17
18 if (!InstantiateSceneObjects)
19 {
20
21 newObj = PhotonNetwork.Instantiate(ObjectToInstantiate.name, pos, Quaternion.identity, 0, null);
22
23
24 // anything you do with newObj locally is not reflected on the other clients.
25 // you can add a script to the Prefab to do some instantiation in Awake() and you can call RPCs on newObj now.
26 }
27 else
28 {
29 newObj = PhotonNetwork.InstantiateSceneObject(ObjectToInstantiate.name, pos, Quaternion.identity, 0, null);
30 //PhotonView pv = newObj.GetComponent
31 //Debug.Log(pv.ownerId + " " + pv.viewID);
32 }
33 }
File name: PhotonNetwork.cs
Copy
2150 public static GameObject InstantiateSceneObject(string prefabName, Vector3 position, Quaternion rotation, int group, object[] data)
2151 {
2152 if (!connected || (InstantiateInRoomOnly && !inRoom))
2153 {
2154 Debug.LogError("Failed to InstantiateSceneObject prefab: " + prefabName + ". Client should be in a room. Current connectionStateDetailed: " + PhotonNetwork.connectionStateDetailed);
2155 return null;
2156 }
2157
2158 if (!isMasterClient)
2159 {
2160 Debug.LogError("Failed to InstantiateSceneObject prefab: " + prefabName + ". Client is not the MasterClient in this room.");
2161 return null;
2162 }
2163
2164 GameObject prefabGo;
2165 if (!UsePrefabCache || !PrefabCache.TryGetValue(prefabName, out prefabGo))
2166 {
2167 prefabGo = (GameObject)Resources.Load(prefabName, typeof(GameObject));
2168 if (UsePrefabCache)
2169 {
2170 PrefabCache.Add(prefabName, prefabGo);
2171 }
2172 }
2173
2174 if (prefabGo == null)
2175 {
2176 Debug.LogError("Failed to InstantiateSceneObject prefab: " + prefabName + ". Verify the Prefab is in a Resources folder (and not in a subfolder)");
2177 return null;
2178 }
2179
2180 // a scene object instantiated with network visibility has to contain a PhotonView
2181 if (prefabGo.GetComponent
2182 {
2183 Debug.LogError("Failed to InstantiateSceneObject prefab:" + prefabName + ". Prefab must have a PhotonView component.");
2184 return null;
2185 }
2186
2187 Component[] views = (Component[])prefabGo.GetPhotonViewsInChildren();
2188 int[] viewIDs = AllocateSceneViewIDs(views.Length);
2189
2190 if (viewIDs == null)
2191 {
2192 Debug.LogError("Failed to InstantiateSceneObject prefab: " + prefabName + ". No ViewIDs are free to use. Max is: " + MAX_VIEW_IDS);
2193 return null;
2194 }
2195
2196 // Send to others, create info
2197 Hashtable instantiateEvent = networkingPeer.SendInstantiate(prefabName, position, rotation, group, viewIDs, data, true);
2198
2199 // Instantiate the GO locally (but the same way as if it was done via event). This will also cache the instantiationId
2200 return networkingPeer.DoInstantiate(instantiateEvent, networkingPeer.mLocalActor, prefabGo);
2201 }
File name: OnClickInstantiate.cs
Copy
12 void OnClick()
13 {
14 if (PhotonNetwork.connectionStateDetailed != PeerState.Joined)
15 {
16 // only use PhotonNetwork.Instantiate while in a room.
17 return;
18 }
19
20 switch (InstantiateType)
21 {
22 case 0:
23 PhotonNetwork.Instantiate(Prefab.name, InputToEvent.inputHitPos + new Vector3(0, 5f, 0), Quaternion.identity, 0);
24 break;
25 case 1:
26 PhotonNetwork.InstantiateSceneObject(Prefab.name, InputToEvent.inputHitPos + new Vector3(0, 5f, 0), Quaternion.identity, 0, null);
27 break;
28 }
29 }
InstantiateSceneObject 202 lượt xem
Gõ tìm kiếm nhanh...