• Unity Editor 打包指定资源(AssetBundle)和加载指定资源


    前言:

            一般用于ui资源打包和加载,代码比较简单没什么好说的,直接上代码。

            打包代码

    1. [MenuItem("Assets/打包指定的预设")]
    2. public static void BuildAsset() {
    3. var selectObject = Selection.activeObject;
    4. if (selectObject != null) {
    5. if (selectObject is GameObject go) {
    6. try {
    7. EditorUtility.DisplayProgressBar("打包资源", go.name, 1.0f);
    8. AssetBundleBuild build = new AssetBundleBuild();
    9. var path = Application.streamingAssetsPath + "/assetbundle";
    10. if (!Directory.Exists(path)) {
    11. Directory.CreateDirectory(path);
    12. }
    13. build.assetBundleName = go.name;
    14. build.assetNames = new string[] { AssetDatabase.GetAssetPath(selectObject) };
    15. AssetBundleManifest manifest = BuildPipeline.BuildAssetBundles(path,
    16. new AssetBundleBuild[] { build },
    17. BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.StandaloneWindows64);
    18. File.Delete(path + "/" + go.name + ".manifest");
    19. AssetDatabase.Refresh();
    20. }
    21. catch (Exception e) {
    22. Debug.Log($"打包资源:{go.name}失败,Error:{e.Message}");
    23. }
    24. EditorUtility.ClearProgressBar();
    25. }
    26. }
    27. //string inputPath = EditorUtility.OpenFolderPanel("选择编译文件夹",
    28. // UnityEngine.Application.dataPath, "HostScripts");
    29. //if (string.IsNullOrEmpty(inputPath)) {
    30. // EditorUtility.DisplayDialog("错误", "必须选择文件夹才能进行编译", "确定");
    31. //}
    32. }

    加载代码:

    1. public void LoadUIformAB<T>(string path, string assetName, Action callBack) where T : UnityEngine.Object {
    2. AssetBundle ab = null;
    3. if (abUIMap.TryGetValue(path, out ab)) {
    4. try {
    5. T asset = (T)GameObject.Instantiate(ab.LoadAsset(assetName));
    6. callBack?.Invoke(asset);
    7. }
    8. catch (Exception e) {
    9. McLogger.Error("UI", nameof(UISetting), $"加载UIab包报错{e.Message}---->{e.StackTrace}");
    10. callBack?.Invoke(null);
    11. }
    12. }
    13. else {
    14. if (File.Exists(path)) {
    15. try {
    16. ab = AssetBundle.LoadFromFile(path);
    17. abUIMap.Add(path, ab);
    18. T asset = (T)GameObject.Instantiate(ab.LoadAsset(assetName));
    19. callBack?.Invoke(asset);
    20. }
    21. catch (Exception e) {
    22. McLogger.Error("UI", nameof(UISetting), $"加载UIab包报错{e.Message}---->{e.StackTrace}");
    23. callBack?.Invoke(null);
    24. }
    25. }
    26. else {
    27. callBack?.Invoke(null);
    28. }
    29. }
    30. }
    1. private string path = Application.streamingAssetsPath + $"/assetbundle/sequenceframe";//ab包路径
    2. private void LoadAssetBundleFile(){
    3. SimApp.UIRuntime.LoadUIformAB(path, "sequenceframe", (data) =>
    4. {
    5. if (data != null)
    6. {
    7. data.transform.SetParent(transform);
    8. data.GetComponent().localPosition = Vector3.zero;
    9. data.GetComponent().sizeDelta = Vector2.zero;
    10. data.transform.localScale = Vector3.one;
    11. }
    12. });
    13. }

  • 相关阅读:
    虹科方案 | 汽车CAN/LIN总线数据采集解决方案
    【C/C++】图文题目吃透内存管理
    如何禁止在堆上和栈上创建对象
    美团二面算法题-输出每对有效括号内的内容;
    java中对象的比较
    Vue 设置v-html中元素样式
    新浪财经行情中心的对象 Market_Center
    基于Java+SpringBoot+mybatis+vue+element实现旅游管理系统
    Spring Boot配置项注入异常:Failed to bind properties
    Docker Swarm: 容器编排的力量和优势深度解析
  • 原文地址:https://blog.csdn.net/ThreePointsHeat/article/details/133947324