GetFiles
How do I use Get Files
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
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
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
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
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
142 public static List143 {
144 List
145
146 try
147 {
148 scripts.AddRange(Directory.GetFiles(folder, "*.cs", SearchOption.AllDirectories));
149 scripts.AddRange(Directory.GetFiles(folder, "*.js", SearchOption.AllDirectories));
150 scripts.AddRange(Directory.GetFiles(folder, "*.boo", SearchOption.AllDirectories));
151 }
152 catch (System.Exception ex)
153 {
154 Debug.Log("Getting script list from folder " + folder + " failed. Exception:\n" + ex.ToString());
155 }
156
157 return scripts;
158 }
File name: PhotonViewHandler.cs
Copy
173 //TODO: check if this can be internal protected (as source in editor AND as dll)
174 public static void LoadAllScenesToFix()
175 {
176 string[] scenes = System.IO.Directory.GetFiles(".", "*.unity", SearchOption.AllDirectories);
177
178 foreach (string scene in scenes)
179 {
180 EditorApplication.OpenScene(scene);
181
182 PhotonViewHandler.HierarchyChange();//TODO: most likely on load also triggers a hierarchy change
183
184 EditorApplication.SaveScene();
185 }
186
187 Debug.Log("Corrected scene views where needed.");
188 }
File name: PunSceneSettings.cs
Copy
23 {
24 get
25 {
26 if (!string.IsNullOrEmpty(punSceneSettingsCsPath))
27 {
28 return punSceneSettingsCsPath;
29 }
30
31 // Unity 4.3.4 does not yet have AssetDatabase.FindAssets(). Would be easier.
32 var result = Directory.GetFiles(Application.dataPath, "PunSceneSettings.cs", SearchOption.AllDirectories);
33 if (result.Length >= 1)
34 {
35 punSceneSettingsCsPath = Path.GetDirectoryName(result[0]);
36 punSceneSettingsCsPath = punSceneSettingsCsPath.Replace('\\', '/');
37 punSceneSettingsCsPath = punSceneSettingsCsPath.Replace(Application.dataPath, "Assets");
38
39 // AssetDatabase paths have to use '/' and are relative to the project's folder. Always.
40 punSceneSettingsCsPath = punSceneSettingsCsPath + "/" + SceneSettingsFileName;
41 }
42
43 return punSceneSettingsCsPath;
44 }
45 }
File name: VSCode.cs
Copy
357 public static void UpdateSolution()
358 {
359 // No need to process if we are not enabled
360 if (!VSCode.Enabled)
361 {
362 return;
363 }
364
365 if (VSCode.Debug)
366 {
367 UnityEngine.Debug.Log("[VSCode] Updating Solution & Project Files");
368 }
369
370 var currentDirectory = Directory.GetCurrentDirectory();
371 var solutionFiles = Directory.GetFiles(currentDirectory, "*.sln");
372 var projectFiles = Directory.GetFiles(currentDirectory, "*.csproj");
373
374 foreach (var filePath in solutionFiles)
375 {
376 string content = File.ReadAllText(filePath);
377 content = ScrubSolutionContent(content);
378
379 File.WriteAllText(filePath, content);
380
381 ScrubFile(filePath);
382 }
383
384 foreach (var filePath in projectFiles)
385 {
386 string content = File.ReadAllText(filePath);
387 content = ScrubProjectContent(content);
388
389 File.WriteAllText(filePath, content);
390
391 ScrubFile(filePath);
392 }
393
394 }
File name: Tiled2UnityMenuItems.cs
Copy
40 private static IEnumerable41 {
42 foreach (string d in Directory.GetDirectories(dir))
43 {
44 foreach (string f in Directory.GetFiles(d))
45 {
46 if (extensions.Any(ext => String.Compare(ext, Path.GetExtension(f), true) == 0))
47 {
48 yield return f;
49 }
50 }
51
52 foreach (string f in EnumerateAssetFilesAt(d, extensions))
53 {
54 yield return f;
55 }
56 }
57 }
File name: Tiled2UnityMenuItems.cs
Copy
59 private static void DeleteAssetsAt(string dir)
60 {
61 // Note: Does not remove any text files.
62 foreach (string f in Directory.GetFiles(dir))
63 {
64 if (f.EndsWith(".txt", true, null))
65 continue;
66
67 if (f.EndsWith(".meta", true, null))
68 continue;
69
70 // Just to be safe. Do not remove scripts.
71 if (f.EndsWith(".cs", true, null))
72 continue;
73
74 AssetDatabase.DeleteAsset(f);
75 }
76 }
GetFiles 146 lượt xem
Gõ tìm kiếm nhanh...