unity3d 根據指定的Assets下的檔案夾路徑 返回這個路徑下的所有檔案名稱,unity3dassets
using UnityEngine;using System.Collections;using System.Collections.Generic;using System.IO;
<pre class="csharp" name="code">public static List<string> nameArray = new List<string>();
/// <summary>/// 根據指定的 Assets下的檔案路徑 返回這個路徑下的所有檔案名稱///// </summary>/// <returns>檔案名稱數組</returns>/// <param name="path">Assets下“一"級路徑</param>/// <param name="pattern">篩選檔案尾碼名的條件.</param>/// <typeparam name="T">函數模板的類型名t</typeparam>void GetObjectNameToArray<T>(string path, string pattern){ string objPath = Application.dataPath + "/" + path; string[] directoryEntries; try { //返回指定的目錄中檔案和子目錄的名稱的數組或空數組directoryEntries = System.IO.Directory.GetFileSystemEntries(objPath); for(int i = 0; i < directoryEntries.Length ; i ++){ string p = directoryEntries[i]; //得到要求目錄下的檔案或者檔案夾(一級的)//string[] tempPaths = StringExtention.SplitWithString(p,"/Assets/"+path+"\\"); //tempPaths 分割後的不可能為空白,只要directoryEntries不為空白//if(tempPaths[1].EndsWith(".meta"))continue;string[] pathSplit = StringExtention.SplitWithString(tempPaths[1],"."); //檔案if(pathSplit.Length > 1){ nameArray.Add(pathSplit[0]); }//遍曆子目錄下 遞迴吧!else{GetObjectNameToArray<T> (path+"/"+pathSplit[0], "pattern");continue;}} } catch (System.IO.DirectoryNotFoundException) { Debug.Log("The path encapsulated in the " + objPath + "Directory object does not exist."); } }
void Start () {//TextAsset[] texts = LoadAsset<TextAsset> ("/CreateScriptDialog/Editor", "cs");//GetObjectNameToArray<string> ("uSequencer/Example Scenes", "xxx"); //可以實現嵌套遍曆GetObjectNameToArray<string> ("uSequencer", "xxx"); //可以實現嵌套遍曆foreach (string str in nameArray) {Debug.Log(str);}}
<pre class="csharp" name="code">/// <summary>/// 自訂的字串分割的方法/// </summary>public class StringExtention { public static string[] SplitWithString(string sourceString, string splitString){string tempSourceString = sourceString;List<string> arrayList = new List<string>(); string s = string.Empty; while (sourceString.IndexOf(splitString) > -1) //分割{ s = sourceString.Substring(0, sourceString.IndexOf(splitString)); sourceString = sourceString.Substring(sourceString.IndexOf(splitString) + splitString.Length); arrayList.Add(s); } arrayList.Add(sourceString); return arrayList.ToArray(); } }
怎擷取assets檔案夾的路徑??
InputStream inStream = context.getAssets().open("assets下的路徑(不包含assets)/檔案名稱");
Unity3D 4x C#指令碼怎讀取Resources目錄下所有檔案的檔案名稱
無法讀取,Unity沒有提供這個功能。你可以建立一個list檔案,裡面記錄所有檔案的檔案名稱,然後用Resources.load來讀取這個檔案來獲得所有檔案的檔案名稱。我覺得你需要的遍曆檔案名稱功能,可能這個函數能用的上Resources.FindObjectsOfTypeAll,列出某種類型的所有Object