Loaded









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

Featured Snippets


File name: PunStartup.cs Copy
34     static void OnUpdate()
35     {
36         bool doneBefore = EditorPrefs.GetBool("PunDemosOpenedBefore");
37         if (doneBefore)
38         {
39             EditorApplication.update -= OnUpdate;
40             return;
41         }
42
43         if (String.IsNullOrEmpty(EditorApplication.currentScene) && EditorBuildSettings.scenes.Length == 0)
44         {
45             #if UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_1 || UNITY_5_2
46             if (EditorApplication.isUpdating) return;
47             #endif
48
49             LoadPunDemoHub();
50             SetPunDemoBuildSettings();
51             EditorPrefs.SetBool("PunDemosOpenedBefore", true);
52             Debug.Log("No scene was open. Loaded PUN Demo Hub Scene and added demos to build settings. Ready to go! This auto-setup is now disabled in this Editor.");
53         }
54     }
File name: ToHubButton.cs Copy
21     public void OnGUI()
22     {
23         if (Application.loadedLevel != 0)
24         {
25             int w = ButtonTexture.width + 4;
26             int h = ButtonTexture.height + 4;
27
28             ButtonRect = new Rect(Screen.width - w, Screen.height - h, w, h);
29             if (GUI.Button(ButtonRect, ButtonTexture, GUIStyle.none))
30             {
31                 PhotonNetwork.Disconnect();
32                 Application.LoadLevel(0);
33             }
34         }
35     }
File name: IELdemo.cs Copy
38     // This is one of the callback/event methods called by PUN (read more in PhotonNetworkingMessage enumeration)
39     public void OnCreatedRoom()
40     {
41         Application.LoadLevel(Application.loadedLevel);
42     }
File name: PhotonViewHandler.cs Copy
25     internal static void HierarchyChange()
26     {
27         if (Application.isPlaying)
28         {
29             //Debug.Log("HierarchyChange ignored, while running.");
30             CheckSceneForStuckHandlers = true; // done once AFTER play mode.
31             return;
32         }
33
34         if (CheckSceneForStuckHandlers)
35         {
36             CheckSceneForStuckHandlers = false;
37             PhotonNetwork.InternalCleanPhotonMonoFromSceneIfStuck();
38         }
39
40         HashSet pvInstances = new HashSet();
41         HashSet usedInstanceViewNumbers = new HashSet();
42         bool fixedSomeId = false;
43
44         //// the following code would be an option if we only checked scene objects (but we can check all PVs)
45         //PhotonView[] pvObjects = GameObject.FindSceneObjectsOfType(typeof(PhotonView)) as PhotonView[];
46         //Debug.Log("HierarchyChange. PV Count: " + pvObjects.Length);
47
48         string levelName = Application.loadedLevelName;
49         #if UNITY_EDITOR
50         levelName = System.IO.Path.GetFileNameWithoutExtension(EditorApplication.currentScene);
51         #endif
52         int minViewIdInThisScene = PunSceneSettings.MinViewIdForScene(levelName);
53         //Debug.Log("Level '" + Application.loadedLevelName + "' has a minimum ViewId of: " + minViewIdInThisScene);
54
55         PhotonView[] pvObjects = Resources.FindObjectsOfTypeAll(typeof(PhotonView)) as PhotonView[];
56
57         foreach (PhotonView view in pvObjects)
58         {
59             // first pass: fix prefabs to viewID 0 if they got a view number assigned (cause they should not have one!)
60             if (EditorUtility.IsPersistent(view.gameObject))
61             {
62                 if (view.viewID != 0 || view.prefixBackup != -1 || view.instantiationId != -1)
63                 {
64                     Debug.LogWarning("PhotonView on persistent object being fixed (id and prefix must be 0). Was: " + view);
65                     view.viewID = 0;
66                     view.prefixBackup = -1;
67                     view.instantiationId = -1;
68                     EditorUtility.SetDirty(view);
69                     fixedSomeId = true;
70                 }
71             }
72             else
73             {
74                 // keep all scene-instanced PVs for later re-check
75                 pvInstances.Add(view);
76             }
77         }
78
79         Dictionary idPerObject = new Dictionary();
80
81         // second pass: check all used-in-scene viewIDs for duplicate viewIDs (only checking anything non-prefab)
82         // scene-PVs must have user == 0 (scene/room) and a subId != 0
83         foreach (PhotonView view in pvInstances)
84         {
85             if (view.ownerId > 0)
86             {
87                 Debug.Log("Re-Setting Owner ID of: " + view);
88             }
89             view.ownerId = 0; // simply make sure no owner is set (cause room always uses 0)
90             view.prefix = -1; // TODO: prefix could be settable via inspector per scene?!
91
92             if (view.viewID != 0)
93             {
94                 if (view.viewID < minViewIdInThisScene || usedInstanceViewNumbers.Contains(view.viewID))
95                 {
96                     view.viewID = 0; // avoid duplicates and negative values by assigning 0 as (temporary) number to be fixed in next pass
97                 }
98                 else
99                 {
100                     usedInstanceViewNumbers.Add(view.viewID); // builds a list of currently used viewIDs
101
102                     int instId = 0;
103                     if (idPerObject.TryGetValue(view.gameObject, out instId))
104                     {
105                         view.instantiationId = instId;
106                     }
107                     else
108                     {
109                         view.instantiationId = view.viewID;
110                         idPerObject[view.gameObject] = view.instantiationId;
111                     }
112                 }
113             }
114
115         }
116
117         // third pass: anything that's now 0 must get a new (not yet used) ID (starting at 0)
118         int lastUsedId = (minViewIdInThisScene > 0) ? minViewIdInThisScene - 1 : 0;
119
120         foreach (PhotonView view in pvInstances)
121         {
122             if (view.viewID == 0)
123             {
124                 // Debug.LogWarning("setting scene ID: " + view.gameObject.name + " ID: " + view.subId.ID + " scene ID: " + view.GetSceneID() + " IsPersistent: " + EditorUtility.IsPersistent(view.gameObject) + " IsSceneViewIDFree: " + IsSceneViewIDFree(view.subId.ID, view));
125                 int nextViewId = PhotonViewHandler.GetID(lastUsedId, usedInstanceViewNumbers);
126
127                 view.viewID = nextViewId;
128
129                 int instId = 0;
130                 if (idPerObject.TryGetValue(view.gameObject, out instId))
131                 {
132                     Debug.Log("Set inst ID");
133                     view.instantiationId = instId;
134                 }
135                 else
136                 {
137                     view.instantiationId = view.viewID;
138                     idPerObject[view.gameObject] = nextViewId;
139                 }
140
141                 //// when using the Editor's serialization (view.subId in this case), this is not needed, it seems
142                 //PrefabUtility.RecordPrefabInstancePropertyModifications(view);
143
144                 lastUsedId = nextViewId;
145                 EditorUtility.SetDirty(view);
146                 fixedSomeId = true;
147             }
148         }
149
150
151         if (fixedSomeId)
152         {
153             //Debug.LogWarning("Some subId was adjusted."); // this log is only interesting for Exit Games
154         }
155     }
File name: NetworkingPeer.cs Copy
3113     public void NewSceneLoaded()
3114     {
3115         if (this.loadingLevelAndPausedNetwork)
3116         {
3117             this.loadingLevelAndPausedNetwork = false;
3118             PhotonNetwork.isMessageQueueRunning = true;
3119         }
3120         // Debug.Log("OnLevelWasLoaded photonViewList.Count: " + photonViewList.Count); // Exit Games internal log
3121
3122         List removeKeys = new List();
3123         foreach (KeyValuePair kvp in this.photonViewList)
3124         {
3125             PhotonView view = kvp.Value;
3126             if (view == null)
3127             {
3128                 removeKeys.Add(kvp.Key);
3129             }
3130         }
3131
3132         for (int index = 0; index < removeKeys.Count; index++)
3133         {
3134             int key = removeKeys[index];
3135             this.photonViewList.Remove(key);
3136         }
3137
3138         if (removeKeys.Count > 0)
3139         {
3140             if (PhotonNetwork.logLevel >= PhotonLogLevel.Informational)
3141                 Debug.Log("New level loaded. Removed " + removeKeys.Count + " scene view IDs from last level.");
3142         }
3143     }
File name: NetworkingPeer.cs Copy
3630     internal protected void LoadLevelIfSynced()
3631     {
3632         if (!PhotonNetwork.automaticallySyncScene || PhotonNetwork.isMasterClient || PhotonNetwork.room == null)
3633         {
3634             return;
3635         }
3636
3637         // check if "current level" is set in props
3638         if (!PhotonNetwork.room.customProperties.ContainsKey(NetworkingPeer.CurrentSceneProperty))
3639         {
3640             return;
3641         }
3642
3643         // if loaded level is not the one defined my master in props, load that level
3644         object sceneId = PhotonNetwork.room.customProperties[NetworkingPeer.CurrentSceneProperty];
3645         if (sceneId is int)
3646         {
3647             if (Application.loadedLevel != (int)sceneId)
3648                 PhotonNetwork.LoadLevel((int)sceneId);
3649         }
3650         else if (sceneId is string)
3651         {
3652             if (Application.loadedLevelName != (string)sceneId)
3653                 PhotonNetwork.LoadLevel((string)sceneId);
3654         }
3655     }
File name: NetworkingPeer.cs Copy
3657     protected internal void SetLevelInPropsIfSynced(object levelId)
3658     {
3659         if (!PhotonNetwork.automaticallySyncScene || !PhotonNetwork.isMasterClient || PhotonNetwork.room == null)
3660         {
3661             return;
3662         }
3663         if (levelId == null)
3664         {
3665             Debug.LogError("Parameter levelId can't be null!");
3666             return;
3667         }
3668
3669         // check if "current level" is already set in props
3670         if (PhotonNetwork.room.customProperties.ContainsKey(NetworkingPeer.CurrentSceneProperty))
3671         {
3672             object levelIdInProps = PhotonNetwork.room.customProperties[NetworkingPeer.CurrentSceneProperty];
3673             if (levelIdInProps is int && Application.loadedLevel == (int)levelIdInProps)
3674             {
3675                 return;
3676             }
3677             if (levelIdInProps is string && Application.loadedLevelName.Equals((string)levelIdInProps))
3678             {
3679                 return;
3680             }
3681         }
3682
3683         // current level is not yet in props, so this client has to set it
3684         Hashtable setScene = new Hashtable();
3685         if (levelId is int) setScene[NetworkingPeer.CurrentSceneProperty] = (int)levelId;
3686         else if (levelId is string) setScene[NetworkingPeer.CurrentSceneProperty] = (string)levelId;
3687         else Debug.LogError("Parameter levelId must be int or string!");
3688
3689         PhotonNetwork.room.SetCustomProperties(setScene);
3690         this.SendOutgoingCommands(); // send immediately! because: in most cases the client will begin to load and not send for a while
3691     }
File name: PhotonHandler.cs Copy
112     protected void OnLevelWasLoaded(int level)
113     {
114         PhotonNetwork.networkingPeer.NewSceneLoaded();
115         PhotonNetwork.networkingPeer.SetLevelInPropsIfSynced(Application.loadedLevelName);
116     }
File name: PhotonHandler.cs Copy
123     protected void OnCreatedRoom()
124     {
125         PhotonNetwork.networkingPeer.SetLevelInPropsIfSynced(Application.loadedLevelName);
126     }
File name: PhotonHandler.cs Copy
199     {
200         get
201         {
202             string prefsRegionCode = PlayerPrefs.GetString(PlayerPrefsKey, "");
203             if (!string.IsNullOrEmpty(prefsRegionCode))
204             {
205                 CloudRegionCode loadedRegion = Region.Parse(prefsRegionCode);
206                 return loadedRegion;
207             }
208
209             return CloudRegionCode.none;
210         }
211         set
212         {
213             if (value == CloudRegionCode.none)
214             {
215                 PlayerPrefs.DeleteKey(PlayerPrefsKey);
216             }
217             else
218             {
219                 PlayerPrefs.SetString(PlayerPrefsKey, value.ToString());
220             }
221         }
222     }

Loaded 137 lượt xem

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