当前位置: 首页 > news >正文

【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。

        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);}}
}

        打印如下。

4 ScriptableObject

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

        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 窗口可以调整属性,如下。

5 AssetDatabase

        AssetDatabase 用于加载资源,仅在 Unity 编辑器模式下使用,官方介绍见→AssetDatabase、Batching 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);// 实例化Cubecube.GetComponent<Renderer>().material = mat;GameObject.Instantiate(cube);}
}

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

6 PlayerPrefs

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

        PlayerPrefs 的静态方法如下。

        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"));}
}

        打印日志如下。

7 Addressables

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

        1)安装 Addressables

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

        2)设置资源为 Addressable

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

        3)分组管理 Addressables

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

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

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

        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,就不会报错了。

http://www.lryc.cn/news/169681.html

相关文章:

  • 数据结构-----队列
  • postgresql教程
  • 1万6千多最好的背单词SQLITE\ACCESS数据库
  • springboot aop Aspectj 切面
  • Leetcode 2862. Maximum Element-Sum of a Complete Subset of Indices
  • 第一百四十七回 自定义组件一
  • MySQL 重复数据的处理
  • Java文字描边效果实现
  • 【Web_环境搭建_Python3_pip】pip的升级、安装、更新、卸载,以及pipupgrade和pip-review的基础使用
  • 农民朋友有福利啦!建行江门市分行“裕农通+农资结算”平台正式上线
  • super详解
  • GMS地下水数值模拟丨GMS各模块、三维地质模型构建及与MODFLOW耦合、地下水流动数值模拟及报告编制、地下水溶质运移模型、反应性溶质运移等
  • Redis 配置文件详解 - 持久化(RDB、AOF)
  • 在线Excel转JSON工具
  • Spring编程常见错误50例-Spring Bean依赖注入常见错误(下)
  • SpringBoot整合Canal实现MySQL与ES数据同步
  • Zookeeper 源码分析流程
  • 计数排序与基数排序
  • Mysql—表操作
  • SpringCloud——微服务
  • 深入理解Java单例模式和优化多线程任务处理
  • 已解决 Kotlin Error: Type mismatch: inferred type is String but Int was expected
  • Web应用系统的小安全漏洞及相应的攻击方式
  • git工具下载和安装
  • 腾讯mini项目-【指标监控服务重构】2023-08-04
  • 怎么推广自己抖店的商品?最适合0经验新手操作的办法,来看看
  • 线性代数的本质(三)——线性方程组
  • 轻量级性能测试工具 wrk 如何使用?
  • WebGL 视图矩阵、模型视图矩阵
  • Python 3 – 文件 readline() 方法