【Unity3D】资源管理

1 前言

​ Unity 中资源管理方案主要有 Resources、TextAsset、ScriptableObject 、AssetDatabase、PlayerPrefs、Addressables、AssetBundle、SQLite,本文将介绍其中大部分方案。

2 Resources

​ Resources 主要用于加载资源,被加载的资源需要放在 Resources 目录下,可以在 Assets 的任何子目录下创建 Resources 目录,Unity 会自动检索到这些 Resources 目录。

​ Test_Resources.cs

using UnityEngine;  public class Test_Resources : MonoBehaviour {     private void Awake() {         GameObject cube = Resources.Load<GameObject>("CubePrefab");         Material cubeMat = Resources.Load<Material>("CubeMat");         cube.GetComponent<Renderer>().material = cubeMat;         Instantiate(cube);     } } 

3 TextAsset

​ TextAsset 主要用于加载文本、表格文件,官方介绍见→TextAsset

【Unity3D】资源管理

​ Test_TextAsset.cs

using UnityEngine;  public class Test_TextAsset : MonoBehaviour {     [SerializeField]     private TextAsset heightDatas;      private void Awake() {         string[] textInLines = heightDatas.text.Split('n');         for (int i = 1; i < textInLines.Length; i++) {             string[] values = textInLines[i].Split(",");             string name = values[0];             int age = int.Parse(values[1]);             float height = float.Parse(values[2]);             Debug.Log("name=" + name + ", age=" + age + ", height=" + height);         }     } } 

​ 打印如下。

【Unity3D】资源管理

4 ScriptableObject

​ ScriptableObject 主要用于持久化数据、项目参数配置、角色参数配置等,官方介绍见→ScriptableObject,主要回调函数如下,继承 ScriptableObject 后,重写下述方法即可在相应状态下自动回调。

【Unity3D】资源管理

​ SOData.cs

using UnityEngine;  public class SOData : ScriptableObject {     [SerializeField]     private string _name; // 姓名     [SerializeField]     private int _age; // 年龄     [SerializeField, Range(1f, 2.3f)]     private float _height; // 身高      public string Name => _name;     public int Age => _age;     public float Height => _height; } 

​ 编译后,在 Assets 窗口右键,依次选择【Create→SOData】,创建对象,并重命名为 SOData_1。选中 SOData_1 对象后,在 Inspector 窗口可以调整属性,如下。

【Unity3D】资源管理

5 AssetDatabase

​ AssetDatabase 用于加载资源,仅在 Unity 编辑器模式下使用,官方介绍见→AssetDatabaseBatching with the AssetDatabase

AssetDatabase.CopyAsset("Assets/Asset1.txt", "Assets/Text/Asset1.txt"); // 复制资源 AssetDatabase.MoveAsset("Assets/Asset2.txt", "Assets/Text/Asset2.txt"); // 移动资源 AssetDatabase.DeleteAsset("Assets/Asset3.txt"); // 删除资源 AssetDatabase.ImportAsset(path1, ImportAssetOptions.Default); // 导入资源 GameObject cube = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Loader/CubePrefab.prefab"); // 加载资源 

​ Test_AssetDatabase.cs

using UnityEditor; using UnityEngine;  public class Test_AssetDatabase {      [MenuItem("Custom/Import Cube")]     public static void ImportImage() {         // 导入Cube预设体         string path1 = "Assets/Loader/CubePrefab.prefab";         AssetDatabase.ImportAsset(path1, ImportAssetOptions.Default);         GameObject cube = AssetDatabase.LoadAssetAtPath<GameObject>(path1);         // 导入Cube材质         string path2 = "Assets/Loader/CubeMat.mat";         AssetDatabase.ImportAsset(path2, ImportAssetOptions.Default);         Material mat = AssetDatabase.LoadAssetAtPath<Material>(path2);         // 实例化Cube         cube.GetComponent<Renderer>().material = mat;         GameObject.Instantiate(cube);     } } 

​ 说明:Test_AssetDatabase 脚本文件需要放在 Editor 目录下面。编译后,在菜单栏依次点击【Custom→Import Cube】即可创建一个 Cube 对象。

6 PlayerPrefs

​ PlayerPrefs 是一个在游戏会话之间存储玩家偏好的类,它可以将字符串、浮点数和整数值存储到用户的平台注册表中。Unity 将 PlayerPrefs 存储在本地注册表中,没有加密,不要使用 PlayerPrefs 数据存储敏感数据。PlayerPrefs 官方介绍见→PlayerPrefs

​ PlayerPrefs 的静态方法如下。

【Unity3D】资源管理

​ Test_PlayerPrefs.cs

using UnityEngine;  public class Test_PlayerPrefs : MonoBehaviour {      private void Awake() {         PlayerPrefs.SetString("name", "Zhang san");         PlayerPrefs.SetInt("age", 25);         PlayerPrefs.SetFloat("height", 1.75f);         Debug.Log("name=" + PlayerPrefs.GetString("name")             + ", age=" + PlayerPrefs.GetInt("age")             + ", height=" + PlayerPrefs.GetFloat("height"));     } } 

​ 打印日志如下。

【Unity3D】资源管理

7 Addressables

​ Addressables 包提供了组织和打包应用内容的工具和脚本,在运行时可以加载和释放资源,官方介绍见→Addressables

1)安装 Addressables

​ 在菜单栏依次选择【Window→Package Manager】,打开包管理器,按照以下步骤安装 Addressables。

【Unity3D】资源管理

2)设置资源为 Addressable

​ 在 Assets 窗口选中资源,在 Inspector 窗口勾选 Addressable,并修改资源 Key,如下。

【Unity3D】资源管理

3)分组管理 Addressables

​ 在菜单栏依次选择【Window→Asset Management→Addressables→Groups】,打开 Addressables Groups 窗口,如下。

【Unity3D】资源管理

​ 可以拖拽 Addressables Groups 窗口,使其停靠在 Game 窗口旁边,如下。可以看到 Resources 目录下的资源会自动添加到 Addressables 中。

【Unity3D】资源管理

​ 依次点击【New→Packed Assets】,创建新包,并重命名,便于对资源进行分组管理,如下。

【Unity3D】资源管理

4)加载资源

​ Test_Addressables.cs

using UnityEngine; using UnityEngine.AddressableAssets; using UnityEngine.ResourceManagement.AsyncOperations;  public class Test_Addressables : MonoBehaviour {     private GameObject cube;      private void Awake() {         Addressables.InstantiateAsync("CubePrefab2").Completed += OnInstantiate;     }      private void OnInstantiate(AsyncOperationHandle<GameObject> operationHandle) { // 游戏对象加载成功回调函数         cube = operationHandle.Result;         Addressables.LoadAssetAsync<Material>("CubeMat2").Completed += OnLoadAsset;     }      private void OnLoadAsset(AsyncOperationHandle<Material> operationHandle) { // 材质加载成功回调函数         cube.GetComponent<Renderer>().material = operationHandle.Result;     } } 

​ 说明:引入 UnityEngine.AddressableAssets 和 UnityEngine.ResourceManagement.AsyncOperations 这两个命名空间时,在 VSCode 中可能会报错,但 Unity 中不会报错,也不影响程序运行。如果想消除 VSCode 中的这个报错,可以重启一下 Unity 和 VSCode,就不会报错了。

​ 声明:本文转自【Unity3D】资源管理

发表评论

评论已关闭。

相关文章