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 方法的调用关系。初始化相关代码就非理解了。这里给出两种调方式。

  1. OperationSystem.Update 调用 AsyncOperationBase.Update
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// OperationSystem.cs
public static void StartOperation(string packageName, AsyncOperationBase operation)
{
_newList.Add(operation); // 注意这个_newList
operation.SetPackageName(packageName);
operation.StartOperation();
}

/// <summary>
/// 更新异步操作系统
/// </summary>
public static void Update()
{
// 移除已经完成的异步操作
// 注意:移除上一帧完成的异步操作,方便调试器接收到完整的信息!
for (int i = _operations.Count - 1; i >= 0; i--)
{
AsyncOperationBase operation = _operations[i];
if (operation.IsFinish)
_operations.RemoveAt(i);
}

// 添加新增的异步操作
if (_newList.Count > 0)
{
bool sorting = false;
foreach (AsyncOperationBase operation in _newList)
{
if (operation.Priority > 0)
{
sorting = true;
break;
}
}

_operations.AddRange(_newList);
_newList.Clear();

// 重新排序优先级
if (sorting)
_operations.Sort();
}

// 更新进行中的异步操作
_frameTime = _watch.ElapsedMilliseconds;
for (int i = 0; i < _operations.Count; i++)
{
if (IsBusy)
break;

var operation = _operations[i];
if (operation.IsFinish)
continue;

operation.UpdateOperation();
}
}

  1. 子进程调用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// RequestPackageVersionOperation.cs 这个Update由 OperationSystem.Update 调用
internal override void InternalUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;

if (_steps == ESteps.RequestPackageVersion)
{
if (_requestPackageVersionOp == null)
{
// 获取主文件系统
var mainFileSystem = _impl.GetMainFileSystem();
// 请求包裹版本
_requestPackageVersionOp = mainFileSystem.RequestPackageVersionAsync(_appendTimeTicks, _timeout);
// 开始操作
_requestPackageVersionOp.StartOperation();
AddChildOperation(_requestPackageVersionOp);
}

// 注意这个 UpdateOperation 调用 就是子进程的 Update
_requestPackageVersionOp.UpdateOperation();
if (_requestPackageVersionOp.IsDone == false)
return;

if (_requestPackageVersionOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.Done;
PackageVersion = _requestPackageVersionOp.PackageVersion;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _requestPackageVersionOp.Error;
}
}
}

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/
作者
SuHang
发布于
2025年8月1日
许可协议