This commit is contained in:
CortexCore
2024-03-31 23:31:00 +08:00
parent e179d2eb53
commit b7b89ee71a
641 changed files with 31286 additions and 22134 deletions

View File

@@ -3,6 +3,7 @@ using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using BITKit.IO;
using BITKit.UX;
using Cysharp.Threading.Tasks;
using UnityEngine;
@@ -39,105 +40,153 @@ namespace BITKit
// BIT4Log.Log<BITFramework>($"BITFramework加载完成,耗时:{Stopwatch.ElapsedMilliseconds}ms");
// }
[SerializeField] private string addressableName;
[SerializeField] private UXBar progressBar;
[SerializeField] private UIDocument document;
[SerializeField] private UXLabel progressLabel;
[SerializeReference, SubclassSelector] private IReference addressableName;
[SerializeReference, SubclassSelector] private IReference packageName = new Reference("DefaultPackages");
[SerializeReference, SubclassSelector] private IRemoteServices remoteServices;
[SerializeReference, SubclassSelector] private IBuildinQueryServices buildinQueryServices;
[SerializeField] private UIDocument document;
[SerializeField] private bool isOffline;
[SerializeField] private bool IsEditorSimulateMode;
private float CurrentOpacity
{
get => document.rootVisualElement.GetOpacity();
set => document.rootVisualElement.SetOpacity(value);
}
[UXBindPath("progress-bar")]
private ProgressBar _progressBar;
[UXBindPath("progress-label")]
private Label _progressLabel;
private async void Start()
{
#if UNITY_EDITOR
#else
IsEditorSimulateMode=false;
#endif
progressBar.Set(0f);
var stopwatch = new Stopwatch();
stopwatch.Start();
DontDestroyOnLoad(gameObject);
// 初始化资源系统
YooAssets.Initialize();
const string PackageName = "DefaultPackages";
// 创建默认的资源包
var package = YooAssets.TryGetPackage(PackageName) ?? YooAssets.CreatePackage(PackageName);
// 设置该资源包为默认的资源包可以使用YooAssets相关加载接口加载该资源包内容。
YooAssets.SetDefaultPackage(package);
const string defaultHostServer = "http://127.0.0.1/CDN/PC/v1.0";
const string fallbackHostServer = "http://127.0.0.1/CDN/PC/v1.0";
InitializeParameters initParameters = new HostPlayModeParameters
try
{
BuildinQueryServices = new GameQueryServices(),
RemoteServices = new RemoteServices(defaultHostServer, fallbackHostServer)
};
if (IsEditorSimulateMode)
{
var editorParameters = new EditorSimulateModeParameters
#if UNITY_EDITOR
#else
IsEditorSimulateMode = false;
#endif
UXUtils.Inject(this);
_progressBar.value=0f;
var stopwatch = new Stopwatch();
stopwatch.Start();
DontDestroyOnLoad(gameObject);
// 初始化资源系统
YooAssets.Initialize();
// 创建默认的资源包
var package = YooAssets.TryGetPackage(packageName.Value) ?? YooAssets.CreatePackage(packageName.Value);
// 设置该资源包为默认的资源包可以使用YooAssets相关加载接口加载该资源包内容。
YooAssets.SetDefaultPackage(package);
InitializeParameters initParameters = new HostPlayModeParameters
{
SimulateManifestFilePath = EditorSimulateModeHelper.SimulateBuild("BuiltinBuildPipeline",PackageName)
BuildinQueryServices = buildinQueryServices,
RemoteServices = remoteServices
};
initParameters = editorParameters;
}
var initOperation = package.InitializeAsync(initParameters);
progressLabel.Set("正在初始化资源系统...");
while (initOperation.IsDone is false)
{
await UniTask.NextFrame(destroyCancellationToken);
progressBar.Set(initOperation.Progress);
}
if (IsEditorSimulateMode)
{
var editorParameters = new EditorSimulateModeParameters
{
SimulateManifestFilePath =
EditorSimulateModeHelper.SimulateBuild("ScriptableBuildPipeline", packageName.Value)
};
initParameters = editorParameters;
}else if (isOffline)
{
initParameters = new OfflinePlayModeParameters();
}
progressLabel.Set("正在更新资源包版本...");
var operation = package.UpdatePackageVersionAsync();
while (operation.IsDone is false)
{
await UniTask.NextFrame(destroyCancellationToken);
progressBar.Set(operation.Progress);
}
InitializationOperation initOperation = null;
try
{
initOperation = package.InitializeAsync(initParameters);
_progressLabel.text ="正在初始化资源系统...";
}
catch (Exception e)
{
_progressLabel.text =e.Message;
if (operation.Status == EOperationStatus.Succeed)
{
//更新成功
string packageVersion = operation.PackageVersion;
Debug.Log($"Updated package Version : {packageVersion}");
}
else
{
//更新失败
Debug.LogError(operation.Error);
}
initParameters = new HostPlayModeParameters
{
BuildinQueryServices = buildinQueryServices,
RemoteServices = remoteServices
};
progressLabel.Set("正在初始化Framework");
var frameworkHandle = YooAssets.LoadAssetAsync<GameObject>(addressableName);
while (frameworkHandle.IsDone is false)
{
await UniTask.NextFrame(destroyCancellationToken);
progressBar.Set(frameworkHandle.Progress);
}
initOperation = package.InitializeAsync(initParameters);
}
var framework = Instantiate(frameworkHandle.AssetObject);
DontDestroyOnLoad(framework);
progressLabel.Set("已加载完成");
while (initOperation.IsDone is false)
{
await UniTask.NextFrame(destroyCancellationToken);
_progressBar.value =initOperation.Progress;
}
try
{
_progressLabel.text="正在更新资源包版本...";
var operation = package.UpdatePackageVersionAsync();
while (operation.IsDone is false)
{
await UniTask.NextFrame(destroyCancellationToken);
_progressBar.value = operation.Progress;
}
if (operation.Status == EOperationStatus.Succeed)
{
//更新成功
string packageVersion = operation.PackageVersion;
Debug.Log($"Updated package Version : {packageVersion}");
}
else
{
//更新失败
Debug.LogError(operation.Error);
}
}
catch (Exception e)
{
BIT4Log.LogException(e);
_progressLabel.text =e.Message;
}
stopwatch.Stop();
Destroy(document);
BIT4Log.Log<BITFramework>("BITFramework加载完成,耗时:" + stopwatch.ElapsedMilliseconds + "ms");
SceneManager.LoadScene(1);
if (addressableName is not null)
{
_progressLabel.text ="正在初始化Framework";
var frameworkHandle = YooAssets.LoadAssetAsync<GameObject>(addressableName.Value);
while (frameworkHandle.IsDone is false)
{
await UniTask.NextFrame(destroyCancellationToken);
_progressBar.value=frameworkHandle.Progress;
}
var framework = Instantiate(frameworkHandle.AssetObject);
DontDestroyOnLoad(framework);
_progressLabel.text="已加载完成";
BIT4Log.Log<BITFramework>("BITFramework加载完成,耗时:" + stopwatch.ElapsedMilliseconds + "ms");
}
stopwatch.Stop();
YooAssetUtils.RegisterPackage(packageName.Value);
YooAssetUtils.RegisterResourcePackage(package);
if (document)
Destroy(document);
SceneManager.LoadScene(1);
}
catch (Exception e)
{
_progressBar.value =0;
_progressLabel.text = e.Message;
}
}
private void OnDestroy()
@@ -146,23 +195,43 @@ namespace BITKit
}
}
public class GameQueryServices : IBuildinQueryServices
[Serializable]
public sealed class GameQueryServices : IBuildinQueryServices
{
public bool Query(string packageName, string fileName)
{
// 注意fileName包含文件格式
return StreamingAssetsHelper.FileExists(packageName, fileName);
}
public bool Query(string packageName, string fileName, string fileCRC)=>Query(packageName,fileName);
}
[Serializable]
public sealed class LocalQueryServices : IBuildinQueryServices
{
public readonly string Path;
public LocalQueryServices(string path)
{
Path = path;
}
public bool Query(string packageName, string fileName, string fileCRC)
{
return File.Exists(System.IO.Path.Combine(Path, fileName));
}
}
/// <summary>
/// 远端资源地址查询服务类
/// </summary>
public class RemoteServices : IRemoteServices
[Serializable]
public sealed class RemoteServices : IRemoteServices
{
private readonly string _defaultHostServer;
private readonly string _fallbackHostServer;
[SerializeField] private string _defaultHostServer;
[SerializeField] private string _fallbackHostServer;
public RemoteServices(){}
public RemoteServices(string defaultHostServer, string fallbackHostServer)
{
_defaultHostServer = defaultHostServer;