zoukankan      html  css  js  c++  java
  • [Unity热更新]LuaFramework08.修改加载方式

    框架使用AB包的方式来加载资源,这样意味着如果资源出现了变更,那么就要重新打包,这样才能加载出最新的资源。为了加快开发效率,可以使用AssetDatabase的形式来加载资源

    0.

    如下,用两种方式去加载cube

     1 using UnityEngine;
     2 using UnityEditor;
     3 
     4 public class TestAB : MonoBehaviour
     5 {
     6     public bool isAssetBundle = true;
     7 
     8     void Start()
     9     {
    10         if (isAssetBundle)
    11         {
    12             AssetBundle cubeAB = AssetBundle.LoadFromFile("Assets/StreamingAssets/cube");
    13             GameObject go = null;
    14             if (cubeAB != null)
    15             {
    16                 go = Instantiate(cubeAB.LoadAsset<GameObject>("cube"));
    17                 go.transform.localScale = Vector3.one * 2;
    18             }
    19         }
    20         else
    21         {
    22 #if UNITY_EDITOR
    23             GameObject cubeAsset = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Prefab/Cube.prefab");
    24             GameObject go = Instantiate(cubeAsset);
    25             go.transform.localScale = Vector3.one * 10;
    26 #endif
    27         }
    28     }
    29 }

    1.

    统一加载路径

    使用AB包时,路径为:Assets/StreamingAssets/cube,而且还需要提供资源名:cube

    使用AssetDatabase时,路径为:Assets/Prefab/Cube.prefab,注意需要提供后缀

    首先,打包时一般以文件夹为单位,因此这里需要对Prefab文件夹进行打包,Cube.prefab即为Prefab包内的资源。

    然后,对于AssetDatabase,其路径是没有大小写敏感的,需要提供后缀。

     1 using UnityEngine;
     2 using UnityEditor;
     3 using System;
     4 using UObject = UnityEngine.Object;
     5 
     6 public class TestAB : MonoBehaviour
     7 {
     8     public bool isAssetBundle = true;
     9 
    10     void Start()
    11     {
    12         Load("Prefab/Cube.prefab", (obj) => {
    13             GameObject go = Instantiate(obj) as GameObject;
    14             go.transform.localScale = Vector3.one * 5;
    15         });
    16     }
    17 
    18     //path:Assets下的路径
    19     private void Load(string path, Action<UObject> cb)
    20     {
    21         if (isAssetBundle)
    22         {
    23             path = "Assets/StreamingAssets/" + path;
    24             path = path.Substring(0, path.LastIndexOf("."));
    25             string assetBundleName = path.Substring(0, path.LastIndexOf("/"));
    26             string assetName = path.Substring(path.LastIndexOf("/") + 1);
    27             Debug.LogWarning(assetBundleName);
    28             Debug.LogWarning(assetName);
    29 
    30             AssetBundle assetBundle = AssetBundle.LoadFromFile(assetBundleName);
    31             if (assetBundle != null)
    32             {
    33                 UObject asset = assetBundle.LoadAsset<UObject>(assetName);
    34                 if (cb != null && asset != null)
    35                 {
    36                     cb(asset);
    37                 }
    38             }
    39         }
    40         else
    41         {
    42 #if UNITY_EDITOR
    43             path = "Assets/" + path;
    44             UObject asset = AssetDatabase.LoadAssetAtPath<UObject>(path);
    45             if (cb != null && asset != null)
    46             {
    47                 cb(asset);
    48             }
    49 #endif
    50         }
    51     }
    52 }

    2.

    修改框架源码

    ResourceManager.cs

    Packager.cs

    3.

    调用

  • 相关阅读:
    ServletWebServerApplicationContext -带有*WebxxxApplication的容器
    SpringMvc-DispatchServlet初始化
    SimpleDateFormat和java8日期格式化
    01导入配置类和用户自定义添加db。ImportBeanDefinitionRegistrar和DeferredImportSelector
    @ConfigurationProperties和@PropertySource
    mybatis语句的存储
    leetCode3: 无重复字符的最长子串
    八锁问题
    数据库杂记
    背包问题
  • 原文地址:https://www.cnblogs.com/lyh916/p/11148878.html
Copyright © 2011-2022 走看看