AllocateViewID









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

Featured Snippets


File name: PhotonNetwork.cs Copy
1959     public static int AllocateViewID()
1960     {
1961         int manualId = AllocateViewID(player.ID);
1962         manuallyAllocatedViewIds.Add(manualId);
1963         return manualId;
1964     }
File name: PhotonNetwork.cs Copy
1971     public static int AllocateSceneViewID()
1972     {
1973         if (!PhotonNetwork.isMasterClient)
1974         {
1975             Debug.LogError("Only the Master Client can AllocateSceneViewID(). Check PhotonNetwork.isMasterClient!");
1976             return -1;
1977         }
1978
1979         int manualId = AllocateViewID(0);
1980         manuallyAllocatedViewIds.Add(manualId);
1981         return manualId;
1982     }
File name: PhotonNetwork.cs Copy
1988     public static void UnAllocateViewID(int viewID)
1989     {
1990         manuallyAllocatedViewIds.Remove(viewID);
1991
1992         if (networkingPeer.photonViewList.ContainsKey(viewID))
1993         {
1994             Debug.LogWarning(string.Format("UnAllocateViewID() should be called after the PhotonView was destroyed (GameObject.Destroy()). ViewID: {0} still found in: {1}", viewID, networkingPeer.photonViewList[viewID]));
1995         }
1996     }
File name: PhotonNetwork.cs Copy
1999     // returns viewID (combined owner and sub id)
2000     private static int AllocateViewID(int ownerId)
2001     {
2002         if (ownerId == 0)
2003         {
2004             // we look up a fresh subId for the owner "room" (mind the "sub" in subId)
2005             int newSubId = lastUsedViewSubIdStatic;
2006             int newViewId;
2007             int ownerIdOffset = ownerId * MAX_VIEW_IDS;
2008             for (int i = 1; i < MAX_VIEW_IDS; i++)
2009             {
2010                 newSubId = (newSubId + 1) % MAX_VIEW_IDS;
2011                 if (newSubId == 0)
2012                 {
2013                     continue; // avoid using subID 0
2014                 }
2015
2016                 newViewId = newSubId + ownerIdOffset;
2017                 if (!networkingPeer.photonViewList.ContainsKey(newViewId))
2018                 {
2019                     lastUsedViewSubIdStatic = newSubId;
2020                     return newViewId;
2021                 }
2022             }
2023
2024             // this is the error case: we didn't find any (!) free subId for this user
2025             throw new Exception(string.Format("AllocateViewID() failed. Room (user {0}) is out of 'scene' viewIDs. It seems all available are in use.", ownerId));
2026         }
2027         else
2028         {
2029             // we look up a fresh SUBid for the owner
2030             int newSubId = lastUsedViewSubId;
2031             int newViewId;
2032             int ownerIdOffset = ownerId * MAX_VIEW_IDS;
2033             for (int i = 1; i < MAX_VIEW_IDS; i++)
2034             {
2035                 newSubId = (newSubId + 1) % MAX_VIEW_IDS;
2036                 if (newSubId == 0)
2037                 {
2038                     continue; // avoid using subID 0
2039                 }
2040
2041                 newViewId = newSubId + ownerIdOffset;
2042                 if (!networkingPeer.photonViewList.ContainsKey(newViewId) && !manuallyAllocatedViewIds.Contains(newViewId))
2043                 {
2044                     lastUsedViewSubId = newSubId;
2045                     return newViewId;
2046                 }
2047             }
2048
2049             throw new Exception(string.Format("AllocateViewID() failed. User {0} is out of subIds, as all viewIDs are used.", ownerId));
2050         }
2051     }
File name: PhotonNetwork.cs Copy
2053     private static int[] AllocateSceneViewIDs(int countOfNewViews)
2054     {
2055         int[] viewIDs = new int[countOfNewViews];
2056         for (int view = 0; view < countOfNewViews; view++)
2057         {
2058             viewIDs[view] = AllocateViewID(0);
2059         }
2060
2061         return viewIDs;
2062     }
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: PhotonView.cs Copy
295     protected internal void OnDestroy()
296     {
297         if (!this.destroyedByPhotonNetworkOrQuit)
298         {
299             PhotonNetwork.networkingPeer.LocalCleanPhotonView(this);
300         }
301
302         if (!this.destroyedByPhotonNetworkOrQuit && !Application.isLoadingLevel)
303         {
304             if (this.instantiationId > 0)
305             {
306                 // if this viewID was not manually assigned (and we're not shutting down or loading a level), you should use PhotonNetwork.Destroy() to get rid of GOs with PhotonViews
307                 Debug.LogError("OnDestroy() seems to be called without PhotonNetwork.Destroy()?! GameObject: " + this.gameObject + " Application.isLoadingLevel: " + Application.isLoadingLevel);
308             }
309             else
310             {
311                 // this seems to be a manually instantiated PV. if it's local, we could warn if the ID is not in the allocated-list
312                 if (this.viewID <= 0)
313                 {
314                     Debug.LogWarning(string.Format("OnDestroy manually allocated PhotonView {0}. The viewID is 0. Was it ever (manually) set?", this));
315                 }
316                 else if (this.isMine && !PhotonNetwork.manuallyAllocatedViewIds.Contains(this.viewID))
317                 {
318                     Debug.LogWarning(string.Format("OnDestroy manually allocated PhotonView {0}. The viewID is local (isMine) but not in manuallyAllocatedViewIds list. Use UnAllocateViewID() after you destroyed the PV.", this));
319                 }
320             }
321         }
322     }
File name: ManualPhotonViewAllocator.cs Copy
9     public void AllocateManualPhotonView()
10     {
11         PhotonView pv = this.gameObject.GetPhotonView();
12         if (pv == null)
13         {
14             Debug.LogError("Can't do manual instantiation without PhotonView component.");
15             return;
16         }
17
18         int viewID = PhotonNetwork.AllocateViewID();
19         pv.RPC("InstantiateRpc", PhotonTargets.AllBuffered, viewID);
20     }
File name: OnClickDestroy.cs Copy
39     public IEnumerator DestroyRpc()
40     {
41         GameObject.Destroy(this.gameObject);
42         yield return 0; // if you allow 1 frame to pass, the object's OnDestroy() method gets called and cleans up references.
43         PhotonNetwork.UnAllocateViewID(this.photonView.viewID);
44     }

AllocateViewID 172 lượt xem

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