GetPhotonView









How do I use Get Photon View
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: Extensions.cs Copy
22     public static PhotonView[] GetPhotonViewsInChildren(this UnityEngine.GameObject go)
23     {
24         return go.GetComponentsInChildren(true) as PhotonView[];
25     }
File name: Extensions.cs Copy
27     public static PhotonView GetPhotonView(this UnityEngine.GameObject go)
28     {
29         return go.GetComponent() as PhotonView;
30     }
File name: NetworkingPeer.cs Copy
1962     public void ExecuteRPC(Hashtable rpcData, PhotonPlayer sender)
1963     {
1964         if (rpcData == null || !rpcData.ContainsKey((byte)0))
1965         {
1966             Debug.LogError("Malformed RPC; this should never occur. Content: " + SupportClass.DictionaryToString(rpcData));
1967             return;
1968         }
1969
1970         // ts: updated with "flat" event data
1971         int netViewID = (int)rpcData[(byte)0]; // LIMITS PHOTONVIEWS&PLAYERS
1972         int otherSidePrefix = 0; // by default, the prefix is 0 (and this is not being sent)
1973         if (rpcData.ContainsKey((byte)1))
1974         {
1975             otherSidePrefix = (short)rpcData[(byte)1];
1976         }
1977
1978         string inMethodName;
1979         if (rpcData.ContainsKey((byte)5))
1980         {
1981             int rpcIndex = (byte)rpcData[(byte)5]; // LIMITS RPC COUNT
1982             if (rpcIndex > PhotonNetwork.PhotonServerSettings.RpcList.Count - 1)
1983             {
1984                 Debug.LogError("Could not find RPC with index: " + rpcIndex + ". Going to ignore! Check PhotonServerSettings.RpcList");
1985                 return;
1986             }
1987             else
1988             {
1989                 inMethodName = PhotonNetwork.PhotonServerSettings.RpcList[rpcIndex];
1990             }
1991         }
1992         else
1993         {
1994             inMethodName = (string)rpcData[(byte)3];
1995         }
1996
1997         object[] inMethodParameters = null;
1998         if (rpcData.ContainsKey((byte)4))
1999         {
2000             inMethodParameters = (object[])rpcData[(byte)4];
2001         }
2002
2003         if (inMethodParameters == null)
2004         {
2005             inMethodParameters = new object[0];
2006         }
2007
2008         PhotonView photonNetview = this.GetPhotonView(netViewID);
2009         if (photonNetview == null)
2010         {
2011             int viewOwnerId = netViewID/PhotonNetwork.MAX_VIEW_IDS;
2012             bool owningPv = (viewOwnerId == this.mLocalActor.ID);
2013             bool ownerSent = (viewOwnerId == sender.ID);
2014
2015             if (owningPv)
2016             {
2017                 Debug.LogWarning("Received RPC \"" + inMethodName + "\" for viewID " + netViewID + " but this PhotonView does not exist! View was/is ours." + (ownerSent ? " Owner called." : " Remote called.") + " By: " + sender.ID);
2018             }
2019             else
2020             {
2021                 Debug.LogWarning("Received RPC \"" + inMethodName + "\" for viewID " + netViewID + " but this PhotonView does not exist! Was remote PV." + (ownerSent ? " Owner called." : " Remote called.") + " By: " + sender.ID + " Maybe GO was destroyed but RPC not cleaned up.");
2022             }
2023             return;
2024         }
2025
2026         if (photonNetview.prefix != otherSidePrefix)
2027         {
2028             Debug.LogError(
2029                 "Received RPC \"" + inMethodName + "\" on viewID " + netViewID + " with a prefix of " + otherSidePrefix
2030                 + ", our prefix is " + photonNetview.prefix + ". The RPC has been ignored.");
2031             return;
2032         }
2033
2034         // Get method name
2035         if (inMethodName == string.Empty)
2036         {
2037             Debug.LogError("Malformed RPC; this should never occur. Content: " + SupportClass.DictionaryToString(rpcData));
2038             return;
2039         }
2040
2041         if (PhotonNetwork.logLevel >= PhotonLogLevel.Full)
2042             Debug.Log("Received RPC: " + inMethodName);
2043
2044
2045         // SetReceiving filtering
2046         if (photonNetview.group != 0 && !allowedReceivingGroups.Contains(photonNetview.group))
2047         {
2048             return; // Ignore group
2049         }
2050
2051         Type[] argTypes = new Type[0];
2052         if (inMethodParameters.Length > 0)
2053         {
2054             argTypes = new Type[inMethodParameters.Length];
2055             int i = 0;
2056             for (int index = 0; index < inMethodParameters.Length; index++)
2057             {
2058                 object objX = inMethodParameters[index];
2059                 if (objX == null)
2060                 {
2061                     argTypes[i] = null;
2062                 }
2063                 else
2064                 {
2065                     argTypes[i] = objX.GetType();
2066                 }
2067
2068                 i++;
2069             }
2070         }
2071
2072         int receivers = 0;
2073         int foundMethods = 0;
2074         MonoBehaviour[] mbComponents = photonNetview.GetComponents(); // NOTE: we could possibly also cache MonoBehaviours per view?!
2075         for (int componentsIndex = 0; componentsIndex < mbComponents.Length; componentsIndex++)
2076         {
2077             MonoBehaviour monob = mbComponents[componentsIndex];
2078             if (monob == null)
2079             {
2080                 Debug.LogError("ERROR You have missing MonoBehaviours on your gameobjects!");
2081                 continue;
2082             }
2083
2084             Type type = monob.GetType();
2085
2086             // Get [RPC] methods from cache
2087             List cachedRPCMethods = null;
2088             if (this.monoRPCMethodsCache.ContainsKey(type))
2089             {
2090                 cachedRPCMethods = this.monoRPCMethodsCache[type];
2091             }
2092
2093             if (cachedRPCMethods == null)
2094             {
2095                 List entries = SupportClass.GetMethods(type, typeof(RPC));
2096
2097                 this.monoRPCMethodsCache[type] = entries;
2098                 cachedRPCMethods = entries;
2099             }
2100
2101             if (cachedRPCMethods == null)
2102             {
2103                 continue;
2104             }
2105
2106             // Check cache for valid methodname+arguments
2107             for (int index = 0; index < cachedRPCMethods.Count; index++)
2108             {
2109                 MethodInfo mInfo = cachedRPCMethods[index];
2110                 if (mInfo.Name == inMethodName)
2111                 {
2112                     foundMethods++;
2113                     ParameterInfo[] pArray = mInfo.GetParameters();
2114                     if (pArray.Length == argTypes.Length)
2115                     {
2116                         // Normal, PhotonNetworkMessage left out
2117                         if (this.CheckTypeMatch(pArray, argTypes))
2118                         {
2119                             receivers++;
2120                             object result = mInfo.Invoke((object)monob, inMethodParameters);
2121                             if (mInfo.ReturnType == typeof(IEnumerator))
2122                             {
2123                                 monob.StartCoroutine((IEnumerator)result);
2124                             }
2125                         }
2126                     }
2127                     else if ((pArray.Length - 1) == argTypes.Length)
2128                     {
2129                         // Check for PhotonNetworkMessage being the last
2130                         if (this.CheckTypeMatch(pArray, argTypes))
2131                         {
2132                             if (pArray[pArray.Length - 1].ParameterType == typeof(PhotonMessageInfo))
2133                             {
2134                                 receivers++;
2135
2136                                 int sendTime = (int)rpcData[(byte)2];
2137                                 object[] deParamsWithInfo = new object[inMethodParameters.Length + 1];
2138                                 inMethodParameters.CopyTo(deParamsWithInfo, 0);
2139                                 deParamsWithInfo[deParamsWithInfo.Length - 1] = new PhotonMessageInfo(sender, sendTime, photonNetview);
2140
2141                                 object result = mInfo.Invoke((object)monob, deParamsWithInfo);
2142                                 if (mInfo.ReturnType == typeof(IEnumerator))
2143                                 {
2144                                     monob.StartCoroutine((IEnumerator)result);
2145                                 }
2146                             }
2147                         }
2148                     }
2149                     else if (pArray.Length == 1 && pArray[0].ParameterType.IsArray)
2150                     {
2151                         receivers++;
2152                         object result = mInfo.Invoke((object)monob, new object[] { inMethodParameters });
2153                         if (mInfo.ReturnType == typeof(IEnumerator))
2154                         {
2155                             monob.StartCoroutine((IEnumerator)result);
2156                         }
2157                     }
2158                 }
2159             }
2160         }
2161
2162         // Error handling
2163         if (receivers != 1)
2164         {
2165             string argsString = string.Empty;
2166             for (int index = 0; index < argTypes.Length; index++)
2167             {
2168                 Type ty = argTypes[index];
2169                 if (argsString != string.Empty)
2170                 {
2171                     argsString += ", ";
2172                 }
2173
2174                 if (ty == null)
2175                 {
2176                     argsString += "null";
2177                 }
2178                 else
2179                 {
2180                     argsString += ty.Name;
2181                 }
2182             }
2183
2184             if (receivers == 0)
2185             {
2186                 if (foundMethods == 0)
2187                 {
2188                     Debug.LogError("PhotonView with ID " + netViewID + " has no method \"" + inMethodName + "\" marked with the [RPC](C#) or @RPC(JS) property! Args: " + argsString);
2189                 }
2190                 else
2191                 {
2192                     Debug.LogError("PhotonView with ID " + netViewID + " has no method \"" + inMethodName + "\" that takes " + argTypes.Length + " argument(s): " + argsString);
2193                 }
2194             }
2195             else
2196             {
2197                 Debug.LogError("PhotonView with ID " + netViewID + " has " + receivers + " methods \"" + inMethodName + "\" that takes " + argTypes.Length + " argument(s): " + argsString + ". Should be just one?");
2198             }
2199         }
2200     }
File name: NetworkingPeer.cs Copy
2279     internal GameObject DoInstantiate(Hashtable evData, PhotonPlayer photonPlayer, GameObject resourceGameObject)
2280     {
2281         // some values always present:
2282         string prefabName = (string)evData[(byte)0];
2283         int serverTime = (int)evData[(byte)6];
2284         int instantiationId = (int)evData[(byte)7];
2285
2286         Vector3 position;
2287         if (evData.ContainsKey((byte)1))
2288         {
2289             position = (Vector3)evData[(byte)1];
2290         }
2291         else
2292         {
2293             position = Vector3.zero;
2294         }
2295
2296         Quaternion rotation = Quaternion.identity;
2297         if (evData.ContainsKey((byte)2))
2298         {
2299             rotation = (Quaternion)evData[(byte)2];
2300         }
2301
2302         int group = 0;
2303         if (evData.ContainsKey((byte)3))
2304         {
2305             group = (int)evData[(byte)3];
2306         }
2307
2308         short objLevelPrefix = 0;
2309         if (evData.ContainsKey((byte)8))
2310         {
2311             objLevelPrefix = (short)evData[(byte)8];
2312         }
2313
2314         int[] viewsIDs;
2315         if (evData.ContainsKey((byte)4))
2316         {
2317             viewsIDs = (int[])evData[(byte)4];
2318         }
2319         else
2320         {
2321             viewsIDs = new int[1] { instantiationId };
2322         }
2323
2324         object[] incomingInstantiationData;
2325         if (evData.ContainsKey((byte)5))
2326         {
2327             incomingInstantiationData = (object[])evData[(byte)5];
2328         }
2329         else
2330         {
2331             incomingInstantiationData = null;
2332         }
2333
2334         // SetReceiving filtering
2335         if (group != 0 && !this.allowedReceivingGroups.Contains(group))
2336         {
2337             return null; // Ignore group
2338         }
2339
2340         // load prefab, if it wasn't loaded before (calling methods might do this)
2341         if (resourceGameObject == null)
2342         {
2343             if (!NetworkingPeer.UsePrefabCache || !NetworkingPeer.PrefabCache.TryGetValue(prefabName, out resourceGameObject))
2344             {
2345                 resourceGameObject = (GameObject)Resources.Load(prefabName, typeof(GameObject));
2346                 if (NetworkingPeer.UsePrefabCache)
2347                 {
2348                     NetworkingPeer.PrefabCache.Add(prefabName, resourceGameObject);
2349                 }
2350             }
2351
2352             if (resourceGameObject == null)
2353             {
2354                 Debug.LogError("PhotonNetwork error: Could not Instantiate the prefab [" + prefabName + "]. Please verify you have this gameobject in a Resources folder.");
2355                 return null;
2356             }
2357         }
2358
2359         // now modify the loaded "blueprint" object before it becomes a part of the scene (by instantiating it)
2360         PhotonView[] resourcePVs = resourceGameObject.GetPhotonViewsInChildren();
2361         if (resourcePVs.Length != viewsIDs.Length)
2362         {
2363             throw new Exception("Error in Instantiation! The resource's PhotonView count is not the same as in incoming data.");
2364         }
2365
2366         for (int i = 0; i < viewsIDs.Length; i++)
2367         {
2368             // NOTE instantiating the loaded resource will keep the viewID but would not copy instantiation data, so it's set below
2369             // so we only set the viewID and instantiationId now. the instantiationData can be fetched
2370             resourcePVs[i].viewID = viewsIDs[i];
2371             resourcePVs[i].prefix = objLevelPrefix;
2372             resourcePVs[i].instantiationId = instantiationId;
2373             resourcePVs[i].isRuntimeInstantiated = true;
2374         }
2375
2376         this.StoreInstantiationData(instantiationId, incomingInstantiationData);
2377
2378         // load the resource and set it's values before instantiating it:
2379         GameObject go = (GameObject)GameObject.Instantiate(resourceGameObject, position, rotation);
2380
2381         for (int i = 0; i < viewsIDs.Length; i++)
2382         {
2383             // NOTE instantiating the loaded resource will keep the viewID but would not copy instantiation data, so it's set below
2384             // so we only set the viewID and instantiationId now. the instantiationData can be fetched
2385             resourcePVs[i].viewID = 0;
2386             resourcePVs[i].prefix = -1;
2387             resourcePVs[i].prefixBackup = -1;
2388             resourcePVs[i].instantiationId = -1;
2389             resourcePVs[i].isRuntimeInstantiated = false;
2390         }
2391
2392         this.RemoveInstantiationData(instantiationId);
2393
2394         // Send OnPhotonInstantiate callback to newly created GO.
2395         // GO will be enabled when instantiated from Prefab and it does not matter if the script is enabled or disabled.
2396         go.SendMessage(PhotonNetworkingMessage.OnPhotonInstantiate.ToString(), new PhotonMessageInfo(photonPlayer, serverTime, null), SendMessageOptions.DontRequireReceiver);
2397         return go;
2398     }
File name: NetworkingPeer.cs Copy
2567     public int GetInstantiatedObjectsId(GameObject go)
2568     {
2569         int id = -1;
2570         if (go == null)
2571         {
2572             Debug.LogError("GetInstantiatedObjectsId() for GO == null.");
2573             return id;
2574         }
2575
2576         PhotonView[] pvs = go.GetPhotonViewsInChildren();
2577         if (pvs != null && pvs.Length > 0 && pvs[0] != null)
2578         {
2579             return pvs[0].instantiationId;
2580         }
2581
2582         if (PhotonNetwork.logLevel >= PhotonLogLevel.Informational)
2583             UnityEngine.Debug.Log("GetInstantiatedObjectsId failed for GO: " + go);
2584
2585
2586         return id;
2587     }
File name: NetworkingPeer.cs Copy
2663     public PhotonView GetPhotonView(int viewID)
2664     {
2665         PhotonView result = null;
2666         this.photonViewList.TryGetValue(viewID, out result);
2667
2668         if (result == null)
2669         {
2670             PhotonView[] views = GameObject.FindObjectsOfType(typeof(PhotonView)) as PhotonView[];
2671
2672             foreach (PhotonView view in views)
2673             {
2674                 if (view.viewID == viewID)
2675                 {
2676                     if (view.didAwake)
2677                     {
2678                         Debug.LogWarning("Had to lookup view that wasn't in photonViewList: " + view);
2679                     }
2680                     return view;
2681                 }
2682             }
2683         }
2684
2685         return result;
2686     }
File name: NetworkingPeer.cs Copy
3339     private void OnSerializeRead(Hashtable data, PhotonPlayer sender, int networkTime, short correctPrefix)
3340     {
3341         // read view ID from key (byte)0: a int-array (PUN 1.17++)
3342         int viewID = (int)data[(byte)0];
3343
3344
3345         PhotonView view = this.GetPhotonView(viewID);
3346         if (view == null)
3347         {
3348             Debug.LogWarning("Received OnSerialization for view ID " + viewID + ". We have no such PhotonView! Ignored this if you're leaving a room. State: " + this.State);
3349             return;
3350         }
3351
3352         if (view.prefix > 0 && correctPrefix != view.prefix)
3353         {
3354             Debug.LogError("Received OnSerialization for view ID " + viewID + " with prefix " + correctPrefix + ". Our prefix is " + view.prefix);
3355             return;
3356         }
3357
3358         // SetReceiving filtering
3359         if (view.group != 0 && !this.allowedReceivingGroups.Contains(view.group))
3360         {
3361             return; // Ignore group
3362         }
3363
3364
3365         if (view.synchronization == ViewSynchronization.ReliableDeltaCompressed)
3366         {
3367             if (!this.DeltaCompressionRead(view, data))
3368             {
3369                 // Skip this packet as we haven't got received complete-copy of this view yet.
3370                 if (PhotonNetwork.logLevel >= PhotonLogLevel.Informational)
3371                     Debug.Log("Skipping packet for " + view.name + " [" + view.viewID + "] as we haven't received a full packet for delta compression yet. This is OK if it happens for the first few frames after joining a game.");
3372                 return;
3373             }
3374
3375             // store last received for delta-compression usage
3376             view.lastOnSerializeDataReceived = data[(byte)1] as object[];
3377         }
3378
3379         if (sender.ID != view.ownerId)
3380         {
3381             if (!view.isSceneView || !sender.isMasterClient)
3382             {
3383                 // obviously the owner changed and we didn't yet notice.
3384                 Debug.Log("Adjusting owner to sender of updates. From: " + view.ownerId + " to: " + sender.ID);
3385                 view.ownerId = sender.ID;
3386             }
3387         }
3388
3389         object[] contents = data[(byte)1] as object[];
3390         PhotonStream pStream = new PhotonStream(false, contents);
3391         PhotonMessageInfo info = new PhotonMessageInfo(sender, networkTime, view);
3392
3393         view.DeserializeView( pStream, info );
3394     }
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() == null)
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() == null)
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: PhotonNetwork.cs Copy
2291     public static void Destroy(PhotonView targetView)
2292     {
2293         if (targetView != null)
2294         {
2295             networkingPeer.RemoveInstantiatedGO(targetView.gameObject, !inRoom);
2296         }
2297         else
2298         {
2299             Debug.LogError("Destroy(targetPhotonView) failed, cause targetPhotonView is null.");
2300         }
2301     }

GetPhotonView 359 lượt xem

Gõ tìm kiếm nhanh...