SendInstantiate
How do I use Send Instantiate
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: NetworkingPeer.cs
Copy
2228 internal Hashtable SendInstantiate(string prefabName, Vector3 position, Quaternion rotation, int group, int[] viewIDs, object[] data, bool isGlobalObject)
2229 {
2230 // first viewID is now also the gameobject's instantiateId
2231 int instantiateId = viewIDs[0]; // LIMITS PHOTONVIEWS&PLAYERS
2232
2233 //TODO: reduce hashtable key usage by using a parameter array for the various values
2234 Hashtable instantiateEvent = new Hashtable(); // This players info is sent via ActorID
2235 instantiateEvent[(byte)0] = prefabName;
2236
2237 if (position != Vector3.zero)
2238 {
2239 instantiateEvent[(byte)1] = position;
2240 }
2241
2242 if (rotation != Quaternion.identity)
2243 {
2244 instantiateEvent[(byte)2] = rotation;
2245 }
2246
2247 if (group != 0)
2248 {
2249 instantiateEvent[(byte)3] = group;
2250 }
2251
2252 // send the list of viewIDs only if there are more than one. else the instantiateId is the viewID
2253 if (viewIDs.Length > 1)
2254 {
2255 instantiateEvent[(byte)4] = viewIDs; // LIMITS PHOTONVIEWS&PLAYERS
2256 }
2257
2258 if (data != null)
2259 {
2260 instantiateEvent[(byte)5] = data;
2261 }
2262
2263 if (this.currentLevelPrefix > 0)
2264 {
2265 instantiateEvent[(byte)8] = this.currentLevelPrefix; // photonview's / object's level prefix
2266 }
2267
2268 instantiateEvent[(byte)6] = this.ServerTimeInMilliSeconds;
2269 instantiateEvent[(byte)7] = instantiateId;
2270
2271
2272 RaiseEventOptions options = new RaiseEventOptions();
2273 options.CachingOption = (isGlobalObject) ? EventCaching.AddToRoomCacheGlobal : EventCaching.AddToRoomCache;
2274
2275 this.OpRaiseEvent(PunEvent.Instantiation, instantiateEvent, true, options);
2276 return instantiateEvent;
2277 }
File name: PhotonNetwork.cs
Copy
2090 public static GameObject Instantiate(string prefabName, Vector3 position, Quaternion rotation, int group, object[] data)
2091 {
2092 if (!connected || (InstantiateInRoomOnly && !inRoom))
2093 {
2094 Debug.LogError("Failed to Instantiate prefab: " + prefabName + ". Client should be in a room. Current connectionStateDetailed: " + PhotonNetwork.connectionStateDetailed);
2095 return null;
2096 }
2097
2098 GameObject prefabGo;
2099 if (!UsePrefabCache || !PrefabCache.TryGetValue(prefabName, out prefabGo))
2100 {
2101 prefabGo = (GameObject)Resources.Load(prefabName, typeof(GameObject));
2102 if (UsePrefabCache)
2103 {
2104 PrefabCache.Add(prefabName, prefabGo);
2105 }
2106 }
2107
2108 if (prefabGo == null)
2109 {
2110 Debug.LogError("Failed to Instantiate prefab: " + prefabName + ". Verify the Prefab is in a Resources folder (and not in a subfolder)");
2111 return null;
2112 }
2113
2114 // a scene object instantiated with network visibility has to contain a PhotonView
2115 if (prefabGo.GetComponent
2116 {
2117 Debug.LogError("Failed to Instantiate prefab:" + prefabName + ". Prefab must have a PhotonView component.");
2118 return null;
2119 }
2120
2121 Component[] views = (Component[])prefabGo.GetPhotonViewsInChildren();
2122 int[] viewIDs = new int[views.Length];
2123 for (int i = 0; i < viewIDs.Length; i++)
2124 {
2125 //Debug.Log("Instantiate prefabName: " + prefabName + " player.ID: " + player.ID);
2126 viewIDs[i] = AllocateViewID(player.ID);
2127 }
2128
2129 // Send to others, create info
2130 Hashtable instantiateEvent = networkingPeer.SendInstantiate(prefabName, position, rotation, group, viewIDs, data, false);
2131
2132 // Instantiate the GO locally (but the same way as if it was done via event). This will also cache the instantiationId
2133 return networkingPeer.DoInstantiate(instantiateEvent, networkingPeer.mLocalActor, prefabGo);
2134 }
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 }
SendInstantiate 146 lượt xem
Gõ tìm kiếm nhanh...