1
This commit is contained in:
@@ -8,22 +8,6 @@ namespace BITKit.Entities
|
||||
{
|
||||
public class Entity : IEntity, IDisposable
|
||||
{
|
||||
private class EntityServiceProvider : IServiceProvider
|
||||
{
|
||||
public ServiceProvider ServiceProvider;
|
||||
public readonly List<Object> Services = new();
|
||||
public object GetService(Type serviceType)
|
||||
{
|
||||
var value = ServiceProvider.GetService(serviceType);
|
||||
if (value != null)
|
||||
{
|
||||
Services.TryAdd(value);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
public Entity()
|
||||
{
|
||||
ServiceCollection.AddSingleton<IEntity>(this);
|
||||
@@ -36,26 +20,12 @@ namespace BITKit.Entities
|
||||
public CancellationToken CancellationToken => _cancellationTokenSource.Token;
|
||||
private readonly CancellationTokenSource _cancellationTokenSource = new();
|
||||
|
||||
public IServiceProvider ServiceProvider
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_serviceProvider is not null)
|
||||
{
|
||||
return _serviceProvider;
|
||||
}
|
||||
|
||||
var value = new EntityServiceProvider()
|
||||
{
|
||||
ServiceProvider = ServiceCollection.BuildServiceProvider()
|
||||
};
|
||||
_serviceProvider = value;
|
||||
return _serviceProvider;
|
||||
}
|
||||
}
|
||||
public IServiceProvider ServiceProvider => _serviceProvider ??= ServiceCollection.BuildServiceProvider();
|
||||
private ServiceProvider _serviceProvider;
|
||||
public IServiceCollection ServiceCollection { get; } = new ServiceCollection();
|
||||
private EntityServiceProvider _serviceProvider;
|
||||
public object[] GetServices()=> _serviceProvider.Services.ToArray();
|
||||
|
||||
public object[] GetServices() => ServiceCollection.ToArray()
|
||||
.Select(x => _serviceProvider.GetService(x.ServiceType)).ToArray();
|
||||
public void Inject(object obj)
|
||||
{
|
||||
foreach (var fieldInfo in obj.GetType().GetFields(ReflectionHelper.Flags))
|
||||
@@ -70,11 +40,7 @@ namespace BITKit.Entities
|
||||
public void Dispose()
|
||||
{
|
||||
_cancellationTokenSource.Cancel();
|
||||
_serviceProvider.ServiceProvider.Dispose();
|
||||
foreach (var x in GetServices().OfType<IDisposable>())
|
||||
{
|
||||
x.Dispose();
|
||||
}
|
||||
_serviceProvider.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
@@ -39,5 +39,29 @@ namespace BITKit
|
||||
{
|
||||
return self?.GetInvocationList().Cast<Func<T0, T1, T2, T3>>();
|
||||
}
|
||||
|
||||
public static IEnumerable<Func<T0, T1, T2, T3, T4>> CastAsFunc<T0, T1, T2, T3, T4>(
|
||||
this Func<T0, T1, T2, T3, T4> self)
|
||||
{
|
||||
return self?.GetInvocationList().Cast<Func<T0, T1, T2, T3, T4>>();
|
||||
}
|
||||
|
||||
public static IEnumerable<Func<T0, T1, T2, T3, T4, T5>> CastAsFunc<T0, T1, T2, T3, T4, T5>(
|
||||
this Func<T0, T1, T2, T3, T4, T5> self)
|
||||
{
|
||||
return self?.GetInvocationList().Cast<Func<T0, T1, T2, T3, T4, T5>>();
|
||||
}
|
||||
|
||||
public static IEnumerable<Func<T0, T1, T2, T3, T4, T5, T6>> CastAsFunc<T0, T1, T2, T3, T4, T5, T6>(
|
||||
this Func<T0, T1, T2, T3, T4, T5, T6> self)
|
||||
{
|
||||
return self?.GetInvocationList().Cast<Func<T0, T1, T2, T3, T4, T5, T6>>();
|
||||
}
|
||||
|
||||
public static IEnumerable<Func<T0, T1, T2, T3, T4, T5, T6, T7>> CastAsFunc<T0, T1, T2, T3, T4, T5, T6, T7>(
|
||||
this Func<T0, T1, T2, T3, T4, T5, T6, T7> self)
|
||||
{
|
||||
return self?.GetInvocationList().Cast<Func<T0, T1, T2, T3, T4, T5, T6, T7>>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -167,6 +167,13 @@ namespace BITKit
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static IEnumerable<T> RemoveIn<T>(this IEnumerable<T> self, T t)
|
||||
{
|
||||
var list = self.ToList();
|
||||
list.Remove(t);
|
||||
return list.ToArray();
|
||||
}
|
||||
public static bool TryRemove<TKey, TValue>(this IDictionary<TKey, TValue> self, TKey t)
|
||||
{
|
||||
if (self.ContainsKey(t))
|
||||
|
@@ -1,8 +0,0 @@
|
||||
namespace BITKit.IO
|
||||
{
|
||||
public interface IAsset
|
||||
{
|
||||
string Name { get; set; }
|
||||
byte[] Buffer{ get; set; }
|
||||
}
|
||||
}
|
@@ -94,6 +94,11 @@ namespace BITKit
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
int Value => 10;
|
||||
/// <summary>
|
||||
/// 创建运行时物品
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
IRuntimeItem CreateRuntimeItem();
|
||||
}
|
||||
|
||||
public interface IRuntimeItem : ICloneable
|
||||
@@ -101,7 +106,7 @@ namespace BITKit
|
||||
/// <summary>
|
||||
/// 运行时Id
|
||||
/// </summary>
|
||||
public int Id { get; }
|
||||
public int Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 配置Id
|
||||
@@ -111,7 +116,7 @@ namespace BITKit
|
||||
/// <summary>
|
||||
/// 数量
|
||||
/// </summary>
|
||||
public int Amount { get; }
|
||||
public int Amount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 运行时属性
|
||||
@@ -133,14 +138,23 @@ namespace BITKit
|
||||
/// 被托管的物品
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public record RuntimeItem : IRuntimeItem
|
||||
public struct RuntimeItem : IRuntimeItem
|
||||
{
|
||||
public int Id { get; set; } = new Random().Next();
|
||||
public static RuntimeItem Create()
|
||||
{
|
||||
var item = new RuntimeItem()
|
||||
{
|
||||
Id = new Random().Next(),
|
||||
RuntimeProperties =
|
||||
new Dictionary<Type, IScriptableItemProperty>()
|
||||
};
|
||||
return item;
|
||||
}
|
||||
public int Id { get; set; }
|
||||
public int ScriptableId { get; set; }
|
||||
public int Amount { get; set; }
|
||||
|
||||
public IDictionary<Type, IScriptableItemProperty> RuntimeProperties { get; set; } =
|
||||
new Dictionary<Type, IScriptableItemProperty>();
|
||||
public IDictionary<Type, IScriptableItemProperty> RuntimeProperties { get; set; }
|
||||
|
||||
public event Action<IRuntimeItem> OnRuntimePropertiesChanged;
|
||||
|
||||
|
@@ -1,8 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Microsoft.SqlServer.Server;
|
||||
|
||||
namespace BITKit
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 支持二进制化的的物品容器,适用于网络化物品容器
|
||||
/// 支持属性
|
||||
@@ -10,6 +15,7 @@ namespace BITKit
|
||||
/// </summary>
|
||||
public interface IRuntimeItemContainer:IBinarySerialize
|
||||
{
|
||||
public ValidHandle IsBusy { get; }
|
||||
/// <summary>
|
||||
/// 物品容器的唯一Id
|
||||
/// </summary>
|
||||
@@ -27,6 +33,11 @@ namespace BITKit
|
||||
/// </summary>
|
||||
bool Remove(int id);
|
||||
/// <summary>
|
||||
/// 设置物品
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
void Set(IRuntimeItem id);
|
||||
/// <summary>
|
||||
/// 通过通过Id丢下物品
|
||||
/// </summary>
|
||||
bool Drop(int id);
|
||||
@@ -67,4 +78,98 @@ namespace BITKit
|
||||
/// </summary>
|
||||
event Action<bool> OnRelease;
|
||||
}
|
||||
|
||||
public class RuntimeItemContainer : IRuntimeItemContainer
|
||||
{
|
||||
public RuntimeItemContainer()
|
||||
{
|
||||
IsBusy.AddListener(x=>OnRelease?.Invoke(!x));
|
||||
}
|
||||
public readonly ConcurrentDictionary<int, IRuntimeItem> Items = new();
|
||||
public void Read(BinaryReader r)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Write(BinaryWriter w)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ValidHandle IsBusy { get; } = new();
|
||||
public int Id { get; set; }
|
||||
public IRuntimeItem[] GetItems()=>Items.Values.ToArray();
|
||||
|
||||
public bool Add(IRuntimeItem item)
|
||||
{
|
||||
using var _ = IsBusy.GetHandle();
|
||||
foreach (var func in AddFactory.CastAsFunc())
|
||||
{
|
||||
if (func.Invoke(item) is false)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Items.Set(item.Id, item);
|
||||
|
||||
OnAdd?.Invoke(item);
|
||||
//BIT4Log.Log<RuntimeItemContainer>($"添加了了:{item.Id}");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Remove(int id)
|
||||
{
|
||||
using var _ = IsBusy.GetHandle();
|
||||
if (Items.TryGetValue(id, out var item) is false) return false;
|
||||
|
||||
foreach (var func in RemoveFactory.CastAsFunc())
|
||||
{
|
||||
if (func.Invoke(item) is false)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Items.TryRemove(item.Id);
|
||||
OnRemove?.Invoke(item);
|
||||
//BIT4Log.Log<RuntimeItemContainer>($"移除了:{id}");
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Set(IRuntimeItem id)
|
||||
{
|
||||
using var _ = IsBusy.GetHandle();
|
||||
if (!Items.TryGetValue(id.Id, out var item)) return;
|
||||
item = id;
|
||||
OnSet?.Invoke(item);
|
||||
}
|
||||
|
||||
public bool Drop(int id)
|
||||
{
|
||||
using var _ = IsBusy.GetHandle();
|
||||
if (Items.TryGetValue(id, out var item) is false) return false;
|
||||
foreach (var func in DropFactory.CastAsFunc())
|
||||
{
|
||||
if (func.Invoke(item) is false)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Items.TryRemove(item.Id);
|
||||
OnDrop?.Invoke(item);
|
||||
//BIT4Log.Log<RuntimeItemContainer>($"丢下了:{id}");
|
||||
return true;
|
||||
}
|
||||
|
||||
public event Func<IRuntimeItem, bool> AddFactory;
|
||||
public event Func<IRuntimeItem, bool> RemoveFactory;
|
||||
public event Func<IRuntimeItem, bool> DropFactory;
|
||||
public event Action<IRuntimeItem> OnAdd;
|
||||
public event Action<IRuntimeItem> OnRemove;
|
||||
public event Action<IRuntimeItem> OnSet;
|
||||
public event Action<IRuntimeItem> OnDrop;
|
||||
public event Action<IRuntimeItemContainer> OnRebuild;
|
||||
public event Action<bool> OnRelease;
|
||||
}
|
||||
}
|
13
Assets/BITKit/Core/Item/ItemEx.cs
Normal file
13
Assets/BITKit/Core/Item/ItemEx.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace BITKit
|
||||
{
|
||||
/// <summary>
|
||||
/// 玩家向背包添加物品,但是被其他逻辑处理
|
||||
/// </summary>
|
||||
public sealed class PlayerInventoryAddItemOverridden : InGameException
|
||||
{
|
||||
|
||||
}
|
||||
}
|
32
Assets/BITKit/Core/Pool/IPoolService.cs
Normal file
32
Assets/BITKit/Core/Pool/IPoolService.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
|
||||
namespace BITKit.Pool
|
||||
{
|
||||
/// <summary>
|
||||
/// 对象池服务
|
||||
/// </summary>
|
||||
public interface IPoolService
|
||||
{
|
||||
/// <summary>
|
||||
/// 生成对象
|
||||
/// </summary>
|
||||
/// <param name="path">可寻址路径</param>
|
||||
/// <typeparam name="T">类型</typeparam>
|
||||
/// <returns></returns>
|
||||
UniTask<T> Spawn<T>(string path) where T : class;
|
||||
/// <summary>
|
||||
/// 回收对象
|
||||
/// </summary>
|
||||
/// <param name="obj">对象实例</param>
|
||||
/// <param name="path">可寻址路径</param>
|
||||
/// <typeparam name="T">类型</typeparam>
|
||||
void Despawn<T>(T obj,string path) where T : class;
|
||||
/// <summary>
|
||||
/// 初始化,在此提前生成所有对象
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
UniTask InitializeAsync();
|
||||
}
|
||||
}
|
176
Assets/BITKit/Core/StateMachine/AsyncStateMachine.cs
Normal file
176
Assets/BITKit/Core/StateMachine/AsyncStateMachine.cs
Normal file
@@ -0,0 +1,176 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
namespace BITKit.StateMachine
|
||||
{
|
||||
/// <summary>
|
||||
/// 动态异步状态机
|
||||
/// </summary>
|
||||
/// <typeparam name="T">异步状态</typeparam>
|
||||
public class AsyncStateMachine<T>:IStateMachine<T> ,IDisposable where T : class, IStateAsync
|
||||
{
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
public AsyncStateMachine(IServiceProvider serviceProvider)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
}
|
||||
public AsyncStateMachine()
|
||||
{
|
||||
_serviceProvider = new ServiceCollection().BuildServiceProvider();
|
||||
}
|
||||
|
||||
public bool Enabled { get; set; } = true;
|
||||
public T CurrentState { get;private set; }
|
||||
public T NextOrCurrentState => _nextTargetState.IfNotAllow(CurrentState);
|
||||
private T _nextState;
|
||||
public event Action<T, T> OnStateChanging;
|
||||
public event Action<T, T> OnStateChanged;
|
||||
public event Action<T> OnStateRegistered;
|
||||
public IReadOnlyDictionary<int, T> Dictionary => _dictionary;
|
||||
public event Action<T> OnStateUnRegistered;
|
||||
public IDictionary<Type, T> StateDictionary { get; } = new Dictionary<Type, T>();
|
||||
public readonly ValidHandle IsBusy=new();
|
||||
private readonly CancellationTokenSource _cancellationTokenSource=new();
|
||||
private readonly Dictionary<int, T> _dictionary = new();
|
||||
private readonly Optional<T> _nextTargetState = new();
|
||||
public async void Initialize()
|
||||
{
|
||||
await IsBusy;
|
||||
if(_cancellationTokenSource.IsCancellationRequested)return;
|
||||
using var _ = IsBusy.GetHandle();
|
||||
foreach (var (_,value) in StateDictionary)
|
||||
{
|
||||
await value.InitializeAsync();
|
||||
value.Initialize();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async void UpdateState(float deltaTime)
|
||||
{
|
||||
if (CurrentState is null) return;
|
||||
using var _ = IsBusy.GetHandle();
|
||||
await CurrentState.OnStateUpdateAsync(deltaTime);
|
||||
CurrentState.OnStateUpdate(deltaTime);
|
||||
}
|
||||
|
||||
|
||||
public async void DisposeState()
|
||||
{
|
||||
await IsBusy;
|
||||
if(_cancellationTokenSource.IsCancellationRequested)return;
|
||||
if (CurrentState is null) return;
|
||||
using var _ = IsBusy.GetHandle();
|
||||
await CurrentState.OnStateExitAsync(CurrentState, null);
|
||||
if(_cancellationTokenSource.IsCancellationRequested)return;
|
||||
CurrentState.OnStateExit(CurrentState, null);
|
||||
CurrentState = null;
|
||||
}
|
||||
|
||||
public T TransitionState<TState>() where TState : T
|
||||
{
|
||||
T nextState;
|
||||
foreach (var (type, value) in Dictionary)
|
||||
{
|
||||
if ((typeof(TState) == value.GetType()) is false) continue;
|
||||
|
||||
nextState = value;
|
||||
|
||||
TransitionState(nextState);
|
||||
return nextState;
|
||||
}
|
||||
nextState = _serviceProvider.GetRequiredService<TState>();
|
||||
|
||||
return TransitionState(nextState);;
|
||||
}
|
||||
|
||||
public async UniTask TransitionStateAsync(T nextState)
|
||||
{
|
||||
if (nextState is not null)
|
||||
{
|
||||
if (nextState.Identifier == 0)
|
||||
{
|
||||
nextState.Identifier = nextState.GetHashCode();
|
||||
}
|
||||
}
|
||||
if (Equals(nextState, CurrentState)) return;
|
||||
if(_nextTargetState.Allow && Equals(_nextTargetState.Value,nextState))return;
|
||||
if (_nextTargetState.Allow)
|
||||
{
|
||||
_nextTargetState.Value = nextState;
|
||||
return;
|
||||
}
|
||||
_nextTargetState.SetValueThenAllow(nextState);
|
||||
await IsBusy;
|
||||
if(_cancellationTokenSource.IsCancellationRequested)return;
|
||||
using var _ = IsBusy.GetHandle();
|
||||
OnStateChanging?.Invoke(CurrentState,nextState);
|
||||
if (_dictionary.TryAdd(nextState.Identifier, nextState))
|
||||
{
|
||||
await nextState.InitializeAsync();
|
||||
if(_cancellationTokenSource.IsCancellationRequested)return;
|
||||
nextState.Initialize();
|
||||
}
|
||||
if (CurrentState is not null)
|
||||
{
|
||||
CurrentState.Enabled = false;
|
||||
await CurrentState.OnStateExitAsync(CurrentState, nextState);
|
||||
if(_cancellationTokenSource.IsCancellationRequested)return;
|
||||
CurrentState.OnStateExit(CurrentState,nextState);
|
||||
}
|
||||
|
||||
var tempState = CurrentState;
|
||||
CurrentState = _nextTargetState.Value;
|
||||
_nextTargetState.Clear();
|
||||
nextState.Enabled = true;
|
||||
await nextState.OnStateEntryAsync(tempState);
|
||||
if(_cancellationTokenSource.IsCancellationRequested)return;
|
||||
nextState.OnStateEntry(tempState);
|
||||
OnStateChanged?.Invoke(tempState,nextState);
|
||||
|
||||
}
|
||||
public T TransitionState(T nextState)
|
||||
{
|
||||
TransitionStateAsync(nextState).Forget();
|
||||
return nextState;
|
||||
}
|
||||
|
||||
public async void UnRegister(T newState)
|
||||
{
|
||||
if (newState is null) return;
|
||||
if (Dictionary.ContainsKey(newState.Identifier) is false) return;
|
||||
_dictionary.Remove(newState.Identifier);
|
||||
|
||||
if (Equals(CurrentState, newState))
|
||||
{
|
||||
await CurrentState.OnStateExitAsync(CurrentState, null);
|
||||
CurrentState.OnStateExit(CurrentState, null);
|
||||
if (CurrentState is IAsyncDisposable asyncDisposable)
|
||||
{
|
||||
await asyncDisposable.DisposeAsync();
|
||||
}
|
||||
if (CurrentState is IDisposable disposable)
|
||||
{
|
||||
disposable.Dispose();
|
||||
}
|
||||
CurrentState = null;
|
||||
}
|
||||
|
||||
OnStateUnRegistered?.Invoke(newState);
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
if(_isDisposed)return;
|
||||
_cancellationTokenSource.Cancel();
|
||||
_cancellationTokenSource.Dispose();
|
||||
_isDisposed = true;
|
||||
}
|
||||
|
||||
private bool _isDisposed;
|
||||
}
|
||||
|
||||
}
|
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
|
||||
namespace BITKit.StateMachine
|
||||
{
|
||||
@@ -31,10 +32,22 @@ namespace BITKit.StateMachine
|
||||
void OnStateExit(IState old, IState newState);
|
||||
}
|
||||
|
||||
public interface IStateAsync:IState
|
||||
{
|
||||
/// <summary>
|
||||
/// 识别符,用于识别多个相同状态但不同用途的状态机
|
||||
/// </summary>
|
||||
int Identifier { get; set; }
|
||||
UniTask InitializeAsync();
|
||||
UniTask OnStateEntryAsync(IState old);
|
||||
UniTask OnStateUpdateAsync(float deltaTime);
|
||||
UniTask OnStateExitAsync(IState old, IState newState);
|
||||
}
|
||||
|
||||
public interface IStateMachine<T> where T:IState
|
||||
{
|
||||
bool Enabled { get; set; }
|
||||
T CurrentState { get; set; }
|
||||
T CurrentState { get; }
|
||||
event Action<T,T> OnStateChanged;
|
||||
event Action<T> OnStateRegistered;
|
||||
event Action<T> OnStateUnRegistered;
|
||||
@@ -43,14 +56,13 @@ namespace BITKit.StateMachine
|
||||
void Initialize();
|
||||
void UpdateState(float deltaTime);
|
||||
void DisposeState();
|
||||
void TransitionState<State>() where State : T;
|
||||
void TransitionState(T state);
|
||||
T TransitionState<TState>() where TState : T;
|
||||
T TransitionState(T state);
|
||||
void Register(T newState) => throw new NotImplementedException("未实现的接口");
|
||||
void UnRegister(T newState) => throw new NotImplementedException("未实现的接口");
|
||||
void InvokeOnStateRegistered(T state){}
|
||||
void InvokeOnStateUnRegistered(T state){}
|
||||
}
|
||||
|
||||
public static class StateMachineUtils
|
||||
{
|
||||
public static void Register<T>(this IStateMachine<T> stateMachine, T newState) where T : IState
|
@@ -43,6 +43,14 @@ namespace BITKit.UX
|
||||
/// </summary>
|
||||
/// <param name="panelName">面板名称</param>
|
||||
void Entry(string panelName);
|
||||
/// <summary>
|
||||
/// 当前面板
|
||||
/// </summary>
|
||||
IUXPanel CurrentPanel { get; }
|
||||
/// <summary>
|
||||
/// 面板改变回调
|
||||
/// </summary>
|
||||
public event Action<IUXPanel, IUXPanel> OnPanelChanged;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
|
||||
namespace BITKit
|
||||
{
|
||||
@@ -22,6 +23,41 @@ namespace BITKit
|
||||
[CustomType(typeof(IValidHandle))]
|
||||
public sealed class ValidHandle: IValidHandle
|
||||
{
|
||||
public class MyHandle:IDisposable
|
||||
{
|
||||
private readonly ValidHandle _validHandle;
|
||||
private bool _isDisable = false;
|
||||
public MyHandle(ValidHandle validHandle,bool isDisable = false)
|
||||
{
|
||||
_validHandle = validHandle;
|
||||
|
||||
_isDisable = isDisable;
|
||||
if (isDisable)
|
||||
{
|
||||
_validHandle.AddDisableElements(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
_validHandle.AddElement(this);
|
||||
}
|
||||
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
if (_isDisable)
|
||||
{
|
||||
_validHandle.RemoveDisableElements(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
_validHandle.RemoveElement(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public MyHandle GetHandle() => new MyHandle(this);
|
||||
public MyHandle GetDisableHandle()=> new MyHandle(this,true);
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"Allow:{enableHandle}\nElements:{string.Join("\n",objs)}\nDisableElements:{string.Join("\n",disableObjs)}";
|
||||
@@ -51,6 +87,7 @@ namespace BITKit
|
||||
public readonly List<object> disableObjs = new List<object>();
|
||||
private bool tempEnable;
|
||||
private Action<bool> EventOnEnableChanged;
|
||||
private readonly Queue<UniTaskCompletionSource> _completionSources = new();
|
||||
|
||||
|
||||
public void AddElement(object obj)
|
||||
@@ -73,7 +110,14 @@ namespace BITKit
|
||||
{
|
||||
enableHandle = tempEnable;
|
||||
if (EventOnEnableChanged is not null)
|
||||
{
|
||||
EventOnEnableChanged.Invoke(enableHandle);
|
||||
}
|
||||
if (tempEnable) return;
|
||||
if (_completionSources.TryDequeue(out var cs))
|
||||
{
|
||||
cs.TrySetResult();
|
||||
}
|
||||
}
|
||||
}
|
||||
public void RemoveElement(object obj)
|
||||
@@ -164,7 +208,16 @@ namespace BITKit
|
||||
EventOnEnableChanged -= action;
|
||||
}
|
||||
}
|
||||
|
||||
public UniTask.Awaiter GetAwaiter()
|
||||
{
|
||||
if (Allow is false)
|
||||
{
|
||||
return UniTask.CompletedTask.GetAwaiter();
|
||||
}
|
||||
var cs = new UniTaskCompletionSource();
|
||||
_completionSources.Enqueue(cs);
|
||||
return cs.Task.GetAwaiter();
|
||||
}
|
||||
public void Clear()
|
||||
{
|
||||
objs.Clear();
|
||||
|
@@ -72,9 +72,9 @@
|
||||
"name": "Aim",
|
||||
"type": "Button",
|
||||
"id": "f521b9d3-bfbb-4ce2-85c8-15cb3fcc2534",
|
||||
"expectedControlType": "Button",
|
||||
"expectedControlType": "",
|
||||
"processors": "",
|
||||
"interactions": "Tap,Hold,Press",
|
||||
"interactions": "Press",
|
||||
"initialStateCheck": false
|
||||
},
|
||||
{
|
||||
@@ -135,7 +135,7 @@
|
||||
"name": "Primary",
|
||||
"type": "Button",
|
||||
"id": "113a3c02-0fbb-4081-9fac-c39878ddfd09",
|
||||
"expectedControlType": "Button",
|
||||
"expectedControlType": "",
|
||||
"processors": "",
|
||||
"interactions": "Press,Hold",
|
||||
"initialStateCheck": false
|
||||
@@ -144,7 +144,7 @@
|
||||
"name": "Secondary",
|
||||
"type": "Button",
|
||||
"id": "57a12f28-2fde-45e5-957f-d8d61afe44de",
|
||||
"expectedControlType": "Button",
|
||||
"expectedControlType": "",
|
||||
"processors": "",
|
||||
"interactions": "Press,Hold",
|
||||
"initialStateCheck": false
|
||||
@@ -153,7 +153,7 @@
|
||||
"name": "Tertiary",
|
||||
"type": "Button",
|
||||
"id": "c2535f43-fec8-42bd-b680-6d1cee5246e5",
|
||||
"expectedControlType": "Button",
|
||||
"expectedControlType": "",
|
||||
"processors": "",
|
||||
"interactions": "Press,Hold",
|
||||
"initialStateCheck": false
|
||||
@@ -162,7 +162,7 @@
|
||||
"name": "Quaternary",
|
||||
"type": "Button",
|
||||
"id": "6740f3a7-7571-4277-96e9-15aa08af7302",
|
||||
"expectedControlType": "Button",
|
||||
"expectedControlType": "",
|
||||
"processors": "",
|
||||
"interactions": "Press,Hold",
|
||||
"initialStateCheck": false
|
||||
@@ -171,7 +171,7 @@
|
||||
"name": "Quinary",
|
||||
"type": "Button",
|
||||
"id": "97511588-e441-4e27-953e-e60cbea80489",
|
||||
"expectedControlType": "Button",
|
||||
"expectedControlType": "",
|
||||
"processors": "",
|
||||
"interactions": "",
|
||||
"initialStateCheck": false
|
||||
@@ -180,7 +180,7 @@
|
||||
"name": "Senary",
|
||||
"type": "Button",
|
||||
"id": "46173186-2ef3-4ab9-8ff4-b94cc113df67",
|
||||
"expectedControlType": "Button",
|
||||
"expectedControlType": "",
|
||||
"processors": "",
|
||||
"interactions": "",
|
||||
"initialStateCheck": false
|
||||
|
@@ -13,25 +13,82 @@ using System.Text;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
using BITKit.Mod;
|
||||
using UnityEngine.InputSystem.Interactions;
|
||||
using Label = UnityEngine.UIElements.Label;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
// ReSharper disable PossibleMultipleEnumeration
|
||||
|
||||
namespace BITKit.Console
|
||||
{
|
||||
public class BITConsole : MonoBehaviour
|
||||
public class UXConsole:IDisposable
|
||||
{
|
||||
[RuntimeInitializeOnLoadMethod]
|
||||
private static void Reload()
|
||||
{
|
||||
Application.logMessageReceivedThreaded += EnqueueLog;
|
||||
}
|
||||
[BITCommand]
|
||||
// ReSharper disable once UnusedMember.Global
|
||||
public static void Console_Exception_Print_StackTrace(int allow)
|
||||
{
|
||||
exceptionPrintStackTrace = allow is 1;
|
||||
_exceptionPrintStackTrace = allow is 1;
|
||||
}
|
||||
private static bool exceptionPrintStackTrace = false;
|
||||
|
||||
private readonly IMainTicker _ticker;
|
||||
public UXConsole(IMainTicker ticker)
|
||||
{
|
||||
_ticker = ticker;
|
||||
_singleton = this;
|
||||
|
||||
_ticker.Add(OnTick);
|
||||
|
||||
InitializeAsync();
|
||||
|
||||
Application.logMessageReceivedThreaded += EnqueueLog;
|
||||
}
|
||||
|
||||
private async void InitializeAsync()
|
||||
{
|
||||
var go = new GameObject("UXConsole");
|
||||
Object.DontDestroyOnLoad(go);
|
||||
var document = go.AddComponent<UIDocument>();
|
||||
document.sortingOrder = 1;
|
||||
try
|
||||
{
|
||||
var panelSettings =await ModService.LoadAsset<PanelSettings>("ux_panel_settings");
|
||||
document.panelSettings = panelSettings;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
BIT4Log.Warning<UXService>("未找到ux_panel_settings");
|
||||
throw;
|
||||
}
|
||||
document.visualTreeAsset = await ModService.LoadAsset<VisualTreeAsset>("ux_console");
|
||||
|
||||
_rootVisualElement = document.rootVisualElement;
|
||||
|
||||
UXUtils.Inject(this,_rootVisualElement);
|
||||
|
||||
_commandSelector = new()
|
||||
{
|
||||
Container = _commandContainer,
|
||||
};
|
||||
_commandSelector.OnSelected += x =>
|
||||
{
|
||||
_textField.SetValueWithoutNotify(x.Name);
|
||||
_textField.Blur();
|
||||
_textField.Focus();
|
||||
};
|
||||
_singleton = this;
|
||||
|
||||
_textField.RegisterValueChangedCallback(OnTextFieldValueChanged);
|
||||
_textField.RegisterCallback<KeyDownEvent>(OnKeyDown);
|
||||
|
||||
_text.text = string.Empty;
|
||||
|
||||
Toggle(false);
|
||||
}
|
||||
|
||||
private VisualElement _rootVisualElement;
|
||||
|
||||
private static bool _exceptionPrintStackTrace;
|
||||
private class CommandSelector
|
||||
{
|
||||
public VisualElement Container { get; set; }
|
||||
@@ -100,111 +157,58 @@ namespace BITKit.Console
|
||||
public static async void Clear()
|
||||
{
|
||||
await BITApp.SwitchToMainThread();
|
||||
singleton.outputString.Clear();
|
||||
singleton.text.text = string.Empty;
|
||||
_singleton._outputString.Clear();
|
||||
_singleton._text.text = string.Empty;
|
||||
}
|
||||
private static BITConsole singleton;
|
||||
[SerializeField] private UIDocument document;
|
||||
[SerializeReference] private InputActionReference toggleAction;
|
||||
private static UXConsole _singleton;
|
||||
|
||||
private static ConcurrentQueue<(string condition, string stackTrace, LogType type)> logQueue = new();
|
||||
private static readonly ConcurrentQueue<(string condition, string stackTrace, LogType type)> LOGQueue = new();
|
||||
private static void EnqueueLog(string condition, string stackTrace, LogType type)
|
||||
{
|
||||
logQueue.Enqueue((condition, stackTrace, type));
|
||||
LOGQueue.Enqueue((condition, stackTrace, type));
|
||||
}
|
||||
|
||||
private readonly InputActionGroup _inputActionGroup=new()
|
||||
{
|
||||
allowGlobalActivation = false
|
||||
};
|
||||
public int logLineLimit = 64;
|
||||
private const int LOGLineLimit = 64;
|
||||
|
||||
[UXBindPath("commands-container")]
|
||||
private VisualElement commandContainer;
|
||||
private VisualElement _commandContainer;
|
||||
[UXBindPath("TextField")]
|
||||
private TextField textField;
|
||||
private TextField _textField;
|
||||
[UXBindPath("Text")]
|
||||
private Label text;
|
||||
private Label _text;
|
||||
[UXBindPath( "context-scrollview")]
|
||||
private ScrollView scrollView;
|
||||
private ScrollView _scrollView;
|
||||
private bool _isRunning;
|
||||
private List<string> outputString = new();
|
||||
private List<string> _outputString = new();
|
||||
|
||||
private CommandSelector _commandSelector;
|
||||
private void Start()
|
||||
{
|
||||
UXUtils.Inject(this);
|
||||
|
||||
_commandSelector = new()
|
||||
{
|
||||
Container = commandContainer,
|
||||
};
|
||||
_commandSelector.OnSelected += x =>
|
||||
{
|
||||
textField.SetValueWithoutNotify(x.Name);
|
||||
textField.Blur();
|
||||
textField.Focus();
|
||||
};
|
||||
|
||||
|
||||
singleton = this;
|
||||
|
||||
|
||||
|
||||
textField.RegisterValueChangedCallback(OnTextFieldValueChanged);
|
||||
textField.RegisterCallback<KeyDownEvent>(OnKeyDown);
|
||||
|
||||
|
||||
text.text = string.Empty;
|
||||
|
||||
|
||||
_inputActionGroup.RegisterCallback(toggleAction,Toggle);
|
||||
//_inputActionGroup.RegisterCallback(nextOrPreviousAction, OnNextCommand);
|
||||
|
||||
_inputActionGroup.allowInput.AddElement(this);
|
||||
|
||||
Toggle(false);
|
||||
|
||||
BIT4Log.OnNextLine += OnNextLine;
|
||||
|
||||
destroyCancellationToken.Register(() =>
|
||||
{
|
||||
BIT4Log.OnNextLine -= OnNextLine;
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void OnNextLine()
|
||||
{
|
||||
if (outputString.Count is not 0 && outputString.Last() != string.Empty)
|
||||
outputString.Add(string.Empty);
|
||||
if (_outputString.Count is not 0 && _outputString.Last() != string.Empty)
|
||||
_outputString.Add(string.Empty);
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
_inputActionGroup.allowInput.RemoveElement(this);
|
||||
}
|
||||
public async void Toggle(bool active)
|
||||
private void Toggle(bool active)
|
||||
{
|
||||
_commandSelector.SetMethods(null);
|
||||
|
||||
document.rootVisualElement.SetActive(active);
|
||||
_rootVisualElement.SetActive(active);
|
||||
_isRunning = active;
|
||||
|
||||
BITAppForUnity.AllowCursor.SetElements(this,active);
|
||||
BITInputSystem.AllowInput.SetDisableElements(this,active);
|
||||
|
||||
await UniTask.WaitForEndOfFrame(this);
|
||||
if (active)
|
||||
{
|
||||
textField.SetValueWithoutNotify(string.Empty);
|
||||
_textField.SetValueWithoutNotify(string.Empty);
|
||||
|
||||
textField.Focus();
|
||||
_textField.Focus();
|
||||
|
||||
scrollView.ScrollToBottom();
|
||||
_scrollView.ScrollToBottom();
|
||||
}
|
||||
else
|
||||
{
|
||||
text.Blur();
|
||||
_text.Blur();
|
||||
}
|
||||
}
|
||||
private void OnTextFieldValueChanged(ChangeEvent<string> callback)
|
||||
@@ -215,11 +219,11 @@ namespace BITKit.Console
|
||||
|
||||
_commandSelector.SetMethods(commands);
|
||||
|
||||
commandContainer.SetActive(commands.Length is not 0);
|
||||
_commandContainer.SetActive(commands.Length is not 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
commandContainer.SetActive(false);
|
||||
_commandContainer.SetActive(false);
|
||||
|
||||
_commandSelector.SetMethods(null);
|
||||
}
|
||||
@@ -230,20 +234,17 @@ namespace BITKit.Console
|
||||
switch (keyDownEvent.keyCode)
|
||||
{
|
||||
case KeyCode.Return:
|
||||
var cmd = textField.text;
|
||||
var cmd = _textField.text;
|
||||
|
||||
LogCallback($">{cmd}", string.Empty, LogType.Log);
|
||||
|
||||
textField.SetValueWithoutNotify(string.Empty);
|
||||
_textField.SetValueWithoutNotify(string.Empty);
|
||||
|
||||
await UniTask.NextFrame();
|
||||
|
||||
if (destroyCancellationToken.IsCancellationRequested) return;
|
||||
|
||||
_textField.Blur();
|
||||
|
||||
textField.Blur();
|
||||
|
||||
textField.Focus();
|
||||
_textField.Focus();
|
||||
|
||||
BITCommands.Excute(cmd);
|
||||
|
||||
@@ -252,10 +253,10 @@ namespace BITKit.Console
|
||||
break;
|
||||
case KeyCode.Tab:
|
||||
break;
|
||||
case KeyCode.DownArrow when string.IsNullOrEmpty(textField.text) is false:
|
||||
case KeyCode.DownArrow when string.IsNullOrEmpty(_textField.text) is false:
|
||||
_commandSelector.Index-=1;
|
||||
break;
|
||||
case KeyCode.UpArrow when string.IsNullOrEmpty(textField.text) is false:
|
||||
case KeyCode.UpArrow when string.IsNullOrEmpty(_textField.text) is false:
|
||||
_commandSelector.Index+=1;
|
||||
break;
|
||||
default:
|
||||
@@ -269,16 +270,6 @@ namespace BITKit.Console
|
||||
keyDownEvent.PreventDefault();
|
||||
}
|
||||
}
|
||||
private void Toggle(InputAction.CallbackContext context)
|
||||
{
|
||||
switch (context)
|
||||
{
|
||||
case { interaction: PressInteraction, performed: true }:
|
||||
Toggle(!_isRunning);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private async void LogCallback(string condition, string stackTrace, LogType type)
|
||||
{
|
||||
try
|
||||
@@ -286,33 +277,33 @@ namespace BITKit.Console
|
||||
switch (type)
|
||||
{
|
||||
case LogType.Error:
|
||||
outputString.Add($"<color=red>{condition}</color>");
|
||||
_outputString.Add($"<color=red>{condition}</color>");
|
||||
break;
|
||||
case LogType.Warning:
|
||||
outputString.Add($"<color=yellow>{condition}</color>");
|
||||
_outputString.Add($"<color=yellow>{condition}</color>");
|
||||
break;
|
||||
case LogType.Exception:
|
||||
outputString.Add($"<color=red>{condition}</color>");
|
||||
if (exceptionPrintStackTrace)
|
||||
outputString.Add($"<color=red>{stackTrace}</color>");
|
||||
_outputString.Add($"<color=red>{condition}</color>");
|
||||
if (_exceptionPrintStackTrace)
|
||||
_outputString.Add($"<color=red>{stackTrace}</color>");
|
||||
break;
|
||||
default:
|
||||
outputString.Add(condition);
|
||||
_outputString.Add(condition);
|
||||
break;
|
||||
}
|
||||
|
||||
var length = outputString.Count;
|
||||
if (length > logLineLimit)
|
||||
var length = _outputString.Count;
|
||||
if (length > LOGLineLimit)
|
||||
{
|
||||
outputString = outputString.GetRange(length - logLineLimit, logLineLimit);
|
||||
_outputString = _outputString.GetRange(length - LOGLineLimit, LOGLineLimit);
|
||||
}
|
||||
StringBuilder stringBuilder = new();
|
||||
outputString.ForEach(x => stringBuilder.AppendLine(x));
|
||||
_outputString.ForEach(x => stringBuilder.AppendLine(x));
|
||||
try
|
||||
{
|
||||
await BITApp.SwitchToMainThread();
|
||||
scrollView.ScrollToBottomAutomatic();
|
||||
text.text = stringBuilder.ToString();
|
||||
_scrollView.ScrollToBottomAutomatic();
|
||||
_text.text = stringBuilder.ToString();
|
||||
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
@@ -325,25 +316,39 @@ namespace BITKit.Console
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
private void OnTick(float delta)
|
||||
{
|
||||
while (logQueue.TryDequeue(out var log))
|
||||
if (_rootVisualElement is null) return;
|
||||
|
||||
if (Keyboard.current is { backquoteKey: { wasPressedThisFrame: true } })
|
||||
{
|
||||
Toggle(_isRunning=!_isRunning);
|
||||
return;
|
||||
}
|
||||
|
||||
if(LOGQueue.TryDequeue(out var log))
|
||||
{
|
||||
LogCallback(log.condition, log.stackTrace, log.type);
|
||||
}
|
||||
|
||||
if (_isRunning is false) return;
|
||||
|
||||
var pos = textField.worldTransform.GetPosition();
|
||||
var size = textField.layout.size;
|
||||
var pos = _textField.worldTransform.GetPosition();
|
||||
var size = _textField.layout.size;
|
||||
|
||||
commandContainer.style.left = 0;
|
||||
commandContainer.style.top = 0;
|
||||
_commandContainer.style.left = 0;
|
||||
_commandContainer.style.top = 0;
|
||||
|
||||
pos.y += size.y;
|
||||
commandContainer.transform.position = pos;
|
||||
_commandContainer.transform.position = pos;
|
||||
|
||||
commandContainer.style.width = size.x;
|
||||
_commandContainer.style.width = size.x;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_ticker.Remove(OnTick);
|
||||
Application.logMessageReceivedThreaded -= EnqueueLog;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,74 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using BITKit.StateMachine;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BITKit
|
||||
{
|
||||
public abstract class StateBasedMonoBehaviour<T> : MonoBehaviour, IStateMachine<T> where T : IState
|
||||
{
|
||||
[SerializeField] private MonoStateMachine<T> stateMachine;
|
||||
protected Transform Transform => _transform ? _transform : _transform = transform;
|
||||
private Transform _transform;
|
||||
public bool Enabled
|
||||
{
|
||||
get => stateMachine.Enabled;
|
||||
set => stateMachine.Enabled = value;
|
||||
}
|
||||
|
||||
public T CurrentState
|
||||
{
|
||||
get => stateMachine.CurrentState;
|
||||
set => stateMachine.CurrentState = value;
|
||||
}
|
||||
|
||||
public event Action<T, T> OnStateChanged
|
||||
{
|
||||
add => stateMachine.OnStateChanged += value;
|
||||
remove => stateMachine.OnStateChanged -= value;
|
||||
}
|
||||
|
||||
public event Action<T> OnStateRegistered;
|
||||
public event Action<T> OnStateUnRegistered;
|
||||
|
||||
public IDictionary<Type, T> StateDictionary => stateMachine.StateDictionary;
|
||||
|
||||
public virtual void Initialize()
|
||||
{
|
||||
stateMachine.Initialize();
|
||||
}
|
||||
|
||||
public virtual void UpdateState(float deltaTime)
|
||||
{
|
||||
stateMachine.UpdateState(deltaTime);
|
||||
}
|
||||
|
||||
public virtual void DisposeState()
|
||||
{
|
||||
stateMachine.DisposeState();
|
||||
}
|
||||
|
||||
public virtual void TransitionState<State>() where State : T
|
||||
{
|
||||
stateMachine.TransitionState<State>();
|
||||
}
|
||||
|
||||
public virtual void TransitionState(T state)
|
||||
{
|
||||
stateMachine.TransitionState(state);
|
||||
}
|
||||
|
||||
public virtual void Register(T newState) => StateMachineUtils.Register(this,newState);
|
||||
public virtual void UnRegister(T newState)=>StateMachineUtils.UnRegister(this,newState);
|
||||
void IStateMachine<T>.InvokeOnStateRegistered(T state)
|
||||
{
|
||||
OnStateRegistered?.Invoke(state);
|
||||
}
|
||||
|
||||
void IStateMachine<T>.InvokeOnStateUnRegistered(T state)
|
||||
{
|
||||
OnStateUnRegistered?.Invoke(state);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BITKit.Physics
|
||||
{
|
||||
public class UnityCollisionController : MonoBehaviour
|
||||
{
|
||||
public event Action<Collision> OnUnityCollisionEnter;
|
||||
public event Action<Collision> OnUnityCollisionStay;
|
||||
public event Action<Collision> OnUnityCollisionExit;
|
||||
|
||||
public event Action<Collider> OnUnityTriggerEnter;
|
||||
public event Action<Collider> OnUnityTriggerExit;
|
||||
public event Action<Collider> OnUnityTriggerStay;
|
||||
|
||||
private void OnCollisionEnter(Collision other)
|
||||
{
|
||||
OnUnityCollisionEnter?.Invoke(other);
|
||||
}
|
||||
|
||||
private void OnCollisionStay(Collision other)
|
||||
{
|
||||
OnUnityCollisionStay?.Invoke(other);
|
||||
}
|
||||
|
||||
private void OnCollisionExit(Collision other)
|
||||
{
|
||||
OnUnityCollisionExit?.Invoke(other);
|
||||
}
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
OnUnityTriggerEnter?.Invoke(other);
|
||||
}
|
||||
|
||||
private void OnTriggerExit(Collider other)
|
||||
{
|
||||
OnUnityTriggerExit?.Invoke(other);
|
||||
}
|
||||
|
||||
private void OnTriggerStay(Collider other)
|
||||
{
|
||||
OnUnityTriggerStay?.Invoke(other);
|
||||
}
|
||||
}
|
||||
}
|
141
Assets/BITKit/Unity/Scripts/Pool/UnityPoolService.cs
Normal file
141
Assets/BITKit/Unity/Scripts/Pool/UnityPoolService.cs
Normal file
@@ -0,0 +1,141 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using BITKit.Mod;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using UnityEngine;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace BITKit.Pool
|
||||
{
|
||||
public class UnityPoolService:IPoolService,IDisposable
|
||||
{
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
public UnityPoolService(IServiceProvider serviceProvider)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
private static readonly ConcurrentDictionary<string,List<object>> Pool = new();
|
||||
private static readonly ConcurrentDictionary<string,List<object>> UsingPool = new();
|
||||
private static readonly ConcurrentDictionary<string, Queue<object>> ReadyPool = new();
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
var hashset = new HashSet<GameObject>();
|
||||
foreach (var (_,list) in Pool)
|
||||
{
|
||||
foreach (var obj in list)
|
||||
{
|
||||
if (obj is not Component component) continue;
|
||||
if (!hashset.Add(component.gameObject)) continue;
|
||||
if (component.gameObject)
|
||||
Object.Destroy(component.gameObject);
|
||||
}
|
||||
}
|
||||
Pool.Clear();
|
||||
UsingPool.Clear();
|
||||
ReadyPool.Clear();
|
||||
}
|
||||
|
||||
public async UniTask<T> Spawn<T>(string path) where T : class
|
||||
{
|
||||
if (Pool.ContainsKey(path))
|
||||
{
|
||||
var readyQueue = ReadyPool[path];
|
||||
var usingList = UsingPool[path];
|
||||
if (readyQueue.TryDequeue(out var obj))
|
||||
{
|
||||
var value = obj as T;
|
||||
usingList.Add(value);
|
||||
return obj as T;
|
||||
}
|
||||
|
||||
if (usingList.Count>0)
|
||||
{
|
||||
obj = usingList[0];
|
||||
usingList.RemoveAt(0);
|
||||
usingList.Add(obj);
|
||||
|
||||
#if UNITY_5_3_OR_NEWER
|
||||
if (obj is GameObject gameObject)
|
||||
{
|
||||
gameObject.SetActive(false);
|
||||
gameObject.SetActive(true);
|
||||
}
|
||||
#endif
|
||||
|
||||
return obj as T;
|
||||
}
|
||||
}
|
||||
|
||||
var list = Pool.GetOrCreate(path);
|
||||
|
||||
UsingPool.GetOrCreate(path);
|
||||
ReadyPool.GetOrCreate(path);
|
||||
|
||||
#if UNITY_5_3_OR_NEWER
|
||||
if (typeof(Object).IsAssignableFrom(typeof(T)))
|
||||
{
|
||||
var asset =await ModService.LoadAsset<T>(path);
|
||||
if (asset is Object o)
|
||||
{
|
||||
var instance = Object.Instantiate(o);
|
||||
list.Add(instance);
|
||||
UsingPool.GetOrCreate(path).Add(instance);
|
||||
ReadyPool.GetOrCreate(path);
|
||||
return instance as T;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
//检查T的构造函数,如果有需要参数的构造函数,就从ServiceProvider中获取,如果没有,则直接通过System.Activator创建
|
||||
if (typeof(T).GetConstructors().Any(constructorInfo => constructorInfo.GetParameters().Length is 0))
|
||||
{
|
||||
var value = Activator.CreateInstance<T>();
|
||||
list.Add(value);
|
||||
UsingPool.GetOrCreate(path).Add(value);
|
||||
ReadyPool.GetOrCreate(path);
|
||||
return value;
|
||||
}
|
||||
{
|
||||
var value = _serviceProvider.GetRequiredService<T>();
|
||||
list.Add(value);
|
||||
UsingPool.GetOrCreate(path).Add(value);
|
||||
ReadyPool.GetOrCreate(path);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
public void Despawn<T>(T obj, string path) where T : class
|
||||
{
|
||||
if (UsingPool.TryGetValue(path, out var value))
|
||||
{
|
||||
value.Remove(obj);
|
||||
ReadyPool[path].Enqueue(obj);
|
||||
#if UNITY_5_3_OR_NEWER
|
||||
if (obj is GameObject gameObject)
|
||||
{
|
||||
gameObject.SetActive(false);
|
||||
}
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
if (Pool.ContainsKey(path)) return;
|
||||
Pool.GetOrCreate(path).Add(obj);
|
||||
ReadyPool.GetOrCreate(path).Enqueue(obj);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public UniTask InitializeAsync()
|
||||
{
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -2,11 +2,21 @@ using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using BITKit.Mod;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace BITKit
|
||||
{
|
||||
public static class ModServiceDictionaryReferenceExtensions
|
||||
{
|
||||
public static UniTask<T> LoadAssets<T>(int id) where T :class
|
||||
{
|
||||
var path = DictionaryReferenceScriptableObject.Dictionary[id];
|
||||
return ModService.LoadAsset<T>(path);
|
||||
}
|
||||
}
|
||||
public sealed class DictionaryReferenceConfigAttribute : System.Attribute
|
||||
{
|
||||
public readonly int index;
|
||||
|
@@ -2,9 +2,10 @@ using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using AYellowpaper.SerializedCollections;
|
||||
|
||||
using YooAsset;
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
|
||||
#endif
|
||||
|
||||
using UnityEngine;
|
||||
@@ -21,7 +22,15 @@ namespace BITKit
|
||||
{
|
||||
if (_singleton == null)
|
||||
{
|
||||
//_singleton = ScriptableObjectHelper.Get<DictionaryReferenceScriptableObject>();
|
||||
#if UNITY_EDITOR
|
||||
_singleton =
|
||||
AssetDatabase.LoadAssetAtPath<DictionaryReferenceScriptableObject>(
|
||||
"Assets/Artists/Configs/reference_dictionary.asset");
|
||||
#else
|
||||
var task = YooAssets.LoadAssetAsync("reference_directory");
|
||||
task.WaitForAsyncComplete();
|
||||
_singleton=task.AssetObject as DictionaryReferenceScriptableObject;
|
||||
#endif
|
||||
}
|
||||
return _singleton;
|
||||
}
|
||||
|
@@ -18,7 +18,9 @@
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"defineConstraints": [
|
||||
"deprecated"
|
||||
],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
@@ -1,29 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace BITKit.Sensors
|
||||
{
|
||||
public class SensorEditorWindow : EditorWindow
|
||||
{
|
||||
private Toggle _toggle;
|
||||
[MenuItem("Tools/Sensor/EditorWindow")]
|
||||
public static void ShowWindow()
|
||||
{
|
||||
GetWindow<SensorEditorWindow>("SensorEditorWindow");
|
||||
}
|
||||
private void CreateGUI()
|
||||
{
|
||||
_toggle = rootVisualElement.Create<Toggle>("Enable Sensor");
|
||||
_toggle.label = "Enable Sensor";
|
||||
_toggle.value = SensorGlobalSettings.Enabled;
|
||||
}
|
||||
private void Update()
|
||||
{
|
||||
SensorGlobalSettings.Enabled = _toggle.value;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,130 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
using UnityEditor.UIElements;
|
||||
#endif
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace BITKit.StateMachine
|
||||
{
|
||||
[Serializable]
|
||||
public class MonoStateMachine<TState> : IStateMachine<TState> where TState : IState
|
||||
{
|
||||
public bool Enabled
|
||||
{
|
||||
get => _enabled;
|
||||
set
|
||||
{
|
||||
_enabled = value;
|
||||
if (CurrentState is null) return;
|
||||
if (value)
|
||||
{
|
||||
CurrentState.OnStateExit(null,null);
|
||||
}
|
||||
else
|
||||
{
|
||||
CurrentState.OnStateEntry(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
public TState CurrentState { get; set; }
|
||||
public event Action<TState, TState> OnStateChanged;
|
||||
public event Action<TState> OnStateRegistered;
|
||||
public event Action<TState> OnStateUnRegistered;
|
||||
|
||||
[SerializeReference, SubclassSelector] public List<TState> states = new();
|
||||
[SerializeField,ReadOnly] private string _currentStateName;
|
||||
[SerializeField] private bool debug;
|
||||
[SerializeField] private bool transitionOnNextFrame;
|
||||
public IDictionary<Type, TState> StateDictionary { get; } = new Dictionary<Type, TState>();
|
||||
private bool _enabled = true;
|
||||
private readonly DoubleBuffer<TState> _nextState = new();
|
||||
protected bool cancelUpdateStateThisFrame;
|
||||
public void Initialize()
|
||||
{
|
||||
foreach (var state in states)
|
||||
{
|
||||
//state.TransitionState = InternalTransitionState;
|
||||
state.Initialize();
|
||||
StateDictionary.Add(state.GetType(), state);
|
||||
}
|
||||
// if (states.Count > 0)
|
||||
// {
|
||||
// TransitionState(states[0]);
|
||||
// }
|
||||
}
|
||||
public void UpdateState(float deltaTime)
|
||||
{
|
||||
if(transitionOnNextFrame && _nextState.TryGetRelease(out var nextState))
|
||||
TransitionStateInternal(nextState);
|
||||
if (Enabled)
|
||||
{
|
||||
if (cancelUpdateStateThisFrame is false)
|
||||
{
|
||||
CurrentState?.OnStateUpdate(deltaTime);
|
||||
}
|
||||
cancelUpdateStateThisFrame = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void DisposeState()
|
||||
{
|
||||
}
|
||||
public void TransitionState<State>() where State : TState
|
||||
{
|
||||
var nextState = StateDictionary.GetOrCreate(typeof(State));
|
||||
TransitionState(nextState);
|
||||
}
|
||||
public void TransitionState(TState newState)
|
||||
{
|
||||
if(transitionOnNextFrame)
|
||||
_nextState.Release(newState);
|
||||
else
|
||||
{
|
||||
TransitionStateInternal(newState);
|
||||
cancelUpdateStateThisFrame = true;
|
||||
}
|
||||
}
|
||||
private void TransitionStateInternal(TState newState)
|
||||
{
|
||||
if (Equals(newState,CurrentState)) return;
|
||||
|
||||
var oldState = CurrentState;
|
||||
if (oldState is not null)
|
||||
{
|
||||
oldState.OnStateExit(oldState, newState);
|
||||
oldState.Enabled = false;
|
||||
}
|
||||
|
||||
_currentStateName = newState is not null ? newState.GetType().Name : "null";
|
||||
if (debug)
|
||||
{
|
||||
BIT4Log.Log<MonoStateMachine<TState>>($"TransitionState from {_currentStateName} to {_currentStateName}");
|
||||
}
|
||||
CurrentState = newState;
|
||||
|
||||
if (newState is not null)
|
||||
{
|
||||
newState.Enabled = true;
|
||||
newState.OnStateEntry(oldState);
|
||||
}
|
||||
OnStateChanged?.Invoke(oldState, newState);
|
||||
}
|
||||
public virtual void Register(TState newState)=>StateMachineUtils.Register(this,newState);
|
||||
public virtual void UnRegister(TState newState)=>StateMachineUtils.UnRegister(this,newState);
|
||||
public void InvokeOnStateRegistered(TState state)
|
||||
{
|
||||
OnStateRegistered?.Invoke(state);
|
||||
}
|
||||
|
||||
public void InvokeOnStateUnRegistered(TState state)
|
||||
{
|
||||
OnStateUnRegistered?.Invoke(state);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -1,12 +1,12 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.Remoting.Contexts;
|
||||
using System.ComponentModel.Design;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using UnityEngine.InputSystem;
|
||||
using BITKit;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using BITKit.Mod;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace BITKit.UX.Internal
|
||||
{
|
||||
public class ContextMenu : IContextMenu
|
||||
@@ -20,16 +20,16 @@ namespace BITKit.UX.Internal
|
||||
{
|
||||
public Label(string text)
|
||||
{
|
||||
this.text = text;
|
||||
_text = text;
|
||||
}
|
||||
string text;
|
||||
private readonly string _text;
|
||||
public override VisualElement GetVisualElement()
|
||||
{
|
||||
UnityEngine.UIElements.Label label = new(text);
|
||||
UnityEngine.UIElements.Label label = new(_text);
|
||||
return label;
|
||||
}
|
||||
}
|
||||
[System.Serializable]
|
||||
[Serializable]
|
||||
public class TestContextMenu
|
||||
{
|
||||
public void Execute()
|
||||
@@ -45,19 +45,20 @@ namespace BITKit.UX.Internal
|
||||
{
|
||||
public Button(string text, params Action[] actions)
|
||||
{
|
||||
this.text = text;
|
||||
_text = text;
|
||||
foreach (var x in actions)
|
||||
{
|
||||
action += x;
|
||||
_action += x;
|
||||
}
|
||||
}
|
||||
string text;
|
||||
Action action;
|
||||
|
||||
private readonly string _text;
|
||||
private readonly Action _action;
|
||||
public override VisualElement GetVisualElement()
|
||||
{
|
||||
UnityEngine.UIElements.Button button = new();
|
||||
button.clicked += action;
|
||||
button.text = text;
|
||||
button.clicked += _action;
|
||||
button.text = _text;
|
||||
return button;
|
||||
}
|
||||
}
|
||||
@@ -77,14 +78,14 @@ namespace BITKit.UX
|
||||
}
|
||||
public static ContextMenuBuilder BuildAction(this ContextMenuBuilder self, string text, Action action)
|
||||
{
|
||||
self.Add(new Internal.Button(text, action, self.Excute));
|
||||
self.Add(new Internal.Button(text, action, self.Execute));
|
||||
return self;
|
||||
}
|
||||
}
|
||||
public class ContextMenuBuilder
|
||||
{
|
||||
readonly List<IContextMenu> contexts = new();
|
||||
public event Action OnExcuted;
|
||||
private readonly List<IContextMenu> _contexts = new();
|
||||
public event Action OnExecuted;
|
||||
private ContextMenuBuilder()
|
||||
{
|
||||
|
||||
@@ -97,53 +98,89 @@ namespace BITKit.UX
|
||||
{
|
||||
UXContextMenu.Singleton.Create(this);
|
||||
}
|
||||
internal void Excute()
|
||||
internal void Execute()
|
||||
{
|
||||
OnExcuted?.Invoke();
|
||||
OnExecuted?.Invoke();
|
||||
}
|
||||
public void Add(IContextMenu x) => contexts.Add(x);
|
||||
public IEnumerable<IContextMenu> GetContextMenus() => contexts.ToArray();
|
||||
public void Add(IContextMenu x) => _contexts.Add(x);
|
||||
public IEnumerable<IContextMenu> GetContextMenus() => _contexts.ToArray();
|
||||
}
|
||||
public class UXContextMenu : MonoBehaviour
|
||||
public class UXContextMenu:IDisposable
|
||||
{
|
||||
internal static UXContextMenu Singleton;
|
||||
[SerializeField] private UIDocument document;
|
||||
private VisualElement root;
|
||||
private VisualElement container;
|
||||
private void Awake()
|
||||
private readonly IUXService _uxService;
|
||||
private VisualElement _root;
|
||||
private VisualElement _container;
|
||||
private bool _isInitialized = false;
|
||||
public UXContextMenu(IUXService uxService)
|
||||
{
|
||||
_uxService = uxService;
|
||||
Singleton = this;
|
||||
root = document.rootVisualElement;
|
||||
container = document.rootVisualElement[1];
|
||||
root.Q("background-image").RegisterCallback<MouseDownEvent>(x =>
|
||||
uxService.OnPanelChanged += OnPanelChanged;
|
||||
InitializeAsync();
|
||||
}
|
||||
|
||||
private async void InitializeAsync()
|
||||
{
|
||||
var go = new GameObject(nameof(UXContextMenu));
|
||||
Object.DontDestroyOnLoad(go);
|
||||
var document = go.AddComponent<UIDocument>();
|
||||
document.sortingOrder = 1;
|
||||
try
|
||||
{
|
||||
var panelSettings =await ModService.LoadAsset<PanelSettings>("ux_panel_settings");
|
||||
document.panelSettings = panelSettings;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
BIT4Log.Warning<UXService>("未找到ux_panel_settings");
|
||||
throw;
|
||||
}
|
||||
document.visualTreeAsset = await ModService.LoadAsset<VisualTreeAsset>("ux_context_menu");
|
||||
|
||||
_root = document.rootVisualElement;
|
||||
_container = _root.Q<VisualElement>("menu-container");
|
||||
_root.Q("background-image").RegisterCallback<MouseDownEvent>(_ =>
|
||||
{
|
||||
Close();
|
||||
});
|
||||
_isInitialized = true;
|
||||
Close();
|
||||
}
|
||||
public void Create(ContextMenuBuilder builder)
|
||||
{
|
||||
var pos = Mouse.current.position.ReadValue();
|
||||
pos.y = Screen.height - pos.y;
|
||||
pos = RuntimePanelUtils.ScreenToPanel(root.panel, pos);
|
||||
pos = RuntimePanelUtils.ScreenToPanel(_root.panel, pos);
|
||||
|
||||
container.style.position = Position.Absolute;
|
||||
container.style.left = pos.x;
|
||||
container.style.top = pos.y;
|
||||
container.Clear();
|
||||
_container.style.position = Position.Absolute;
|
||||
_container.style.left = pos.x;
|
||||
_container.style.top = pos.y;
|
||||
_container.Clear();
|
||||
|
||||
root.SetActive(true);
|
||||
_root.SetActive(true);
|
||||
|
||||
foreach (var context in builder.GetContextMenus())
|
||||
{
|
||||
container.Add(context.GetVisualElement());
|
||||
_container.Add(context.GetVisualElement());
|
||||
}
|
||||
builder.OnExcuted += Close;
|
||||
builder.OnExecuted += Close;
|
||||
}
|
||||
void Close()
|
||||
private void Close()
|
||||
{
|
||||
container.Clear();
|
||||
root.SetActive(false);
|
||||
if(_isInitialized is false)return;
|
||||
_container.Clear();
|
||||
_root.SetActive(false);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_uxService.OnPanelChanged -= OnPanelChanged;
|
||||
}
|
||||
|
||||
private void OnPanelChanged(IUXPanel arg1, IUXPanel arg2)
|
||||
{
|
||||
Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -28,10 +28,55 @@ namespace BITKit.UX
|
||||
protected abstract string DocumentPath { get; }
|
||||
public VisualElement RootVisualElement { get; set; }
|
||||
protected VisualTreeAsset VisualTreeAsset { get; private set; }
|
||||
private readonly ValidHandle _isBusy = new();
|
||||
protected UIToolKitPanel(IUXService uxService)
|
||||
{
|
||||
UXService = uxService;
|
||||
uxService.Register(this);
|
||||
InitializeAsync().Forget();
|
||||
}
|
||||
|
||||
private async UniTask InitializeAsync()
|
||||
{
|
||||
await _isBusy;
|
||||
using var b = _isBusy.GetHandle();
|
||||
if (RootVisualElement is null)
|
||||
{
|
||||
VisualTreeAsset = await ModService.LoadAsset<VisualTreeAsset>(DocumentPath);
|
||||
|
||||
RootVisualElement = UXService.Root.As<VisualElement>().Create(VisualTreeAsset);
|
||||
RootVisualElement.pickingMode = PickingMode.Ignore;
|
||||
RootVisualElement.style.position = Position.Absolute;
|
||||
RootVisualElement.style.left = 0;
|
||||
RootVisualElement.style.right = 0;
|
||||
RootVisualElement.style.top = 0;
|
||||
RootVisualElement.style.bottom = 0;
|
||||
|
||||
var invisible = RootVisualElement.Create<VisualElement>();
|
||||
invisible.name = "invisible_return_generate";
|
||||
invisible.style.position = Position.Absolute;
|
||||
invisible.pickingMode = PickingMode.Ignore;
|
||||
invisible.style.left = 0;
|
||||
invisible.style.right = 0;
|
||||
invisible.style.top = 0;
|
||||
invisible.style.bottom = 0;
|
||||
invisible.SendToBack();
|
||||
|
||||
if (CloseWhenClickOutside)
|
||||
{
|
||||
invisible.RegisterCallback<MouseDownEvent>(x => { OnReturn(); });
|
||||
invisible.pickingMode = PickingMode.Position;
|
||||
}
|
||||
|
||||
if (IsWindow)
|
||||
{
|
||||
invisible.style.backgroundColor = new Color(0, 0, 0, 0.9f);
|
||||
}
|
||||
|
||||
UXUtils.Inject(this);
|
||||
|
||||
RootVisualElement.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual Optional<float> EntryDuration { get; }= new();
|
||||
@@ -62,42 +107,8 @@ namespace BITKit.UX
|
||||
OnEntry?.Invoke();
|
||||
}
|
||||
async UniTask IEntryElement.EntryAsync()
|
||||
{
|
||||
if (RootVisualElement is null)
|
||||
{
|
||||
VisualTreeAsset = await ModService.LoadAsset<VisualTreeAsset>(DocumentPath);
|
||||
|
||||
RootVisualElement = UXService.Root.As<VisualElement>().Create(VisualTreeAsset);
|
||||
RootVisualElement.pickingMode = PickingMode.Ignore;
|
||||
RootVisualElement.style.position = Position.Absolute;
|
||||
RootVisualElement.style.left = 0;
|
||||
RootVisualElement.style.right = 0;
|
||||
RootVisualElement.style.top = 0;
|
||||
RootVisualElement.style.bottom = 0;
|
||||
|
||||
var invisible = RootVisualElement.Create<VisualElement>();
|
||||
invisible.name = "invisible_return_generate";
|
||||
invisible.style.position = Position.Absolute;
|
||||
invisible.pickingMode = PickingMode.Ignore;
|
||||
invisible.style.left = 0;
|
||||
invisible.style.right = 0;
|
||||
invisible.style.top = 0;
|
||||
invisible.style.bottom = 0;
|
||||
invisible.SendToBack();
|
||||
|
||||
if (CloseWhenClickOutside)
|
||||
{
|
||||
invisible.RegisterCallback<MouseDownEvent>(x => { OnReturn(); });
|
||||
invisible.pickingMode = PickingMode.Position;
|
||||
}
|
||||
|
||||
if (IsWindow)
|
||||
{
|
||||
invisible.style.backgroundColor = new Color(0, 0, 0, 0.9f);
|
||||
}
|
||||
|
||||
UXUtils.Inject(this);
|
||||
}
|
||||
{
|
||||
await InitializeAsync();
|
||||
|
||||
RootVisualElement.SetActive(true);
|
||||
RootVisualElement.AddToClassList(USSEntry);
|
||||
|
@@ -103,7 +103,9 @@ namespace BITKit.UX
|
||||
|
||||
public void Entry(IUXPanel panel) => _entryQueue.Push(panel);
|
||||
public void Entry(string panelName) => _entryQueueByName.TryAdd(panelName);
|
||||
|
||||
public IUXPanel CurrentPanel => _currentPanel;
|
||||
public event Action<IUXPanel, IUXPanel> OnPanelChanged;
|
||||
|
||||
public void Return()
|
||||
{
|
||||
if(_windowEntryGroup.TryGetEntried(out _))
|
||||
@@ -124,6 +126,7 @@ namespace BITKit.UX
|
||||
|
||||
private void OnEntry(IUXPanel obj)
|
||||
{
|
||||
OnPanelChanged?.Invoke(_currentPanel,obj);
|
||||
_currentPanel = obj;
|
||||
}
|
||||
private void OnTick(float delta)
|
||||
|
@@ -2,15 +2,15 @@
|
||||
<Style src="project://database/Assets/BITKit/Unity/UX/Common/Common.uss?fileID=7433441132597879392&guid=a3a69d3518fd02b489e721f3c5b0b539&type=3#Common" />
|
||||
<Style src="project://database/Assets/BITKit/Unity/UX/BITConsole.uss?fileID=7433441132597879392&guid=9175bfb75cd47b9488b9f12bd34e50fd&type=3#BITConsole" />
|
||||
<ui:VisualElement name="Root" style="position: absolute; left: 0; top: 0; right: 0; bottom: 0;">
|
||||
<ui:VisualElement name="Console" class="Console" style="width: 1024px; height: 768px; border-top-left-radius: 16px; border-bottom-left-radius: 16px; border-top-right-radius: 16px; border-bottom-right-radius: 16px; padding-left: 16px; padding-right: 16px; padding-top: 8px; padding-bottom: 8px; margin-left: auto; margin-right: auto; margin-top: auto; margin-bottom: auto;">
|
||||
<ui:Label text="Console" display-tooltip-when-elided="true" class="Console-Text" />
|
||||
<ui:ScrollView name="context-scrollview" class="Console-ScrollView" style="height: auto; flex-grow: 1; display: flex;">
|
||||
<ui:Label text="This is Console This is <size=24>Rich Text</size> This is <color=yellow>Warning</color> This is <color=red>Error</color> This is loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong text This is Console This is <size=24>Rich Text</size> This is <color=yellow>Warning</color> This is <color=red>Error</color> This is loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong text <color=#add8e6ff><b>TypeName</b></color>:MyMessage" display-tooltip-when-elided="true" name="Text" enable-rich-text="true" parse-escape-sequences="false" class="Console-Text" style="padding-left: 8px; padding-right: 8px; padding-top: 4px; padding-bottom: 4px; white-space: normal;" />
|
||||
<ui:VisualElement name="console" class="console" style="width: 1024px; height: 768px; border-top-left-radius: 16px; border-bottom-left-radius: 16px; border-top-right-radius: 16px; border-bottom-right-radius: 16px; padding-left: 16px; padding-right: 16px; padding-top: 8px; padding-bottom: 8px; margin-left: auto; margin-right: auto; margin-top: auto; margin-bottom: auto;">
|
||||
<ui:Label text="console" display-tooltip-when-elided="true" class="console-Text" />
|
||||
<ui:ScrollView name="context-scrollview" class="console-container" style="height: auto; flex-grow: 1; display: flex;">
|
||||
<ui:Label text="This is console This is <size=24>Rich Text</size> This is <color=yellow>Warning</color> This is <color=red>Error</color> This is loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong text This is console This is <size=24>Rich Text</size> This is <color=yellow>Warning</color> This is <color=red>Error</color> This is loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong text <color=#add8e6ff><b>TypeName</b></color>:MyMessage" display-tooltip-when-elided="true" name="Text" enable-rich-text="true" parse-escape-sequences="false" class="console-Text" style="padding-left: 8px; padding-right: 8px; padding-top: 4px; padding-bottom: 4px; white-space: normal;" />
|
||||
</ui:ScrollView>
|
||||
<ui:TextField picking-mode="Ignore" value="filler text" text="filler text" name="TextField" class="Console-TextField" style="height: 48px;" />
|
||||
<ui:TextField picking-mode="Ignore" value="filler text" text="filler text" name="TextField" class="console-TextField" style="height: 48px; margin-top: 8px;" />
|
||||
</ui:VisualElement>
|
||||
</ui:VisualElement>
|
||||
<ui:VisualElement name="commands-container" picking-mode="Ignore" style="position: absolute; top: 830px; left: 120px; align-items: stretch; min-width: 512px;">
|
||||
<ui:VisualElement name="commands-container" picking-mode="Ignore" class="console" style="position: absolute; top: 830px; left: 120px; align-items: stretch; min-width: 512px;">
|
||||
<ui:Button text="Button" parse-escape-sequences="true" display-tooltip-when-elided="true" />
|
||||
<ui:Button text="Button" parse-escape-sequences="true" display-tooltip-when-elided="true" />
|
||||
<ui:Button text="Button" parse-escape-sequences="true" display-tooltip-when-elided="true" />
|
||||
|
@@ -31,3 +31,4 @@
|
||||
scale: 1.2 1.2;
|
||||
opacity: 0.5;
|
||||
}
|
||||
|
||||
|
@@ -1,19 +1,16 @@
|
||||
Button {
|
||||
background-color: rgba(0, 0, 0, 0);
|
||||
color: rgba(255, 255, 255, 0.5);
|
||||
-unity-font-definition: url('project://database/Assets/Artist/Art/Fonts/NotoSansSC-Bold%20SDF.asset?fileID=11400000&guid=033cc1a4c6c35c6488ad74f4cee476ac&type=2#NotoSansSC-Bold SDF');
|
||||
border-left-width: 0;
|
||||
border-right-width: 0;
|
||||
border-top-width: 0;
|
||||
border-bottom-width: 0;
|
||||
-unity-text-align: middle-left;
|
||||
}
|
||||
|
||||
Button:hover {
|
||||
background-color: rgba(64, 64, 64, 255);
|
||||
background-color: rgb(64, 64, 64);
|
||||
}
|
||||
|
||||
Button:active {
|
||||
background-color: rgba(125, 125, 125, 255);
|
||||
color: rgba(29, 29, 29, 255);
|
||||
background-color: rgb(125, 125, 125);
|
||||
color: rgb(29, 29, 29);
|
||||
}
|
||||
|
@@ -0,0 +1,56 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using MonKey;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BITKit.GameEditor
|
||||
{
|
||||
public class QuickFixBoxCollider
|
||||
{
|
||||
[Command(nameof(FixCollider), "快速修复BoxCollider编写", QuickName = "fb"),
|
||||
MenuItem("Tools/Scenes/Fix Float Model Position")]
|
||||
public static void FixCollider()
|
||||
{
|
||||
foreach (var transform in UnityEditor.Selection.transforms)
|
||||
{
|
||||
// 获取或创建 BoxCollider 组件
|
||||
var boxCollider = transform.GetComponent<BoxCollider>();
|
||||
if (boxCollider == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// 获取所有可见的 MeshRenderer 组件
|
||||
var meshRenderers = transform.GetComponentsInChildren<MeshRenderer>();
|
||||
|
||||
if (meshRenderers.Length == 0)
|
||||
{
|
||||
Debug.LogWarning("No MeshRenderer components found in children.");
|
||||
return;
|
||||
}
|
||||
|
||||
// 初始化包围盒,以第一个 MeshRenderer 的包围盒为基准
|
||||
Bounds bounds = new Bounds(transform.transform.InverseTransformPoint(meshRenderers[0].bounds.center),
|
||||
transform.transform.InverseTransformVector(meshRenderers[0].bounds.size));
|
||||
|
||||
// 遍历所有 MeshRenderer,合并包围盒
|
||||
for (int i = 1; i < meshRenderers.Length; i++)
|
||||
{
|
||||
Bounds localBounds = meshRenderers[i].bounds;
|
||||
Vector3 localCenter = transform.transform.InverseTransformPoint(localBounds.center);
|
||||
Vector3 localSize = transform.transform.InverseTransformVector(localBounds.size);
|
||||
|
||||
bounds.Encapsulate(new Bounds(localCenter, localSize));
|
||||
}
|
||||
|
||||
// 设置 BoxCollider 的中心和大小
|
||||
boxCollider.center = bounds.center;
|
||||
boxCollider.size = bounds.size;
|
||||
|
||||
EditorUtility.SetDirty(boxCollider);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user