2023-11-15 23:55:06 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
2024-03-31 23:31:00 +08:00
|
|
|
|
using BITKit.IO;
|
2023-11-15 23:55:06 +08:00
|
|
|
|
using BITKit.UX;
|
|
|
|
|
using Cysharp.Threading.Tasks;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.Rendering;
|
|
|
|
|
using UnityEngine.SceneManagement;
|
|
|
|
|
using UnityEngine.UIElements;
|
|
|
|
|
using YooAsset;
|
|
|
|
|
using Debug = UnityEngine.Debug;
|
|
|
|
|
|
|
|
|
|
namespace BITKit
|
|
|
|
|
{
|
|
|
|
|
public class BITFramework : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
private static System.Diagnostics.Stopwatch Stopwatch;
|
|
|
|
|
|
|
|
|
|
[RuntimeInitializeOnLoadMethod]
|
|
|
|
|
private static void Reload()
|
|
|
|
|
{
|
|
|
|
|
Stopwatch = new Stopwatch();
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
#else
|
|
|
|
|
SplashScreen.Stop(SplashScreen.StopBehavior.StopImmediate);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
// [SerializeField] private AssetReference serviceReference;
|
|
|
|
|
// private async void Start()
|
|
|
|
|
// {
|
|
|
|
|
// if(InitializationState is not InitializationState.None)return;
|
|
|
|
|
// InitializationState = InitializationState.Initializing;
|
|
|
|
|
// var asyncOperationHandle = serviceReference.LoadAssetAsync<GameObject>();
|
|
|
|
|
// await UniTask.WaitUntil(() => asyncOperationHandle.IsDone);
|
|
|
|
|
// DontDestroyOnLoad(Instantiate(asyncOperationHandle.Result));
|
|
|
|
|
// Stopwatch.Stop();
|
|
|
|
|
// BIT4Log.Log<BITFramework>($"BITFramework加载完成,耗时:{Stopwatch.ElapsedMilliseconds}ms");
|
|
|
|
|
// }
|
|
|
|
|
|
2024-03-31 23:31:00 +08:00
|
|
|
|
[SerializeReference, SubclassSelector] private IReference addressableName;
|
|
|
|
|
[SerializeReference, SubclassSelector] private IReference packageName = new Reference("DefaultPackages");
|
|
|
|
|
|
|
|
|
|
[SerializeReference, SubclassSelector] private IRemoteServices remoteServices;
|
|
|
|
|
[SerializeReference, SubclassSelector] private IBuildinQueryServices buildinQueryServices;
|
|
|
|
|
|
2024-05-31 01:23:15 +08:00
|
|
|
|
[SerializeField] private Optional<string> loadEntryScene;
|
|
|
|
|
|
2023-11-15 23:55:06 +08:00
|
|
|
|
[SerializeField] private UIDocument document;
|
|
|
|
|
|
2024-03-31 23:31:00 +08:00
|
|
|
|
[SerializeField] private bool isOffline;
|
2023-11-15 23:55:06 +08:00
|
|
|
|
[SerializeField] private bool IsEditorSimulateMode;
|
|
|
|
|
|
2024-03-31 23:31:00 +08:00
|
|
|
|
[UXBindPath("progress-bar")]
|
|
|
|
|
private ProgressBar _progressBar;
|
|
|
|
|
[UXBindPath("progress-label")]
|
|
|
|
|
private Label _progressLabel;
|
2023-11-15 23:55:06 +08:00
|
|
|
|
private async void Start()
|
|
|
|
|
{
|
2024-03-31 23:31:00 +08:00
|
|
|
|
try
|
2023-11-15 23:55:06 +08:00
|
|
|
|
{
|
2024-03-31 23:31:00 +08:00
|
|
|
|
#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
|
2023-11-15 23:55:06 +08:00
|
|
|
|
{
|
2024-03-31 23:31:00 +08:00
|
|
|
|
BuildinQueryServices = buildinQueryServices,
|
|
|
|
|
RemoteServices = remoteServices
|
2023-11-15 23:55:06 +08:00
|
|
|
|
};
|
2024-03-31 23:31:00 +08:00
|
|
|
|
if (IsEditorSimulateMode)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
var editorParameters = new EditorSimulateModeParameters
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
SimulateManifestFilePath =
|
|
|
|
|
EditorSimulateModeHelper.SimulateBuild("ScriptableBuildPipeline", packageName.Value)
|
|
|
|
|
};
|
|
|
|
|
initParameters = editorParameters;
|
|
|
|
|
}else if (isOffline)
|
|
|
|
|
{
|
|
|
|
|
initParameters = new OfflinePlayModeParameters();
|
|
|
|
|
}
|
2023-11-15 23:55:06 +08:00
|
|
|
|
|
2024-03-31 23:31:00 +08:00
|
|
|
|
InitializationOperation initOperation = null;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
initOperation = package.InitializeAsync(initParameters);
|
|
|
|
|
_progressLabel.text ="正在初始化资源系统...";
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_progressLabel.text =e.Message;
|
2023-11-15 23:55:06 +08:00
|
|
|
|
|
2024-03-31 23:31:00 +08:00
|
|
|
|
initParameters = new HostPlayModeParameters
|
|
|
|
|
{
|
|
|
|
|
BuildinQueryServices = buildinQueryServices,
|
|
|
|
|
RemoteServices = remoteServices
|
|
|
|
|
};
|
2023-11-15 23:55:06 +08:00
|
|
|
|
|
2024-03-31 23:31:00 +08:00
|
|
|
|
initOperation = package.InitializeAsync(initParameters);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (initOperation.IsDone is false)
|
|
|
|
|
{
|
|
|
|
|
await UniTask.NextFrame(destroyCancellationToken);
|
|
|
|
|
_progressBar.value =initOperation.Progress;
|
|
|
|
|
}
|
2023-11-15 23:55:06 +08:00
|
|
|
|
|
2024-03-31 23:31:00 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
2023-11-15 23:55:06 +08:00
|
|
|
|
|
2024-03-31 23:31:00 +08:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2024-05-31 01:23:15 +08:00
|
|
|
|
if (loadEntryScene.Allow)
|
|
|
|
|
{
|
|
|
|
|
_progressLabel.text="正在加载场景...";
|
|
|
|
|
await package.LoadSceneAsync(loadEntryScene.Value);
|
|
|
|
|
if (document)
|
|
|
|
|
Destroy(document);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (document)
|
|
|
|
|
Destroy(document);
|
|
|
|
|
SceneManager.LoadScene(1);
|
|
|
|
|
}
|
2024-03-31 23:31:00 +08:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2024-05-31 01:23:15 +08:00
|
|
|
|
await UniTask.SwitchToMainThread();
|
2024-03-31 23:31:00 +08:00
|
|
|
|
_progressBar.value =0;
|
|
|
|
|
_progressLabel.text = e.Message;
|
|
|
|
|
}
|
2023-11-15 23:55:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDestroy()
|
|
|
|
|
{
|
|
|
|
|
YooAssets.Destroy();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-31 23:31:00 +08:00
|
|
|
|
[Serializable]
|
|
|
|
|
public sealed class GameQueryServices : IBuildinQueryServices
|
2023-11-15 23:55:06 +08:00
|
|
|
|
{
|
|
|
|
|
public bool Query(string packageName, string fileName)
|
|
|
|
|
{
|
|
|
|
|
// 注意:fileName包含文件格式
|
|
|
|
|
return StreamingAssetsHelper.FileExists(packageName, fileName);
|
|
|
|
|
}
|
2024-03-31 23:31:00 +08:00
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
}
|
2023-11-15 23:55:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 远端资源地址查询服务类
|
|
|
|
|
/// </summary>
|
2024-03-31 23:31:00 +08:00
|
|
|
|
[Serializable]
|
|
|
|
|
public sealed class RemoteServices : IRemoteServices
|
2023-11-15 23:55:06 +08:00
|
|
|
|
{
|
2024-03-31 23:31:00 +08:00
|
|
|
|
[SerializeField] private string _defaultHostServer;
|
|
|
|
|
[SerializeField] private string _fallbackHostServer;
|
|
|
|
|
public RemoteServices(){}
|
2023-11-15 23:55:06 +08:00
|
|
|
|
public RemoteServices(string defaultHostServer, string fallbackHostServer)
|
|
|
|
|
{
|
|
|
|
|
_defaultHostServer = defaultHostServer;
|
|
|
|
|
_fallbackHostServer = fallbackHostServer;
|
|
|
|
|
}
|
|
|
|
|
string IRemoteServices.GetRemoteMainURL(string fileName)
|
|
|
|
|
{
|
|
|
|
|
return $"{_defaultHostServer}/{fileName}";
|
|
|
|
|
}
|
|
|
|
|
string IRemoteServices.GetRemoteFallbackURL(string fileName)
|
|
|
|
|
{
|
|
|
|
|
return $"{_fallbackHostServer}/{fileName}";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|