YooAssets源码解读-初始化
YooAssets源码解读 - 初始化
最近打完包,刚好有空,看一下项目中使用的 YooAsset 源码,这部分是关于初始化的代码。
YooAsset.Initialize()
初始化,主要记录 YooAsset 在初始化时做了什么
关键类
- YooAsset.cs
- YooAssetDriver.cs
- OperationSystem.cs
流程图
graph TD
A[YooAsset] --> B[Initialize]
B --> YooAssetDriver[YooAssetDriver]
YooAssetDriver --> Update[Update]
Update --> A
A --> OperationSystemUpdate[OperationSystemUpdate]
B --> OperationSystem[OperationSystem]
OperationSystem --> Initialize[Initialize]
OperationSystem --> OperationSystemUpdate
%% 暗色主题样式
style A fill:#ff6b6b,stroke:#ffffff,stroke-width:3px,color:#ffffff
style B fill:#4ecdc4,stroke:#ffffff,stroke-width:3px,color:#000000
style YooAssetDriver fill:#45b7d1,stroke:#ffffff,stroke-width:3px,color:#ffffff
style Update fill:#f9ca24,stroke:#ffffff,stroke-width:3px,color:#000000
style OperationSystem fill:#6c5ce7,stroke:#ffffff,stroke-width:3px,color:#ffffff
style OperationSystemUpdate fill:#a29bfe,stroke:#ffffff,stroke-width:3px,color:#ffffff
style Initialize fill:#fd79a8,stroke:#ffffff,stroke-width:3px,color:#ffffff
初始化包
YooAsset 在初始化时会创建一个资源包(ResourcePackage),并设置其运行模式。
关键类
- YooAsset.cs
- ResourcePackage.cs
流程图
graph TD
A[YooAsset.CreatePackage] -->|new| ResourcePackage[ResourcePackage]
%% 暗色主题样式
style A fill:#00d2d3,stroke:#ffffff,stroke-width:3px,color:#ffffff
style ResourcePackage fill:#ff9ff3,stroke:#ffffff,stroke-width:3px,color:#000000
初始化资源运行模式
初始化资源的运行模式(OfflinePlayModeParameters, EditorSimulateModeParameters, HostPlayModeParameters, WebPlayModeParameters)。
关键类
- ResourcePackage.cs
- PlayModeImpl.cs
- InitializeParameters.cs
流程图
graph TD
F[FileSystemParameters]
A[ResourcePackage.InitializeAsync] --> AA[PlayModeImpl]
AA --> InitializationOperation[InitializationOperation
初始化操作]
InitializationOperation -.-> B[EditorSimulateModeParameters.InitializeAsync]
InitializationOperation -.-> C[OfflinePlayModeParameters.InitializeAsync]
InitializationOperation -.-> D[HostPlayModeParameters.InitializeAsync]
InitializationOperation -.-> E[WebPlayModeParameters.InitializeAsync]
%% 暗色主题样式
style InitializationOperation fill:#00aaff,stroke:#ffffff,stroke-width:3px,color:#ffffff
style A fill:#ff3838,stroke:#ffffff,stroke-width:3px,color:#ffffff
style AA fill:#2ed573,stroke:#ffffff,stroke-width:3px,color:#000000
style B fill:#ffa502,stroke:#ffffff,stroke-width:3px,color:#000000
style C fill:#ffa502,stroke:#ffffff,stroke-width:3px,color:#000000
style D fill:#ffa502,stroke:#ffffff,stroke-width:3px,color:#000000
style E fill:#ffa502,stroke:#ffffff,stroke-width:3px,color:#000000
继承关系图
graph LR
%% 继承关系清晰展示
InitializationOperation[InitializationOperation
初始化操作]
EditorSimulateModeParameters[EditorSimulateModeParameters
编辑器模拟模式参数]
OfflinePlayModeParameters[OfflinePlayModeParameters
离线运行模式参数]
HostPlayModeParameters[HostPlayModeParameters
主机运行模式参数]
WebPlayModeParameters[WebPlayModeParameters
Web运行模式参数]
InitializationOperation -.->|派生| EditorSimulateModeParameters
InitializationOperation -.->|派生| OfflinePlayModeParameters
InitializationOperation -.->|派生| HostPlayModeParameters
InitializationOperation -.->|派生| WebPlayModeParameters
%% 样式 - 暗色主题高对比度
style InitializationOperation fill:#00aaff,stroke:#ffffff,stroke-width:3px,color:#ffffff
style EditorSimulateModeParameters fill:#ff6600,stroke:#ffffff,stroke-width:3px,color:#ffffff
style OfflinePlayModeParameters fill:#00ff88,stroke:#ffffff,stroke-width:3px,color:#000000
style HostPlayModeParameters fill:#ff4444,stroke:#ffffff,stroke-width:3px,color:#ffffff
style WebPlayModeParameters fill:#44ff44,stroke:#ffffff,stroke-width:3px,color:#000000
获取资源版本
关键类
- ResourcePackage.cs
- OperationSystem.cs
- PlayModeImpl.cs
- RequestPackageVersionImplOperation.cs
- AsyncOperationBase.cs
- RequestPackageVersionOperation.cs
流程图
需要注意黄色 YooAsset.Update 是一个独立的分支,看到这里理解了 AsyncOperationBase.Update 和 各个异步操作的 InternalUpdate 方法的调用关系。初始化相关代码就非理解了。这里给出两种调方式。
- OperationSystem.Update 调用 AsyncOperationBase.Update
1 |
|
- 子进程调用
1 |
|
graph TB
%% ========== 请求发起层 ==========
A[ResourcePackage.RequestPackageVersionAsync]
%% ========== 系统管理层 ==========
A --> B[PlayModeImpl.RequestPackageVersionAsync]
A --> C[OperationSystem.StartOperation]
%% ========== 操作创建层 ==========
B --> D[RequestPackageVersionImplOperation]
C --> EA[AsyncOperationBase.StartOperation]
%% ========== 操作执行层 ==========
EA --> FA[RequestPackageVersionOperation.InternalStart]
%% ========== 异步更新流程(独立分支)==========
G[YooAsset.Update] --> H[OperationSystem.Update]
H --> EB[AsyncOperationBase.Update]
EB --> FB[RequestPackageVersionOperation.InternalUpdate]
FB --> DA[RequestPackageVersionImplOperation.InternalUpdate]
%% 样式定义 - 适配暗色主题的高对比度配色
classDef requestLayer fill:#ff4444,stroke:#ffffff,stroke-width:3px,color:#ffffff
classDef systemLayer fill:#44ff44,stroke:#ffffff,stroke-width:3px,color:#000000
classDef operationLayer fill:#4444ff,stroke:#ffffff,stroke-width:3px,color:#ffffff
classDef updateLayer fill:#ffaa00,stroke:#ffffff,stroke-width:3px,color:#000000
class A requestLayer
class B,C systemLayer
class D,EA,FA operationLayer
class G,H,EB,FB,DA updateLayer
继承关系图
graph LR
%% 继承关系清晰展示
AsyncOperationBase[AsyncOperationBase
异步操作基类]
RequestPackageVersionOperation[RequestPackageVersionOperation
请求包版本操作]
RequestPackageVersionImplOperation[RequestPackageVersionImplOperation
请求包版本实现操作]
AsyncOperationBase -.->|派生| RequestPackageVersionOperation
RequestPackageVersionOperation -.->|派生| RequestPackageVersionImplOperation
%% 样式 - 暗色主题高对比度
style AsyncOperationBase fill:#00aaff,stroke:#ffffff,stroke-width:3px,color:#ffffff
style RequestPackageVersionOperation fill:#ff6600,stroke:#ffffff,stroke-width:3px,color:#ffffff
style RequestPackageVersionImplOperation fill:#00ff88,stroke:#ffffff,stroke-width:3px,color:#000000
更新资源清单
基本上和获取资源版本差不多的逻辑。(懒了,不写了)。
YooAssets源码解读-初始化
https://lshgame.com/2025/08/01/YooAssets_Source_Code_Reading_Initialization/