183 lines
6.2 KiB
C#
183 lines
6.2 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Diagnostics;
|
||
using System.IO;
|
||
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");
|
||
// }
|
||
|
||
[SerializeField] private string addressableName;
|
||
[SerializeField] private UXBar progressBar;
|
||
[SerializeField] private UIDocument document;
|
||
[SerializeField] private UXLabel progressLabel;
|
||
|
||
[SerializeField] private bool IsEditorSimulateMode;
|
||
|
||
private float CurrentOpacity
|
||
{
|
||
get => document.rootVisualElement.GetOpacity();
|
||
set => document.rootVisualElement.SetOpacity(value);
|
||
}
|
||
|
||
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
|
||
{
|
||
BuildinQueryServices = new GameQueryServices(),
|
||
RemoteServices = new RemoteServices(defaultHostServer, fallbackHostServer)
|
||
};
|
||
if (IsEditorSimulateMode)
|
||
{
|
||
var editorParameters = new EditorSimulateModeParameters
|
||
{
|
||
SimulateManifestFilePath = EditorSimulateModeHelper.SimulateBuild(EDefaultBuildPipeline.BuiltinBuildPipeline,PackageName)
|
||
};
|
||
initParameters = editorParameters;
|
||
}
|
||
|
||
var initOperation = package.InitializeAsync(initParameters);
|
||
progressLabel.Set("正在初始化资源系统...");
|
||
while (initOperation.IsDone is false)
|
||
{
|
||
await UniTask.NextFrame(destroyCancellationToken);
|
||
progressBar.Set(initOperation.Progress);
|
||
}
|
||
|
||
progressLabel.Set("正在更新资源包版本...");
|
||
var operation = package.UpdatePackageVersionAsync();
|
||
while (operation.IsDone is false)
|
||
{
|
||
await UniTask.NextFrame(destroyCancellationToken);
|
||
progressBar.Set(operation.Progress);
|
||
}
|
||
|
||
if (operation.Status == EOperationStatus.Succeed)
|
||
{
|
||
//更新成功
|
||
string packageVersion = operation.PackageVersion;
|
||
Debug.Log($"Updated package Version : {packageVersion}");
|
||
}
|
||
else
|
||
{
|
||
//更新失败
|
||
Debug.LogError(operation.Error);
|
||
}
|
||
|
||
progressLabel.Set("正在初始化Framework");
|
||
var frameworkHandle = YooAssets.LoadAssetAsync<GameObject>(addressableName);
|
||
while (frameworkHandle.IsDone is false)
|
||
{
|
||
await UniTask.NextFrame(destroyCancellationToken);
|
||
progressBar.Set(frameworkHandle.Progress);
|
||
}
|
||
|
||
var framework = Instantiate(frameworkHandle.AssetObject);
|
||
DontDestroyOnLoad(framework);
|
||
progressLabel.Set("已加载完成");
|
||
|
||
stopwatch.Stop();
|
||
|
||
Destroy(document);
|
||
|
||
BIT4Log.Log<BITFramework>("BITFramework加载完成,耗时:" + stopwatch.ElapsedMilliseconds + "ms");
|
||
|
||
SceneManager.LoadScene(1);
|
||
}
|
||
|
||
private void OnDestroy()
|
||
{
|
||
YooAssets.Destroy();
|
||
}
|
||
}
|
||
|
||
public class GameQueryServices : IBuildinQueryServices
|
||
{
|
||
public bool Query(string packageName, string fileName)
|
||
{
|
||
// 注意:fileName包含文件格式
|
||
return StreamingAssetsHelper.FileExists(packageName, fileName);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 远端资源地址查询服务类
|
||
/// </summary>
|
||
public class RemoteServices : IRemoteServices
|
||
{
|
||
private readonly string _defaultHostServer;
|
||
private readonly string _fallbackHostServer;
|
||
|
||
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}";
|
||
}
|
||
}
|
||
}
|
||
|
||
|