笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,已出版书籍:《手把手教你架构3D游戏引擎》电子工业出版社和《Unity3D实战核心技术详解》电子工业出版社等。

CSDN视频网址:http://edu.csdn.net/lecturer/144

Unity工具是游戏制作必须配备的,Unity虽然提供了强大的编辑器,但是对于游戏开发的需求来说还是远远不够的,这也需要我们自己编写一些小工具去实现特定的需求,比如编写遍历资源文件的所有子文件用于打包处理,这些需要我们自己去封装函数接口。

第一步,实现获取存储文件以及子文件的函数:

private static List<string> GetResAllDirPath()
	{
        List<string> ret = AssetBundleBuild.GetAllLocalSubDirs(cAssetsResourcesPath);
        if (DirExistResource(cAssetsResourcesPath))
        {
            if (ret == null)
                ret = new List<string>();
            ret.Add(cAssetsResourcesPath);
        }

        if (ret != null)
            ret.Sort(OnDirSort);
        return ret;
    }

该函数是对外提供的函数,它返回资源的所有文件以及子文件,在该函数中使用了接口

AssetBundleBuild.GetAllLocalSubDirs(cAssetsResourcesPath);

第二步,实现遍历所有子文件函数,它采用的是迭代的遍历:

	public static List<string> GetAllLocalSubDirs(string rootPath)
	{
		if (string.IsNullOrEmpty (rootPath))
			return null;
		string fullRootPath = System.IO.Path.GetFullPath (rootPath);
		if (string.IsNullOrEmpty (fullRootPath))
			return null;

		string[] dirs = System.IO.Directory.GetDirectories (fullRootPath);
		if ((dirs == null) || (dirs.Length <= 0))
			return null;
		List<string> ret = new List<string> ();

		for (int i = 0; i < dirs.Length; ++i) {
			string dir = AssetBunbleInfo.GetLocalPath(dirs[i]);
			ret.Add (dir);
		}
		for (int i = 0; i < dirs.Length; ++i) {
			string dir = dirs[i];
			List<string> list = GetAllLocalSubDirs(dir);
			if (list != null)
				ret.AddRange(list);
		}

		return ret;
	}

在上述函数中调用了函数接口

AssetBunbleInfo.GetLocalPath(dirs[i]);

该接口的实现函数如下所示:

	// 获得根据Assets目录的局部目录
	public static string GetLocalPath(string path)
	{
        return AssetBundleMgr.GetAssetRelativePath(path);
	}

继续接口的调用函数

GetAssetRelativePath

实现如下所示:

    public static string GetAssetRelativePath(string fullPath)
    {
        if (string.IsNullOrEmpty(fullPath))
            return string.Empty;
        fullPath = fullPath.Replace("\\", "/");
        int index = fullPath.IndexOf("Assets/", StringComparison.CurrentCultureIgnoreCase);
        if (index < 0)
            return fullPath;
        string ret = fullPath.Substring(index);
        return ret;
    }

最后一个函数是DirExistResource函数用于判断文件是否存在,它的实现代码如下所示:

// 根据目录判断是否有资源文件
	public static bool DirExistResource(string path)
	{
		if (string.IsNullOrEmpty (path))
			return false;
		string fullPath = Path.GetFullPath (path);
		if (string.IsNullOrEmpty (fullPath))
			return false;

		string[] files = System.IO.Directory.GetFiles (fullPath);
		if ((files == null) || (files.Length <= 0))
			return false;
		for (int i = 0; i < files.Length; ++i) {
			string ext = System.IO.Path.GetExtension(files[i]);
			if (string.IsNullOrEmpty(ext))
				continue;
			for (int j = 0; j < ResourceExts.Length; ++j)
			{
				if (string.Compare(ext, ResourceExts[j], true) == 0)
				{
					if ((ResourceExts[j] == ".fbx") || (ResourceExts[j] == ".controller"))
					{
						// ingore xxx@idle.fbx
						string name = Path.GetFileNameWithoutExtension(files[i]);
						if (name.IndexOf('@') >= 0)
							return false;
					} else
					if (ResourceExts[j] == ".unity")
					{
						if (!IsVaildSceneResource(files[i]))
							return false;
					}
					return true;
				}
			}
		}

		return false;
	}

代码中的

ResourceExts

是事先定义好的能够遍历到的文件扩展名,如下所示:

	// 支持的资源文件格式
	private static readonly string[] ResourceExts = {".prefab", ".fbx",
							 ".png", ".jpg", ".dds", ".gif", ".psd", ".tga", ".bmp",
							 ".txt", ".bytes", ".xml", ".csv", ".json",
							".controller", ".shader", ".anim", ".unity", ".mat",
							".wav", ".mp3", ".ogg",
							".ttf",
							 ".shadervariants", ".asset"};

最后一步是判断scene是否有效的函数接口:

IsVaildSceneResource

实现代码如下所示:

