【Unity实战|热更】Addressable读取SO文件报错解决

情景再现

假定你有一个Unity工程,使用了HybridCLR和Addressable,SO文件存放在Addressable中。热更加载后进入游戏场景出现了SO文件读取报错:

复制代码
UnityEngine.AddressableAssets.InvalidKeyException: Exception of type 'UnityEngine.AddressableAssets.InvalidKeyException' was thrown. No Asset found with for Key=Inventory Model Database. Key exists as Type=System.Object, which is not assignable from the requested Type=InventorySystem.Models.InventoryModelDatabase
UnityEngine.AddressableAssets.Addressables:LoadAssetAsync(Object)

可能原因

读取SO文件时,文件绑定的脚本并没有成功还原出来。见官方文档

解决办法

加载SO文件的代码时不要声明具体的类型,改用System.Object,成功后进行强转。

Before:

cs 复制代码
Addressables.LoadAssetAsync<InventoryModelDatabase>("Inventory Model Database").Completed += handle =>
			{
				if (handle.Status == AsyncOperationStatus.Succeeded)
				{
					_inventoryModelDatabase = handle.Result;
					Debug.Log($"[{GetType()}] Inventory Model Database loaded!");
				}
				else
					Debug.LogError($"[{GetType()}] Failed to load Inventory Model Database!");
			};

After:

cs 复制代码
Addressables.LoadAssetAsync<System.Object>("Inventory Model Database").Completed += handle =>
			{
				if (handle.Status == AsyncOperationStatus.Succeeded)
				{
					_inventoryModelDatabase = handle.Result as InventoryModelDatabase;
					Debug.Log($"[{GetType()}] Inventory Model Database loaded!");
				}
				else
					Debug.LogError($"[{GetType()}] Failed to load Inventory Model Database!");
			};

问题解决。

相关推荐
风痕天际37 分钟前
Godot扫雷游戏制作记录3——随机埋雷
游戏·游戏引擎·godot
淡海水1 天前
【节点】[EvaluateTipThickness节点]原理解析与实际应用
unity·游戏引擎·shadergraph·图形·evaluate·thickness
小贺儿开发1 天前
Unity3D 木胎雕刻
科技·unity·人机交互·互动·雕刻
HY小海2 天前
【Unity游戏创作】常见的设计模式
unity·设计模式·c#·游戏程序
淡海水3 天前
【节点】[EvaluateSimulationAdditionalData节点]原理解析与实际应用
unity·游戏引擎·shadergraph·图形·simulation·evaluate
小贺儿开发4 天前
Unity3D 文物互动大屏
3d·unity·实时互动·udp·socket·网络通信
秦奈4 天前
Unity学习复习随笔(12):网络开发基础
网络·笔记·学习·unity
淡海水4 天前
【节点】[EvaluateRefractionData节点]原理解析与实际应用
unity·游戏引擎·shadergraph·data·图形·evaluate·refraction
淡海水4 天前
【节点】[EvaluateScatteringColor节点]原理解析与实际应用
unity·游戏引擎·shadergraph·color·图形·evaluate·scattering
风痕天际5 天前
Godot扫雷游戏制作记录2——鼠标交互
游戏·游戏引擎·godot