Asset









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

Featured Snippets


File name: PunStartup.cs Copy
68     public static void LoadPunDemoHub()
69     {
70         bool ret = EditorApplication.OpenScene(demoBasePath + demoPaths[0]);
71         if (ret)
72         {
73             Selection.activeObject = AssetDatabase.LoadMainAssetAtPath(demoBasePath + demoPaths[0]);
74         }
75     }
File name: PunStartup.cs Copy
80     public static void SetPunDemoBuildSettings()
81     {
82         // find path of pun guide
83         string[] tempPaths = Directory.GetDirectories(Application.dataPath + "/Photon Unity Networking", "Demos", SearchOption.AllDirectories);
84         if (tempPaths == null || tempPaths.Length != 1)
85         {
86             return;
87         }
88
89         // find scenes of guide
90         string guidePath = tempPaths[0];
91         tempPaths = Directory.GetFiles(guidePath, "*.unity", SearchOption.AllDirectories);
92
93         if (tempPaths == null || tempPaths.Length == 0)
94         {
95             return;
96         }
97
98         // add found guide scenes to build settings
99         List sceneAr = new List();
100         for (int i = 0; i < tempPaths.Length; i++)
101         {
102             //Debug.Log(tempPaths[i]);
103             string path = tempPaths[i].Substring(Application.dataPath.Length - "Assets".Length);
104             path = path.Replace('\\', '/');
105             //Debug.Log(path);
106
107             if (path.Contains("PUNGuide_M2H"))
108             {
109                 continue;
110             }
111
112             if (path.Contains("Hub"))
113             {
114                 sceneAr.Insert(0, new EditorBuildSettingsScene(path, true));
115                 continue;
116             }
117
118             sceneAr.Add(new EditorBuildSettingsScene(path, true));
119         }
120
121         EditorBuildSettings.scenes = sceneAr.ToArray();
122         EditorApplication.OpenScene(sceneAr[0].path);
123     }
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: PhotonConverter.cs Copy
160     static void ConvertScripts(List scriptPathList)
161     {
162         bool ignoreWarningIsLogged = false;
163
164         foreach (string script in scriptPathList)
165         {
166             if (script.Contains("PhotonNetwork")) //Don't convert this file (and others)
167             {
168                 if (!ignoreWarningIsLogged)
169                 {
170                     ignoreWarningIsLogged = true;
171                     Debug.LogWarning("Conversion to PUN ignores all files with \"PhotonNetwork\" in their file-path.\nCheck: " + script);
172                 }
173                 continue;
174             }
175             if (script.Contains("Image Effects"))
176             {
177                 continue;
178             }
179
180             ConvertToPhotonAPI(script);
181         }
182
183         foreach (string script in scriptPathList)
184         {
185             AssetDatabase.ImportAsset(script, ImportAssetOptions.ForceUpdate);
186         }
187     }
File name: PhotonConverter.cs Copy
341     static void EnsureFolder(string path)
342     {
343         if (!Directory.Exists(path))
344         {
345             Directory.CreateDirectory(path);
346             AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
347             AssetDatabase.Refresh();
348         }
349     }
File name: PhotonConverter.cs Copy
351     static int ConvertNetworkView(NetworkView[] netViews, bool isScene)
352     {
353         for (int i = netViews.Length - 1; i >= 0; i--)
354         {
355             NetworkView netView = netViews[i];
356             PhotonView view = netView.gameObject.AddComponent();
357             if (isScene)
358             {
359                 //Get scene ID
360                 string str = netView.viewID.ToString().Replace("SceneID: ", "");
361                 int firstSpace = str.IndexOf(" ");
362                 str = str.Substring(0, firstSpace);
363                 int oldViewID = int.Parse(str);
364
365                 view.viewID = oldViewID;
366                 EditorUtility.SetDirty(view);
367                 EditorUtility.SetDirty(view.gameObject);
368             }
369             view.observed = netView.observed;
370             if (netView.stateSynchronization == NetworkStateSynchronization.Unreliable)
371             {
372                 view.synchronization = ViewSynchronization.Unreliable;
373             }
374             else if (netView.stateSynchronization == NetworkStateSynchronization.ReliableDeltaCompressed)
375             {
376                 view.synchronization = ViewSynchronization.ReliableDeltaCompressed;
377             }
378             else
379             {
380                 view.synchronization = ViewSynchronization.Off;
381             }
382             DestroyImmediate(netView, true);
383         }
384         AssetDatabase.Refresh();
385         AssetDatabase.SaveAssets();
386
387         return netViews.Length;
388     }
File name: PhotonEditor.cs Copy
215     static PhotonEditor()
216     {
217         EditorApplication.projectWindowChanged += EditorUpdate;
218         EditorApplication.hierarchyWindowChanged += EditorUpdate;
219         EditorApplication.playmodeStateChanged += PlaymodeStateChanged;
220         EditorApplication.update += OnUpdate;
221
222         WizardIcon = AssetDatabase.LoadAssetAtPath("Assets/Photon Unity Networking/photoncloud-icon.png", typeof(Texture2D)) as Texture2D;
223
224         // to be used in toolbar, the enum needs conversion to string[] being done here, once.
225         Array enumValues = Enum.GetValues(typeof(CloudRegionCode));
226         CloudServerRegionNames = new string[enumValues.Length];
227         for (int i = 0; i < CloudServerRegionNames.Length; i++)
228         {
229             CloudServerRegionNames[i] = enumValues.GetValue(i).ToString();
230             if (CloudServerRegionNames[i].Equals("none"))
231             {
232                 CloudServerRegionNames[i] = PhotonEditor.CurrentLang.BestRegionLabel;
233             }
234         }
235
236         // detect optional packages
237         PhotonEditor.CheckPunPlus();
238
239     }
File name: PhotonEditor.cs Copy
241     internal protected static bool CheckPunPlus()
242     {
243         androidLibExists = File.Exists("Assets/Plugins/Android/libPhotonSocketPlugin.so");
244         iphoneLibExists = File.Exists("Assets/Plugins/IPhone/libPhotonSocketPlugin.a");
245
246         isPunPlus = androidLibExists || iphoneLibExists;
247         return isPunPlus;
248     }
File name: PhotonEditor.cs Copy
250     private static void ImportWin8Support()
251     {
252         if (EditorApplication.isCompiling || EditorApplication.isPlayingOrWillChangePlaymode)
253         {
254             return; // don't import while compiling
255         }
256
257         #if UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_1 || UNITY_5_2
258         const string win8Package = "Assets/Plugins/Photon3Unity3D-Win8.unitypackage";
259
260         bool win8LibsExist = File.Exists("Assets/Plugins/WP8/Photon3Unity3D.dll") && File.Exists("Assets/Plugins/Metro/Photon3Unity3D.dll");
261         if (!win8LibsExist && File.Exists(win8Package))
262         {
263             AssetDatabase.ImportPackage(win8Package, false);
264         }
265         #endif
266     }
File name: PhotonEditor.cs Copy
827     {
828         get
829         {
830             if (currentSettings == null)
831             {
832                 // find out if ServerSettings can be instantiated (existing script check)
833                 ScriptableObject serverSettingTest = CreateInstance("ServerSettings");
834                 if (serverSettingTest == null)
835                 {
836                     Debug.LogError(CurrentLang.ServerSettingsMissingLabel);
837                     return null;
838                 }
839                 DestroyImmediate(serverSettingTest);
840
841                 // try to load settings from file
842                 ReLoadCurrentSettings();
843
844                 // if still not loaded, create one
845                 if (currentSettings == null)
846                 {
847                     string settingsPath = Path.GetDirectoryName(PhotonNetwork.serverSettingsAssetPath);
848                     if (!Directory.Exists(settingsPath))
849                     {
850                         Directory.CreateDirectory(settingsPath);
851                         AssetDatabase.ImportAsset(settingsPath);
852                     }
853
854                     currentSettings = (ServerSettings)ScriptableObject.CreateInstance("ServerSettings");
855                     if (currentSettings != null)
856                     {
857                         AssetDatabase.CreateAsset(currentSettings, PhotonNetwork.serverSettingsAssetPath);
858                     }
859                     else
860                     {
861                         Debug.LogError(CurrentLang.ServerSettingsMissingLabel);
862                     }
863                 }
864
865                 // settings were loaded or created. set this editor's initial selected region now (will be changed in GUI)
866                 if (currentSettings != null)
867                 {
868                     selectedRegion = currentSettings.PreferredRegion;
869                 }
870             }
871
872             return currentSettings;
873         }
874
875         protected set
876         {
877             currentSettings = value;
878         }
879     }

Download file with original file name:Asset

Asset 101 lượt xem

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