private static bool IsVaildSceneResource(string fileName)
	{
		bool ret = false;

		if (string.IsNullOrEmpty (fileName))
			return ret;

		string localFileName = AssetBunbleInfo.GetLocalPath (fileName);
		if (string.IsNullOrEmpty (localFileName))
			return ret;

		var scenes = EditorBuildSettings.scenes;
		if (scenes == null)
			return ret;

		var iter = scenes.GetEnumerator ();
		while (iter.MoveNext()) {
			EditorBuildSettingsScene scene = iter.Current as EditorBuildSettingsScene;
			if ((scene != null) && scene.enabled)
			{
				if (string.Compare(scene.path, localFileName, true) == 0)
				{
					ret = true;
					break;
				}
			}
		}

		return ret;
	}

这样遍历所有资源下的文件和子文件的所有函数就完成了,放在工具代码中实现即可。

Unity遍历资源下的所有文件以及子文件的更多相关文章

  1. IO流-递归遍历目录下指定后缀名结尾的文件名称

    /* *自定义遍历目录下指定后缀名结尾文件的名称的方法: * * param file:指定目录 name:指定后缀名 */ 1 public static void FileName(File fi ...

  2. PHP递归获得树形菜单和遍历文件夹下的所有文件以及子文件夹

    PHP递归获得树形菜单和遍历文件夹下的所有文件以及子文件夹 一.使用递归获取树形菜单 数据表category(id,name,parent_id) <?php class category{ / ...

  3. C#遍历指定文件夹中的所有文件和子文件夹

    参考:http://www.cnblogs.com/skylaugh/archive/2012/09/23/2698850.html DirectoryInfo TheFolder=new Direc ...

  4. php 遍历一个文件夹下的所有文件和子文件

    php 遍历一个文件夹下的所有文件和子文件 <?php /** * 将读取到的目录以数组的形式展现出来 * @return array * opendir() 函数打开一个目录句柄,可由 clo ...

  5. Shell遍历目前下后缀名为.xml的文件并替换文件内容

    1.shell查找 .xml文件 find /home/esoon/test/external/ -type f -name '*.xml' 2.替换方法 sed -i "s/10.111. ...

  6. 一个php函数,能够遍历一个文件夹下的所有文件和子文件夹

    <?phpfunction my_scandir($dir){    $files=array();    if(is_dir($dir))     {        if($handle=op ...

  7. php 遍历一个文件夹下的所有文件和子文件夹

    <?php function my_scandir($dir) { $files=array(); if(is_dir($dir)) { if($handle=opendir($dir)) { ...

  8. PHP 遍历文件夹下的文件以及子文件夹

    // 递归的方式实现function my_dir( $dir ){ if ( !is_dir($dir) ) { return 'not dir';die(); } $files = array() ...

  9. PHP 遍历目录下面的所有文件及子文件夹

    //遍历文件夹下面所有文件及子文件夹 function read_all_dir ( $dir ){ $result = array(); $handle = opendir($dir);//读资源 ...

随机推荐

  1. Linux文件系统学习(一)之相关概念⭐⭐⭐

    “一切皆是文件”是 Unix/Linux 的基本哲学之一.不仅普通的文件,目录.字符设备.块设备.套接字等在 Unix/Linux 中都是以文件被对待:它们虽然类型不同,但是对其提供的却是同一套操作界 ...

  2. 关于js中对事件绑定与普通事件的理解

    普通事件指的是可以用来注册的事件: 事件绑定是指把事件注册到具体的元素之上. 通俗点说: 普通事件:给html元素添加一个特定的属性(如:onclick): 事件绑定:js代码中通过标记(id  ta ...

  3. Myeclipse中java项目转换为Web项目

    https://blog.csdn.net/u010097777/article/details/51281059 这两天工作安排做一个跳转页面,不过昨天发布自己的Tomact花了不少时间,给的项目添 ...

  4. 【纯代码】Swift-自定义PickerView单选(可修改分割线颜色、修改字体大小、修改字体颜色。)(可根据需要自己扩展)

    typealias PopPickerViewCallBackClosure = (_ resultStr:NSString?) -> () class PopPickerView : UIVi ...

  5. Ubuntu 设置为固定ip

    vmware设置为桥接模式 使用root登陆ubuntu 使用gedit打开/etc/network/interfaces auto lo iface lo inet loopback auto en ...

  6. ISSCC 2017论文导读 Session 14 Deep Learning Processors,A 2.9TOPS/W Deep Convolutional Neural Network SOC

    最近ISSCC2017大会刚刚举行,看了关于Deep Learning处理器的Session 14,有一些不错的东西,在这里记录一下. A 2.9TOPS/W Deep Convolutional N ...

  7. centos7更改主机名

    操作环境 [root@centos701 ~]# uname Linux [root@centos701 ~]# uname -a Linux centos701 3.10.0-693.el7.x86 ...

  8. Spring mvc异步处理

    基于Servlet3.0的异步处理,springmvc的异步处理 控制器返回callable, spring mvc异步处理,将callable提交到TaskExecutor  使用一个隔离线程进行执 ...

  9. AtCoder Grand Round 012B Splatter Painting

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  10. codevs 1540 银河英雄传说 并查集

    1540 银河英雄传说 2002年NOI全国竞赛  时间限制: 1 s  空间限制: 256000 KB       题目描述 Description 公元五八○一年,地球居民迁移至金牛座α第二行星, ...