增加aa加载包和重做标签系统包

This commit is contained in:
MuHua-123
2025-03-24 17:36:33 +08:00
parent e373ec09e5
commit 0cc1ec3866
30 changed files with 453 additions and 76 deletions
@@ -3,22 +3,18 @@ using System.Collections.Generic;
using UnityEngine;
using MuHua;
namespace MuHua.Sample
{
public class StandardLabel : MonoBehaviour
{
public GameObject labelPrefab;
public Vector3 offset = new Vector3(0, 1, 0);
private GameObject labelObject;
namespace MuHua.Sample {
public class StandardLabel : MonoBehaviour {
public GameObject labelPrefab;
public Vector3 offset = new Vector3(0, 1, 0);
private GameObject labelObject;
void Start()
{
labelObject = LabelController.CreateLabel(transform, labelPrefab, offset);
}
void OnDestroy()
{
if (labelObject != null) { Destroy(labelObject); }
}
}
void Start() {
// labelObject = FollowerController.CreateLabel(transform, labelPrefab, offset);
}
void OnDestroy() {
if (labelObject != null) { Destroy(labelObject); }
}
}
}
+8
View File
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 3699f8709aed43a46bb5220a7124e92b
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
+64
View File
@@ -0,0 +1,64 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.AddressableAssets.ResourceLocators;
using UnityEngine.ResourceManagement.AsyncOperations;
namespace MuHua {
/// <summary>
/// 加载可寻址资源目录
/// </summary>
public class AACatalog {
public readonly string filePath;
public Action<float> OnProgress;
public Action<string> OnError;
public Action OnComplete;
/// <summary> 加载可寻址资源目录 </summary>
public AACatalog(string filePath, Action OnComplete = null) {
this.filePath = filePath;
this.OnComplete = OnComplete;
}
/// <summary> 获取异步句柄 </summary>
public AsyncOperationHandle<IResourceLocator> Handle() {
return Addressables.LoadContentCatalogAsync(filePath, true);
}
/// <summary> 加载目录 </summary>
public async void Load() => await ALoad();
/// <summary> 加载目录 </summary>
public async Task ALoad() {
AsyncOperationHandle<IResourceLocator> handle = Handle();
if (handle.Status == AsyncOperationStatus.Failed) {
OnError?.Invoke($"无法加载资源目录!({filePath})"); return;
}
while (!handle.IsDone) {
float downloadProgress = handle.GetDownloadStatus().Percent;
float loadProgress = handle.PercentComplete;
float totalProgress = (downloadProgress + loadProgress) / 2.0f;
// Debug.Log($"下载进度: {downloadProgress * 100}% , 加载进度: {loadProgress * 100}% , 总进度: {totalProgress * 100}%");
OnProgress?.Invoke(totalProgress);
await Task.Delay(100);
}
OnComplete?.Invoke();
}
/// <summary> 加载目录 </summary>
public IEnumerator ILoad() {
AsyncOperationHandle<IResourceLocator> handle = Handle();
if (handle.Status == AsyncOperationStatus.Failed) {
OnError?.Invoke($"无法加载资源目录!({filePath})");
yield break;
}
while (!handle.IsDone) {
float downloadProgress = handle.GetDownloadStatus().Percent;
float loadProgress = handle.PercentComplete;
float totalProgress = (downloadProgress + loadProgress) / 2.0f;
OnProgress?.Invoke(totalProgress);
yield return new WaitForEndOfFrame();
}
OnComplete?.Invoke();
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: cdf535680c5435444845cfa205c9619f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,57 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.SceneManagement;
namespace MuHua {
/// <summary>
/// 从目录加载场景
/// </summary>
public class AACatalogToScene {
public enum Progress { Catalog, Label, Scene }
public readonly string filePath;
public readonly string sceneName;
public readonly LoadSceneMode loadSceneMode;
public readonly bool activateOnLoad;
public Action<float, Progress> OnProgress;
public Action<string> OnError;
public Action OnComplete;
/// <summary> 加载可寻址资源目录 </summary>
public AACatalogToScene(string filePath, string sceneName, LoadSceneMode loadSceneMode = LoadSceneMode.Single, bool activateOnLoad = true) {
this.filePath = filePath;
this.sceneName = sceneName;
this.loadSceneMode = loadSceneMode;
this.activateOnLoad = activateOnLoad;
}
/// <summary> 从目录加载场景 </summary>
public async void Load() => await ALoad();
/// <summary> 从目录加载场景 </summary>
public async Task ALoad() {
AACatalog catalog = new AACatalog(filePath);
catalog.OnProgress = (value) => { OnProgress?.Invoke(value, Progress.Catalog); };
catalog.OnError = OnError;
await catalog.ALoad();
AAScene aaScene = new AAScene(sceneName, loadSceneMode, activateOnLoad);
aaScene.OnProgress = (value) => { OnProgress?.Invoke(value, Progress.Scene); };
aaScene.OnError = OnError;
aaScene.OnComplete = OnComplete;
await aaScene.ALoad();
}
/// <summary> 从目录加载场景 </summary>
public IEnumerator ILoad() {
AACatalog catalog = new AACatalog(filePath);
catalog.OnProgress = (value) => { OnProgress?.Invoke(value, Progress.Catalog); };
catalog.OnError = OnError;
yield return catalog.ILoad();
AAScene aaScene = new AAScene(sceneName, loadSceneMode, activateOnLoad);
aaScene.OnProgress = (value) => { OnProgress?.Invoke(value, Progress.Scene); };
aaScene.OnError = OnError;
aaScene.OnComplete = OnComplete;
yield return aaScene.ILoad();
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c8003e83f422c6e4797a12c63bf25406
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+65
View File
@@ -0,0 +1,65 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.AddressableAssets.ResourceLocators;
using UnityEngine.ResourceManagement.AsyncOperations;
namespace MuHua {
/// <summary>
/// 按标签加载可寻址资源
/// </summary>
public class AALabel<T> {
public readonly string label;
public Action<float> OnProgress;
public Action<string> OnError;
public Action<T> OnComplete;
public T result;
/// <summary> 按标签加载可寻址资源 </summary>
public AALabel(string label, Action<T> OnComplete = null) {
this.label = label;
this.OnComplete = OnComplete;
}
/// <summary> 获取异步句柄 </summary>
public AsyncOperationHandle<T> Handle() {
return Addressables.LoadAssetAsync<T>(label);
}
/// <summary> 加载资源 </summary>
public async void Load() => await ALoad();
/// <summary> 加载资源 </summary>
public async Task ALoad() {
AsyncOperationHandle<T> handle = Handle();
if (handle.Status == AsyncOperationStatus.Failed) {
OnError?.Invoke($"无法加载资源!(label={label} , type={typeof(T)})"); return;
}
while (!handle.IsDone) {
float downloadProgress = handle.GetDownloadStatus().Percent;
float loadProgress = handle.PercentComplete;
float totalProgress = (downloadProgress + loadProgress) / 2.0f;
// Debug.Log($"下载进度: {downloadProgress * 100}% , 加载进度: {loadProgress * 100}% , 总进度: {totalProgress * 100}%");
OnProgress?.Invoke(totalProgress);
await Task.Delay(100);
}
OnComplete?.Invoke(handle.Result);
}
/// <summary> 加载资源 </summary>
public IEnumerator ILoad() {
AsyncOperationHandle<T> handle = Handle();
if (handle.Status == AsyncOperationStatus.Failed) {
OnError?.Invoke($"无法加载资源!(label={label} , type={typeof(T)})"); yield break;
}
while (!handle.IsDone) {
float downloadProgress = handle.GetDownloadStatus().Percent;
float loadProgress = handle.PercentComplete;
float totalProgress = (downloadProgress + loadProgress) / 2.0f;
OnProgress?.Invoke(totalProgress);
yield return new WaitForEndOfFrame();
}
result = handle.Result;
OnComplete?.Invoke(handle.Result);
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 05c642602f45beb4391c7dbbd6b78968
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+68
View File
@@ -0,0 +1,68 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.AddressableAssets.ResourceLocators;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.ResourceManagement.ResourceProviders;
using UnityEngine.SceneManagement;
namespace MuHua {
/// <summary>
/// 加载可寻址场景
/// </summary>
public class AAScene {
public readonly string name;
public readonly LoadSceneMode loadSceneMode;
public readonly bool activateOnLoad;
public Action<float> OnProgress;
public Action<string> OnError;
public Action OnComplete;
/// <summary> 加载可寻址场景 </summary>
public AAScene(string name, LoadSceneMode loadSceneMode = LoadSceneMode.Single, bool activateOnLoad = true) {
this.name = name;
this.loadSceneMode = loadSceneMode;
this.activateOnLoad = activateOnLoad;
}
/// <summary> 获取异步句柄 </summary>
public AsyncOperationHandle<SceneInstance> Handle() {
return Addressables.LoadSceneAsync(name, loadSceneMode, activateOnLoad);
}
/// <summary> 加载场景 </summary>
public async void Load() => await ALoad();
/// <summary> 加载场景 </summary>
public async Task ALoad() {
AsyncOperationHandle<SceneInstance> handle = Handle();
if (handle.Status == AsyncOperationStatus.Failed) {
OnError?.Invoke($"无法加载场景!({name})"); return;
}
while (!handle.IsDone) {
float downloadProgress = handle.GetDownloadStatus().Percent;
float loadProgress = handle.PercentComplete;
float totalProgress = (downloadProgress + loadProgress) / 2.0f;
// Debug.Log($"下载进度: {downloadProgress * 100}% , 加载进度: {loadProgress * 100}% , 总进度: {totalProgress * 100}%");
OnProgress?.Invoke(totalProgress);
await Task.Delay(100);
}
OnComplete?.Invoke();
}
/// <summary> 加载场景 </summary>
public IEnumerator ILoad() {
AsyncOperationHandle<SceneInstance> handle = Handle();
if (handle.Status == AsyncOperationStatus.Failed) {
OnError?.Invoke($"无法加载场景!({name})"); yield break;
}
while (!handle.IsDone) {
float downloadProgress = handle.GetDownloadStatus().Percent;
float loadProgress = handle.PercentComplete;
float totalProgress = (downloadProgress + loadProgress) / 2.0f;
OnProgress?.Invoke(totalProgress);
yield return new WaitForEndOfFrame();
}
OnComplete?.Invoke();
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 68519b916d961c745aeba6680a8cafe3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,17 @@
{
"name": "MuHua.AALoading",
"rootNamespace": "",
"references": [
"GUID:9e24947de15b9834991c9d8411ea37cf",
"GUID:84651a3751eca9349aac36a66bba901b"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 26f33ddf2c81134458ea0d2283ffaaaa
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
+14
View File
@@ -0,0 +1,14 @@
{
"name": "muhua-aaloading",
"version": "1.0.0",
"displayName": "MuHua AALoading",
"description": "\u534f\u7a0b\u548c\u5f02\u6b65\u5b9e\u73b0\u52a0\u8f7daa\u8d44\u6e90",
"author": {
"name": "MuHua",
"email": "muhua233@qq.com"
},
"type": "tool",
"dependencies": {
"com.unity.addressables": "1.21.21"
}
}
+7
View File
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 03fe4d2014c6ea3488b135e4f4898c14
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,37 @@
using UnityEngine;
namespace MuHua {
/// <summary>
/// 跟随标签控制器
/// </summary>
public abstract class FollowController<T> : MonoBehaviour where T : FollowController<T> {
/// <summary> 模块单例 </summary>
public static T I => instance;
/// <summary> 模块单例 </summary>
protected static T instance;
/// <summary> 初始化 </summary>
protected abstract void Awake();
/// <summary> 替换,并且设置切换场景不销毁 </summary>
protected virtual void Replace(bool isDontDestroy = true) {
if (instance != null) { Destroy(instance.gameObject); }
instance = (T)this;
if (isDontDestroy) { DontDestroyOnLoad(gameObject); }
}
/// <summary> 不替换,并且设置切换场景不销毁 </summary>
protected virtual void NoReplace(bool isDontDestroy = true) {
if (isDontDestroy) { DontDestroyOnLoad(gameObject); }
if (instance == null) { instance = (T)this; }
else { Destroy(gameObject); }
}
/// <summary> 创建标签 </summary>
public static Transform CreateLabel(Transform target, Transform labelPrefab, Transform parent, Vector3 offset) {
Transform labelObject = Instantiate(labelPrefab, parent);
FollowTag followObjectLabel = labelObject.GetComponent<FollowTag>();
followObjectLabel.target = target;
followObjectLabel.offset = offset;
return labelObject;
}
}
}
+19
View File
@@ -0,0 +1,19 @@
using UnityEngine;
namespace MuHua {
/// <summary>
/// 跟随标签
/// </summary>
public class FollowTag : MonoBehaviour {
public Transform target; // 要跟随的目标物体
public Vector3 offset; // 标签的偏移量
protected virtual void Update() {
if (target == null) { return; }
// 设置标签的位置
transform.position = target.position + offset;
// 使标签面向相机
transform.rotation = Camera.main.transform.rotation;
}
}
}
@@ -1,5 +1,5 @@
{
"name": "MuHua.LabelFollow",
"name": "MuHua.FollowTag",
"rootNamespace": "",
"references": [],
"includePlatforms": [],
@@ -1,7 +1,7 @@
{
"name": "muhua-label-follow",
"name": "muhua-follow-tag",
"version": "1.0.0",
"displayName": "MuHua LabelFollow",
"displayName": "MuHua FollowTag",
"description": "\u6d6e\u52a8\u6807\u7b7e\u7cfb\u7edf",
"author": {
"name": "MuHua",
@@ -1,34 +0,0 @@
using UnityEngine;
namespace MuHua
{
public class LabelController : MonoBehaviour
{
public static LabelController Instance { get; private set; }
public Transform parent; // 标签父物体
public GameObject labelPrefab; // 标签预制体
void Awake()
{
if (Instance == null) { Instance = this; }
else { Destroy(gameObject); }
}
// 启用标签
public static void Enable(bool enable) => Instance.parent.gameObject.SetActive(enable);
// 创建标签
public static GameObject CreateLabel(Transform target) => CreateLabel(target, Vector3.zero);
public static GameObject CreateLabel(Transform target, Vector3 offset) => CreateLabel(target, Instance.labelPrefab, offset);
public static GameObject CreateLabel(Transform target, GameObject labelPrefab, Vector3 offset)
{
GameObject labelObject = Instantiate(labelPrefab, Instance.parent);
LabelFollower followObjectLabel = labelObject.GetComponent<LabelFollower>();
followObjectLabel.target = target;
followObjectLabel.offset = offset;
return labelObject;
}
}
}
@@ -1,21 +0,0 @@
using UnityEngine;
namespace MuHua
{
public class LabelFollower : MonoBehaviour
{
public Transform target; // 要跟随的目标物体
public Vector3 offset; // 标签的偏移量
void Update()
{
if (target != null)
{
// 设置标签的位置
transform.position = target.position + offset;
// 使标签面向相机
transform.rotation = Camera.main.transform.rotation;
}
}
}
}
+31 -2
View File
@@ -6,6 +6,20 @@
"source": "embedded",
"dependencies": {}
},
"com.unity.addressables": {
"version": "1.21.21",
"depth": 1,
"source": "registry",
"dependencies": {
"com.unity.scriptablebuildpipeline": "1.21.23",
"com.unity.modules.assetbundle": "1.0.0",
"com.unity.modules.imageconversion": "1.0.0",
"com.unity.modules.jsonserialize": "1.0.0",
"com.unity.modules.unitywebrequest": "1.0.0",
"com.unity.modules.unitywebrequestassetbundle": "1.0.0"
},
"url": "https://packages.unity.cn"
},
"com.unity.burst": {
"version": "1.8.15",
"depth": 1,
@@ -140,6 +154,13 @@
"com.unity.render-pipelines.core": "14.0.10"
}
},
"com.unity.scriptablebuildpipeline": {
"version": "1.21.23",
"depth": 2,
"source": "registry",
"dependencies": {},
"url": "https://packages.unity.cn"
},
"com.unity.searcher": {
"version": "4.9.2",
"depth": 2,
@@ -250,6 +271,14 @@
},
"url": "https://packages.unity.cn"
},
"muhua-aaloading": {
"version": "file:AALoading",
"depth": 0,
"source": "embedded",
"dependencies": {
"com.unity.addressables": "1.21.21"
}
},
"muhua-character": {
"version": "file:Character",
"depth": 0,
@@ -258,8 +287,8 @@
"com.unity.inputsystem": "1.7.0"
}
},
"muhua-label-follow": {
"version": "file:LabelFollow",
"muhua-follow-tag": {
"version": "file:FollowTag",
"depth": 0,
"source": "embedded",
"dependencies": {}