unity3d 依据指定的Assets下的目录路径 返回这个路径下的全部文件名称
2015-12-18 18:43
671 查看
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();}}
相关文章推荐
- “400 Bad Request”解决办法及Unity下载文件保存到本地
- 小米游戏中心sdk接入unity
- iOS与Unity3d的交互实现
- Unity插件之NGUI学习(5)—— 创建Label图文混排及文字点击
- PureMVC for Unity3d Demo
- unity3d中摄像机只看对应的对象.
- unity异步从外部文件加载音频和图片
- Unity封装dll教程整理
- 3dsmax的模型优化技巧
- Unity中使用Bumpmap Lightmap教程
- 使用Unity开发项目的一点经验
- unity3d用鼠标拖动物体的一段代码
- Script 脚本所有编译器属性详解
- 如何向另一场景发送信息
- 构建Unity3D信号槽机制 【ZObject.cs】
- unitywebplayer屏蔽鼠标右键
- Unity NGUI 描点控件的位移动画
- Unity 帧率设置和显示FPS脚本
- 【Unity3D基础教程】给初学者看的Unity教程(零):如何学习Unity3D
- Unity3d动态数据管理(1)Export AssetBundles