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

本文介绍了在Unity中,使用System.IO下的函数时,可以采用相对路径如Assets/xx,简化路径管理。通过结合Application.dataPath获取绝对路径,避免了在绝对路径和相对路径间频繁切换的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原来在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 on 2019-06-12 12:06 时空观察者9号 阅读(...) 评论(...) 编辑 收藏

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值