flowchart TB
A["ResourcePackage资源包裹入口"] --> B["ResourceManager资源管理器"]
B --> C["ProviderOperation提供者基类"]
C --> D["AssetProvider单资源提供者"]
C --> E["SubAssetsProvider子资源提供者"]
C --> F["AllAssetsProvider所有资源提供者"]
C --> G["RawFileProvider原生文件提供者"]
C --> H["SceneProvider场景提供者"]
B --> I["LoadBundleFileOperation Bundle加载器"]
I --> J["BundleInfo Bundle信息"]
J --> K["FileSystem文件系统"]
D --> L["AssetHandle资源句柄"]
E --> M["SubAssetsHandle子资源句柄"]
F --> N["AllAssetsHandle所有资源句柄"]
G --> O["RawFileHandle原生文件句柄"]
H --> P["SceneHandle场景句柄"]
%% 样式定义
style A fill:#ff6b6b,stroke:#ffffff,stroke-width:2px,color:#ffffff
style B fill:#4ecdc4,stroke:#ffffff,stroke-width:2px,color:#000000
style C fill:#45b7d1,stroke:#ffffff,stroke-width:2px,color:#ffffff
style D fill:#f9ca24,stroke:#ffffff,stroke-width:2px,color:#000000
style E fill:#6c5ce7,stroke:#ffffff,stroke-width:2px,color:#ffffff
style F fill:#a29bfe,stroke:#ffffff,stroke-width:2px,color:#ffffff
style G fill:#fd79a8,stroke:#ffffff,stroke-width:2px,color:#ffffff
style H fill:#00d2d3,stroke:#ffffff,stroke-width:2px,color:#ffffff
style I fill:#ff9ff3,stroke:#ffffff,stroke-width:2px,color:#000000
style J fill:#00aaff,stroke:#ffffff,stroke-width:2px,color:#ffffff
style K fill:#ff3838,stroke:#ffffff,stroke-width:2px,color:#ffffff
style L fill:#2ed573,stroke:#ffffff,stroke-width:2px,color:#000000
style M fill:#ffa502,stroke:#ffffff,stroke-width:2px,color:#000000
style N fill:#74b9ff,stroke:#ffffff,stroke-width:2px,color:#ffffff
style O fill:#e17055,stroke:#ffffff,stroke-width:2px,color:#ffffff
style P fill:#81ecec,stroke:#ffffff,stroke-width:2px,color:#000000
ResourcePackage - 资源包裹入口
ResourcePackage 是用户接触资源加载的主要入口点,它提供了统一的资源加载接口。
关键类
ResourcePackage.cs (Line 8-1209)
ResourceManager.cs
AssetInfo.cs
核心加载方法分析
ResourcePackage 提供了四种主要的资源加载方式:
1. LoadAssetAsync - 单资源加载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// ResourcePackage.cs Line 689-732 public AssetHandle LoadAssetAsync(AssetInfo assetInfo, uint priority = 0) { DebugCheckInitialize(); return LoadAssetInternal(assetInfo, false, priority); }
// 泛型获取资源对象 public TAsset GetAssetObject<TAsset>() where TAsset : UnityEngine.Object // 同步实例化GameObject public GameObject InstantiateSync() // 异步实例化GameObject public InstantiateOperation InstantiateAsync(bool actived = true) // 等待异步完成 publicvoidWaitForAsyncComplete() // 完成事件 publicevent System.Action<AssetHandle> Completed }
资源加载完整流程
从用户调用到资源加载完成的调用链
flowchart TD
A["用户代码"] -->|package.LoadAssetAsync| B["ResourcePackage.LoadAssetAsync"]
B -->|LoadAssetInternal| C["ResourceManager.LoadAssetAsync"]
C -->|创建或复用| D["AssetProvider"]
D -->|OperationSystem.StartOperation| E["Provider.InternalUpdate"]
E -->|检查Bundle状态| F["ProcessBundleResult"]
F -->|BundleResultObject.LoadAssetAsync| G["FSLoadAssetOperation"]
G -->|Unity AssetBundle.LoadAssetAsync| H["UnityEngine.AssetBundle"]
H -->|加载完成| I["AssetProvider.AssetObject"]
I -->|CreateHandle| J["AssetHandle"]
J -->|返回给用户| K["用户获得资源"]
%% 详细说明
L["关键步骤说明"]
L --> M["ResourcePackage统一入口"]
L --> N["ResourceManager管理Provider"]
L --> O["AssetProvider执行具体加载"]
L --> P["Handle提供用户接口"]
%% 样式定义
style A fill:#ff6b6b,stroke:#ffffff,stroke-width:2px,color:#ffffff
style B fill:#4ecdc4,stroke:#ffffff,stroke-width:2px,color:#000000
style C fill:#45b7d1,stroke:#ffffff,stroke-width:2px,color:#ffffff
style D fill:#f9ca24,stroke:#ffffff,stroke-width:2px,color:#000000
style E fill:#6c5ce7,stroke:#ffffff,stroke-width:2px,color:#ffffff
style F fill:#a29bfe,stroke:#ffffff,stroke-width:2px,color:#ffffff
style G fill:#fd79a8,stroke:#ffffff,stroke-width:2px,color:#ffffff
style H fill:#00d2d3,stroke:#ffffff,stroke-width:2px,color:#ffffff
style I fill:#ff9ff3,stroke:#ffffff,stroke-width:2px,color:#000000
style J fill:#00aaff,stroke:#ffffff,stroke-width:2px,color:#ffffff
style K fill:#ff3838,stroke:#ffffff,stroke-width:2px,color:#ffffff
style L fill:#2ed573,stroke:#ffffff,stroke-width:2px,color:#000000
style M fill:#ffa502,stroke:#ffffff,stroke-width:2px,color:#000000
style N fill:#74b9ff,stroke:#ffffff,stroke-width:2px,color:#ffffff
style O fill:#e17055,stroke:#ffffff,stroke-width:2px,color:#ffffff
style P fill:#81ecec,stroke:#ffffff,stroke-width:2px,color:#000000
详细调用步骤解析
1. 用户入口 - ResourcePackage
1 2
// 用户代码 var handle = package.LoadAssetAsync<Texture2D>("UITextures/Background");
flowchart TD
A["LoadSceneAsync"] -->|生成唯一GUID| B["创建SceneProvider"]
B -->|不复用Provider| C["每次都是新Provider"]
C -->|LoadSceneParameters| D["Unity SceneManager"]
D -->|支持暂停加载| E["suspendLoad=true"]
D -->|正常加载| F["suspendLoad=false"]
E --> G["加载到90%暂停"]
F --> H["完整加载场景"]
G --> I["用户调用ActivateScene"]
H --> J["SceneHandle返回"]
I --> J
%% 样式定义
style A fill:#ff6b6b,stroke:#ffffff,stroke-width:2px,color:#ffffff
style B fill:#4ecdc4,stroke:#ffffff,stroke-width:2px,color:#000000
style C fill:#45b7d1,stroke:#ffffff,stroke-width:2px,color:#ffffff
style D fill:#f9ca24,stroke:#ffffff,stroke-width:2px,color:#000000
style E fill:#6c5ce7,stroke:#ffffff,stroke-width:2px,color:#ffffff
style F fill:#a29bfe,stroke:#ffffff,stroke-width:2px,color:#ffffff
style G fill:#fd79a8,stroke:#ffffff,stroke-width:2px,color:#ffffff
style H fill:#00d2d3,stroke:#ffffff,stroke-width:2px,color:#ffffff
style I fill:#ff9ff3,stroke:#ffffff,stroke-width:2px,color:#000000
style J fill:#00aaff,stroke:#ffffff,stroke-width:2px,color:#ffffff
if (_downloadFileOp.IsDone) { if (_downloadFileOp.Status == EOperationStatus.Succeed) { _steps = ESteps.LoadAssetBundle; // 下载成功,继续加载 } else { _steps = ESteps.Done; Status = EOperationStatus.Failed; Error = _downloadFileOp.Error; // 下载失败 } } }
按需下载流程图
graph TD
A[开始加载资源] --> B[CheckExist检查文件]
B --> C{文件是否存在?}
C -->|存在| D[LoadAssetBundle直接加载]
C -->|不存在| E{是否禁用按需下载?}
E -->|是| F[加载失败返回错误]
E -->|否| G[DownloadFile开始下载]
G --> H[创建下载操作]
H --> I[更新下载进度]
I --> J{下载是否完成?}
J -->|否| I
J -->|是| K{下载是否成功?}
K -->|成功| D
K -->|失败| L[加载失败返回下载错误]
D --> M[CheckResult检查加载结果]
M --> N[加载完成]
F --> O[结束]
L --> O
N --> O
%% 暗色主题样式
style A fill:#ff6b6b,stroke:#ffffff,stroke-width:3px,color:#ffffff
style B fill:#4ecdc4,stroke:#ffffff,stroke-width:3px,color:#000000
style C fill:#f9ca24,stroke:#ffffff,stroke-width:3px,color:#000000
style D fill:#6c5ce7,stroke:#ffffff,stroke-width:3px,color:#ffffff
style E fill:#a29bfe,stroke:#ffffff,stroke-width:3px,color:#ffffff
style F fill:#fd79a8,stroke:#ffffff,stroke-width:3px,color:#ffffff
style G fill:#00d2d3,stroke:#ffffff,stroke-width:3px,color:#ffffff
style H fill:#ff9ff3,stroke:#ffffff,stroke-width:3px,color:#000000
style I fill:#00aaff,stroke:#ffffff,stroke-width:3px,color:#ffffff
style J fill:#ff3838,stroke:#ffffff,stroke-width:3px,color:#ffffff
style K fill:#2ed573,stroke:#ffffff,stroke-width:3px,color:#000000
style L fill:#ffa502,stroke:#ffffff,stroke-width:3px,color:#000000
style M fill:#74b9ff,stroke:#ffffff,stroke-width:3px,color:#ffffff
style N fill:#e17055,stroke:#ffffff,stroke-width:3px,color:#ffffff
style O fill:#81ecec,stroke:#ffffff,stroke-width:3px,color:#000000
配置控制选项
YooAsset提供了DisableOnDemandDownload配置项来控制按需下载行为:
1 2 3 4 5 6
// 在初始化参数中配置 var initParameters = new HostPlayModeParameters() { // 其他配置... DisableOnDemandDownload = false// 允许按需下载(默认) };