1
This commit is contained in:
@@ -1,19 +1,116 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using AYellowpaper.SerializedCollections;
|
||||
using UnityEngine;
|
||||
using YooAsset;
|
||||
using Object = UnityEngine.Object;
|
||||
using Random = UnityEngine.Random;
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
#endif
|
||||
|
||||
namespace BITKit
|
||||
{
|
||||
public class AddressableHelper
|
||||
public class AddressableHelper:MonoBehaviour
|
||||
{
|
||||
public static string GetAddressablePath(Object obj)
|
||||
private static IDictionary<ulong,string> PathsById { get; } = new Dictionary<ulong, string>();
|
||||
public static T Get<T>(ulong id) where T : Object
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
return string.Empty;
|
||||
#else
|
||||
throw new NotImplementedException();
|
||||
#endif
|
||||
var task = YooAssets.LoadAssetAsync<T>(PathsById[id]);
|
||||
task.WaitForAsyncComplete();
|
||||
return task.AssetObject.As<T>();
|
||||
}
|
||||
[SerializeField] private SerializedDictionary<ulong,string> pathsById;
|
||||
private void Start()
|
||||
{
|
||||
PathsById.Clear();
|
||||
foreach (var x in pathsById)
|
||||
{
|
||||
PathsById.Add(x.Key,x.Value);
|
||||
}
|
||||
}
|
||||
#if UNITY_EDITOR
|
||||
[BIT]
|
||||
private void BuildCache()
|
||||
{
|
||||
var guids = AssetDatabase.FindAssets($"t:Object",new[] {"Assets"});
|
||||
var paths = guids.Select(AssetDatabase.GUIDToAssetPath);
|
||||
|
||||
var objects = new List<IAddressable>();
|
||||
var stringBuilder = new System.Text.StringBuilder();
|
||||
|
||||
|
||||
foreach (var path in paths)
|
||||
{
|
||||
var asset = AssetDatabase.LoadAssetAtPath<Object>(path);
|
||||
switch (asset)
|
||||
{
|
||||
case GameObject go when go.TryGetComponent(out IAddressable addressable):
|
||||
objects.Add(addressable);
|
||||
break;
|
||||
case IAddressable addressable:
|
||||
objects.Add(addressable);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
stringBuilder.AppendLine($"所有资源数量:{guids.Length},其中包含{objects.Count}个Addressable资源");
|
||||
|
||||
pathsById.Clear();
|
||||
foreach (var x in objects)
|
||||
{
|
||||
if (x is not Object unityObject) continue;
|
||||
|
||||
//if (x.AddressableId is ulong.MinValue or ulong.MaxValue)
|
||||
{
|
||||
x.AddressableId = RandomUlong.NextUlong;
|
||||
EditorUtility.SetDirty(unityObject);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (x.AddressableId is not ulong.MinValue && !string.IsNullOrEmpty(x.AddressablePath))
|
||||
{
|
||||
if (pathsById.TryAdd(x.AddressableId, x.AddressablePath))
|
||||
{
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
stringBuilder.AppendLine($"资源{unityObject.name}的AddressableId:{x.AddressableId}已经存在");
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
stringBuilder.AppendLine($"{unityObject.name}的AddressableId或AddressablePath为空");
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
stringBuilder.AppendLine($"{unityObject.name}遇到了错误:{e.Message}");
|
||||
}
|
||||
}
|
||||
EditorUtility.SetDirty(this);
|
||||
Debug.Log(stringBuilder);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
public static class RandomUlong
|
||||
{
|
||||
private static readonly System.Random _random = new();
|
||||
|
||||
public static ulong NextUlong
|
||||
{
|
||||
get
|
||||
{
|
||||
var buf = new byte[8];
|
||||
_random.NextBytes(buf);
|
||||
return BitConverter.ToUInt64(buf, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user