unity3d 根據指定的Assets下的檔案夾路徑 返回這個路徑下的所有檔案名稱,unity3dassets

來源:互聯網
上載者:User

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
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.