zoukankan      html  css  js  c++  java
  • [Unity打包assetbundle]适用于5.x

    <1> 先标记所有.prefab资源 当然 也可以标记.png 等等

    <2>打包

    <3>源码

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEditor;
    using System.IO;
    using System.Text;
    using System.Diagnostics;

    public class BuildAsset : MonoBehaviour
    {
    //输出目录
    private const string outPath = "Assets/Res/AssetBundle";
    //打包目录
    // private const string prefabsPath = "Assets/Res/Arts/Prefabs/";
    private const string prefabsPath = "Assets/Resources/";
    private const string atlasPath = "Assets/Res/Arts/Atlas/";

    /// <summary>
    /// 打包所有被标记过AssetBundle name的资源
    /// </summary>
    [MenuItem("BuildAsset/BuildAll", false, 1002)]
    static void BuildABs()
    {
    if (!Directory.Exists(outPath))
    {
    Directory.CreateDirectory(outPath);
    }
    BuildPipeline.BuildAssetBundles(outPath, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
    //游戏启动 加载ab依赖文件 加载bundle再从依赖文件中查找此bundle的依赖
    AssetDatabase.Refresh();
    }

    /// <summary>
    /// 打包Res/Prefabs下面的所有资源
    /// </summary>
    [MenuItem("BuildAsset/BuildAB", false, 1001)]
    static void BuildFormResPrefabsPath()
    {
    if (!Directory.Exists(prefabsPath))
    {
    Directory.CreateDirectory(prefabsPath);
    }
    if (!Directory.Exists(atlasPath))
    {
    Directory.CreateDirectory(atlasPath);
    }
    Stopwatch watch = new Stopwatch();
    watch.Start();
    markRes(prefabsPath, ".prefab");
    markRes(atlasPath, ".png", true);
    BuildABs();
    watch.Stop();

    EditorUtility.DisplayDialog("提示", "资源打包ab完毕 耗时 秒:" + (watch.ElapsedMilliseconds / 1000).ToString(), "确定");
    }

    //标记所有资源
    static private void markRes(string path, string suff, bool isAll = false)
    {
    if (Directory.Exists(path))
    {
    string name = null;
    DirectoryInfo dirInfo = new DirectoryInfo(path);
    FileInfo[] files = dirInfo.GetFiles("*", SearchOption.AllDirectories);
    if (isAll)
    {
    name = getFolderName(path);
    }

    for (int i = 0; i < files.Length; i++)
    {
    if (files[i].Name.EndsWith(suff))
    {
    AssetImporter imp = AssetImporter.GetAtPath(path + files[i].Name);
    if (imp != null)
    {
    if (!isAll)
    {
    imp.assetBundleName = files[i].Name.Replace(suff, ".assetbundle");
    }
    else
    {
    imp.assetBundleName = name + ".assetbundle";
    }
    }
    }
    }
    }
    }

    static private string getFolderName(string path)
    {
    string[] lst = path.Split('/');
    return lst[lst.Length - 2];
    }

    static public void Write(string key, List<string> vals)
    {
    FileStream fs = new FileStream("E:\depends.txt", FileMode.Create);
    StringBuilder value = new StringBuilder();
    value.Append(key + ':');
    for (int i = 0; i < vals.Count; i++)
    {
    value.Append(vals[i] + ',');
    }
    //获得字节数组
    byte[] data = System.Text.Encoding.Default.GetBytes(value.ToString());
    //开始写入
    fs.Write(data, 0, data.Length);
    //清空缓冲区、关闭流
    fs.Flush();
    fs.Close();
    }

    }

  • 相关阅读:
    如何判断DataSet里有多少个DataTable
    ADO.NET五大对象详解
    c# 中的封装、继承、多态详解
    什么是递归算法
    反射是什么
    什么是泛型
    方法中参数的类型详细
    Struts2学习笔记二 配置详解
    Struts2学习笔记一 简介及入门程序
    Hibernate学习笔记四 查询
  • 原文地址:https://www.cnblogs.com/cocotang/p/8384282.html
Copyright © 2011-2022 走看看