原来在UNITY中使用system.io下的所有函数都可以用相对路径 : Assets/xx

代码如下图,这样就不用在绝对路径和相对路径之间不断转换了。

想要得到绝对路径时就傅 Application.dataPath  + xxx

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
public class abbuilder
{
    [MenuItem("AssetBundle/BuildABx")]
    public static void BuildAB()
    {
        var rootPath = "Assets/ResInGame";

        if (!Directory.Exists(rootPath))
        {
            Debug.LogError("file not exist:" + rootPath);
            return;
        }

        var files = Directory.GetFiles(rootPath, "*", SearchOption.AllDirectories);
        var abBuilds = new List<AssetBundleBuild>(files.Length / 2); //估计值,肯定够了,因为每个文件夹也有META文件

        var stopwt = System.Diagnostics.Stopwatch.StartNew();
        var t1 = stopwt.ElapsedMilliseconds;

        foreach (var item in files)
        {
            var dir = Path.GetDirectoryName(item);
            var fileName = Path.GetFileName(item);

            if (item.EndsWith(".meta"))
                continue;

            var relativeDir = item.Substring(17);
            var abBuild = new AssetBundleBuild();
            abBuild.assetBundleName = relativeDir;
            abBuild.assetNames = new string[] { item };
            abBuild.assetBundleVariant = "ab";
            //Debug.Log(item + "\n" + fileName + "\n");

            abBuilds.Add(abBuild);
        }

        var abPath = Application.dataPath.Substring(0, Application.dataPath.Length-6) + "Bundles";
        
        if (!Directory.Exists(abPath))
        {
            Directory.CreateDirectory(abPath);
        }

        var dt1 = stopwt.ElapsedMilliseconds - t1;

        BuildPipeline.BuildAssetBundles(abPath, abBuilds.ToArray(), BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.Android);

        var dt2 = stopwt.ElapsedMilliseconds - t1 - dt1;

        EditorUtility.DisplayDialog("", dt1 +"," + dt2, "ok");
    }
}

 

posted @ 2019-06-12 12:06  时空观察者9号  阅读(697)  评论(0编辑  收藏  举报