码迷,mamicode.com
首页 > 编程语言 > 详细

unity3d 依据指定的Assets下的目录路径 返回这个路径下的全部文件名称

时间:2015-12-18 21:13:29      阅读:317      评论:0      收藏:0      [点我收藏+]

标签:

 

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




   

unity3d 依据指定的Assets下的目录路径 返回这个路径下的全部文件名称

标签:

原文地址:http://www.cnblogs.com/mengfanrong/p/5057936.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!