Quit









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

Featured Snippets


File name: ChatGui.cs Copy
87     public void OnApplicationQuit()
88     {
89         if (this.chatClient != null)
90         {
91             this.chatClient.Disconnect();
92         }
93     }
File name: PhotonConverter.cs Copy
18     public static void RunConversion()
19     {
20         //Ask if user has made a backup.
21         int option = EditorUtility.DisplayDialogComplex("Conversion", "Attempt automatic conversion from Unity Networking to Photon Unity Networking \"PUN\"?", "Yes", "No!", "Pick Script Folder");
22         switch (option)
23         {
24             case 0:
25                 break;
26             case 1:
27                 return;
28             case 2:
29                 PickFolderAndConvertScripts();
30                 return;
31             default:
32                 return;
33         }
34
35         //REAAAALY?
36         bool result = EditorUtility.DisplayDialog("Conversion", "Disclaimer: The code conversion feature is quite crude, but should do it's job well (see the sourcecode). A backup is therefore strongly recommended!", "Yes, I've made a backup: GO", "Abort");
37         if (!result)
38         {
39             return;
40         }
41         Output(EditorApplication.timeSinceStartup + " Started conversion of Unity networking -> Photon");
42
43         //Ask to save current scene (optional)
44         EditorApplication.SaveCurrentSceneIfUserWantsTo();
45
46         EditorUtility.DisplayProgressBar("Converting..", "Starting.", 0);
47
48         //Convert NetworkViews to PhotonViews in Project prefabs
49         //Ask the user if we can move all prefabs to a resources folder
50         bool movePrefabs = EditorUtility.DisplayDialog("Conversion", "Can all prefabs that use a PhotonView be moved to a Resources/ folder? You need this if you use Network.Instantiate.", "Yes", "No");
51
52
53         string[] prefabs = Directory.GetFiles("Assets/", "*.prefab", SearchOption.AllDirectories);
54         foreach (string prefab in prefabs)
55         {
56             EditorUtility.DisplayProgressBar("Converting..", "Object:" + prefab, 0.6f);
57
58             Object[] objs = (Object[])AssetDatabase.LoadAllAssetsAtPath(prefab);
59             int converted = 0;
60             foreach (Object obj in objs)
61             {
62                 if (obj != null && obj.GetType() == typeof(GameObject))
63                     converted += ConvertNetworkView(((GameObject)obj).GetComponents(), false);
64             }
65             if (movePrefabs && converted > 0)
66             {
67                 //This prefab needs to be under the root of a Resources folder!
68                 string path = prefab.Replace("\\", "/");
69                 int lastSlash = path.LastIndexOf("/");
70                 int resourcesIndex = path.LastIndexOf("/Resources/");
71                 if (resourcesIndex != lastSlash - 10)
72                 {
73                     if (path.Contains("/Resources/"))
74                     {
75                         Debug.LogWarning("Warning, prefab [" + prefab + "] was already in a resources folder. But has been placed in the root of another one!");
76                     }
77                     //This prefab NEEDS to be placed under a resources folder
78                     string resourcesFolder = path.Substring(0, lastSlash) + "/Resources/";
79                     EnsureFolder(resourcesFolder);
80                     string newPath = resourcesFolder + path.Substring(lastSlash + 1);
81                     string error = AssetDatabase.MoveAsset(prefab, newPath);
82                     if (error != "")
83                         Debug.LogError(error);
84                     Output("Fixed prefab [" + prefab + "] by moving it into a resources folder.");
85                 }
86             }
87         }
88
89         //Convert NetworkViews to PhotonViews in scenes
90         string[] sceneFiles = Directory.GetFiles("Assets/", "*.unity", SearchOption.AllDirectories);
91         foreach (string sceneName in sceneFiles)
92         {
93             EditorApplication.OpenScene(sceneName);
94             EditorUtility.DisplayProgressBar("Converting..", "Scene:" + sceneName, 0.2f);
95
96             int converted2 = ConvertNetworkView((NetworkView[])GameObject.FindObjectsOfType(typeof(NetworkView)), true);
97             if (converted2 > 0)
98             {
99                 //This will correct all prefabs: The prefabs have gotten new components, but the correct ID's were lost in this case
100                 PhotonViewHandler.HierarchyChange(); //TODO: most likely this is triggered on change or on save
101
102                 Output("Replaced " + converted2 + " NetworkViews with PhotonViews in scene: " + sceneName);
103                 EditorApplication.SaveScene(EditorApplication.currentScene);
104             }
105
106         }
107
108         //Convert C#/JS scripts (API stuff)
109         List scripts = GetScriptsInFolder("Assets");
110
111         EditorUtility.DisplayProgressBar("Converting..", "Scripts..", 0.9f);
112         ConvertScripts(scripts);
113
114         Output(EditorApplication.timeSinceStartup + " Completed conversion!");
115         EditorUtility.ClearProgressBar();
116
117         EditorUtility.DisplayDialog("Completed the conversion", "Don't forget to add \"PhotonNetwork.ConnectWithDefaultSettings();\" to connect to the Photon server before using any multiplayer functionality.", "OK");
118     }
File name: NetworkingPeer.cs Copy
253     public bool Connect(string serverAddress, ServerConnection type)
254     {
255         if (PhotonHandler.AppQuits)
256         {
257             Debug.LogWarning("Ignoring Connect() because app gets closed. If this is an error, check PhotonHandler.AppQuits.");
258             return false;
259         }
260
261         if (PhotonNetwork.connectionStateDetailed == global::PeerState.Disconnecting)
262         {
263             Debug.LogError("Connect() failed. Can't connect while disconnecting (still). Current state: " + PhotonNetwork.connectionStateDetailed);
264             return false;
265         }
266
267         // connect might fail, if the DNS name can't be resolved or if no network connection is available
268         bool connecting = base.Connect(serverAddress, "");
269         if (connecting)
270         {
271             switch (type)
272             {
273                 case ServerConnection.NameServer:
274                     State = global::PeerState.ConnectingToNameServer;
275                     break;
276                 case ServerConnection.MasterServer:
277                     State = global::PeerState.ConnectingToMasterserver;
278                     break;
279                 case ServerConnection.GameServer:
280                     State = global::PeerState.ConnectingToGameserver;
281                     break;
282             }
283         }
284
285         return connecting;
286     }
File name: NetworkingPeer.cs Copy
294     public bool ConnectToNameServer()
295     {
296         if (PhotonHandler.AppQuits)
297         {
298             Debug.LogWarning("Ignoring Connect() because app gets closed. If this is an error, check PhotonHandler.AppQuits.");
299             return false;
300         }
301
302         IsUsingNameServer = true;
303         this.CloudRegion = CloudRegionCode.none;
304
305         if (this.State == global::PeerState.ConnectedToNameServer)
306         {
307             return true;
308         }
309
310         #if RHTTP
311         string address = (this.UsedProtocol == ConnectionProtocol.RHttp) ? this.NameServerAddressHttp : this.NameServerAddress;
312         #else
313         string address = this.NameServerAddress;
314         #endif
315
316         if (!address.Contains(":"))
317         {
318             int port = 0;
319             ProtocolToNameServerPort.TryGetValue(this.UsedProtocol, out port);
320             address = string.Format("{0}:{1}", address, port);
321             Debug.Log("Server to connect to: " + address + " settings protocol: " + PhotonNetwork.PhotonServerSettings.Protocol);
322         }
323         if (!base.Connect(address, "ns"))
324         {
325             return false;
326         }
327
328         this.State = global::PeerState.ConnectingToNameServer;
329         return true;
330     }
File name: NetworkingPeer.cs Copy
336     public bool ConnectToRegionMaster(CloudRegionCode region)
337     {
338         if (PhotonHandler.AppQuits)
339         {
340             Debug.LogWarning("Ignoring Connect() because app gets closed. If this is an error, check PhotonHandler.AppQuits.");
341             return false;
342         }
343
344         IsUsingNameServer = true;
345         this.CloudRegion = region;
346
347         if (this.State == global::PeerState.ConnectedToNameServer)
348         {
349             return this.OpAuthenticate(this.mAppId, this.mAppVersionPun, this.PlayerName, this.CustomAuthenticationValues, region.ToString());
350         }
351
352         #if RHTTP
353         string address = (this.UsedProtocol == ConnectionProtocol.RHttp) ? this.NameServerAddressHttp : this.NameServerAddress;
354         #else
355         string address = this.NameServerAddress;
356         #endif
357
358         if (!address.Contains(":"))
359         {
360             int port = 0;
361             ProtocolToNameServerPort.TryGetValue(this.UsedProtocol, out port);
362             address = string.Format("{0}:{1}", address, port);
363             //Debug.Log("Server to connect to: "+ address + " settings protocol: " + PhotonNetwork.PhotonServerSettings.Protocol);
364         }
365         if (!base.Connect(address, "ns"))
366         {
367             return false;
368         }
369
370         this.State = global::PeerState.ConnectingToNameServer;
371         return true;
372     }
File name: NetworkingPeer.cs Copy
395     /// Complete disconnect from photon (and the open master OR game server)
397     public override void Disconnect()
398     {
399         if (this.PeerState == PeerStateValue.Disconnected)
400         {
401             if (!PhotonHandler.AppQuits)
402             {
403                 Debug.LogWarning(string.Format("Can't execute Disconnect() while not connected. Nothing changed. State: {0}", this.State));
404             }
405             return;
406         }
407
408         this.State = global::PeerState.Disconnecting;
409         base.Disconnect();
410
411         //this.LeftRoomCleanup();
412         //this.LeftLobbyCleanup();
413     }
File name: NetworkingPeer.cs Copy
2656     public void LocalCleanPhotonView(PhotonView view)
2657     {
2658         view.destroyedByPhotonNetworkOrQuit = true;
2659         this.photonViewList.Remove(view.viewID);
2660     }
File name: PhotonHandler.cs Copy
52     protected void OnApplicationQuit()
53     {
54         PhotonHandler.AppQuits = true;
55         PhotonHandler.StopFallbackSendAckThread();
56         PhotonNetwork.Disconnect();
57     }
File name: PhotonNetwork.cs Copy
1285     public static void Disconnect()
1286     {
1287         if (offlineMode)
1288         {
1289             offlineMode = false;
1290             offlineModeRoom = null;
1291             networkingPeer.State = PeerState.Disconnecting;
1292             networkingPeer.OnStatusChanged(StatusCode.Disconnect);
1293             return;
1294         }
1295
1296         if (networkingPeer == null)
1297         {
1298             return; // Surpress error when quitting playmode in the editor
1299         }
1300
1301         networkingPeer.Disconnect();
1302     }
File name: PhotonView.cs Copy
290     protected internal void OnApplicationQuit()
291     {
292         destroyedByPhotonNetworkOrQuit = true; // on stop-playing its ok Destroy is being called directly (not by PN.Destroy())
293     }

Download file with original file name:Quit

Quit 122 lượt xem

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