unity3d 根据指定的Assets下的文件夹路径 返回这个路径下的所有文件名

 

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();  
	}  
} 



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。