This commit is contained in:
CortexCore
2024-11-08 12:52:09 +08:00
parent 4ba741408d
commit 1650126d55
27 changed files with 851 additions and 193 deletions

View File

@@ -39,5 +39,29 @@ namespace BITKit
{ {
return self?.GetInvocationList().Cast<Func<T0, T1, T2, T3>>(); 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>>();
}
} }
} }

View File

@@ -167,6 +167,13 @@ namespace BITKit
} }
return false; 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) public static bool TryRemove<TKey, TValue>(this IDictionary<TKey, TValue> self, TKey t)
{ {
if (self.ContainsKey(t)) if (self.ContainsKey(t))

View File

@@ -1,8 +0,0 @@
namespace BITKit.IO
{
public interface IAsset
{
string Name { get; set; }
byte[] Buffer{ get; set; }
}
}

View File

@@ -94,6 +94,11 @@ namespace BITKit
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
int Value => 10; int Value => 10;
/// <summary>
/// 创建运行时物品
/// </summary>
/// <returns></returns>
IRuntimeItem CreateRuntimeItem();
} }
public interface IRuntimeItem : ICloneable public interface IRuntimeItem : ICloneable
@@ -101,7 +106,7 @@ namespace BITKit
/// <summary> /// <summary>
/// 运行时Id /// 运行时Id
/// </summary> /// </summary>
public int Id { get; } public int Id { get; set; }
/// <summary> /// <summary>
/// 配置Id /// 配置Id
@@ -133,14 +138,23 @@ namespace BITKit
/// 被托管的物品 /// 被托管的物品
/// </summary> /// </summary>
[Serializable] [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 ScriptableId { get; set; }
public int Amount { get; set; } public int Amount { get; set; }
public IDictionary<Type, IScriptableItemProperty> RuntimeProperties { get; set; } = public IDictionary<Type, IScriptableItemProperty> RuntimeProperties { get; set; }
new Dictionary<Type, IScriptableItemProperty>();
public event Action<IRuntimeItem> OnRuntimePropertiesChanged; public event Action<IRuntimeItem> OnRuntimePropertiesChanged;

View File

@@ -1,4 +1,8 @@
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.SqlServer.Server; using Microsoft.SqlServer.Server;
namespace BITKit namespace BITKit
@@ -67,4 +71,83 @@ namespace BITKit
/// </summary> /// </summary>
event Action<bool> OnRelease; event Action<bool> OnRelease;
} }
public class RuntimeItemContainer : IRuntimeItemContainer
{
private readonly ConcurrentDictionary<int, IRuntimeItem> _items = new();
public void Read(BinaryReader r)
{
throw new NotImplementedException();
}
public void Write(BinaryWriter w)
{
throw new NotImplementedException();
}
public int Id { get; set; }
public IRuntimeItem[] GetItems()=>_items.Values.ToArray();
public bool Add(IRuntimeItem item)
{
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)
{
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 bool Drop(int id)
{
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;
}
} }

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: fe7ae68fb2f193640afdc79aef634c98 guid: 787baa1105305fb4683a3196b038b1b3
folderAsset: yes folderAsset: yes
DefaultImporter: DefaultImporter:
externalObjects: {} externalObjects: {}

View 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();
}
}

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 3eb498b90aa487048a9fd9a3e9253dd8 guid: 05f75f7f468db924bb3d58fc4a5aad99
MonoImporter: MonoImporter:
externalObjects: {} externalObjects: {}
serializedVersion: 2 serializedVersion: 2

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 08ae9a3e8a64da349961e04bf98471ad
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,133 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
namespace BITKit.StateMachine
{
/// <summary>
/// 动态异步状态机
/// </summary>
/// <typeparam name="T">异步状态</typeparam>
public class AsyncStateMachine<T>:IStateMachine<T> ,IDisposable where T : IStateAsync
{
private enum AsyncState
{
None,
Initializing,
InEntering,
InExiting,
InUpdating,
}
private readonly IServiceCollection _serviceCollection;
public AsyncStateMachine(IServiceCollection serviceCollection)
{
_serviceCollection = serviceCollection;
}
public bool Enabled { get; set; }
public T CurrentState { get; set; }
private T _nextState;
public event Action<T, T> OnStateChanging;
public event Action<T, T> OnStateChanged;
public event Action<T> OnStateRegistered;
public event Action<T> OnStateUnRegistered;
public IDictionary<Type, T> StateDictionary { get; } = new Dictionary<Type, T>();
private AsyncState _currentState;
private Task _currentTask;
private readonly CancellationTokenSource _cancellationTokenSource=new();
public async void Initialize()
{
if (_currentState is not AsyncState.None)
{
throw new InvalidOperationException("状态机可能已初始化");
}
_currentState = AsyncState.Initializing;
foreach (var (type,value) in StateDictionary)
{
await value.InitializeAsync();
}
foreach (var (type,value) in StateDictionary)
{
value.Initialize();
}
_currentState = AsyncState.None;
}
public void UpdateState(float deltaTime)
{
if(_cancellationTokenSource.IsCancellationRequested)return;
switch (_currentState)
{
case AsyncState.None:
if (_nextState is not null)
{
_currentState = AsyncState.InEntering;
_currentTask = CurrentState.OnStateEntryAsync(CurrentState).AsTask();
OnStateChanging?.Invoke(CurrentState,_nextState);
return;
}
_currentState = AsyncState.InUpdating;
if (CurrentState is not null)
{
_currentTask = CurrentState.OnStateUpdateAsync(deltaTime).AsTask();
}
break;
case AsyncState.InExiting:
if (_currentTask.IsCompleted)
{
_currentState =_nextState is not null ? AsyncState.InEntering : AsyncState.None;
CurrentState.OnStateExit(CurrentState,_nextState);
if (_nextState is not null)
{
_currentTask = _nextState.OnStateEntryAsync(_nextState).AsTask();
}
}
break;
case AsyncState.InEntering:
if (_currentTask.IsCompleted)
{
_currentState = AsyncState.None;
_nextState.OnStateEntry(CurrentState);
OnStateChanged?.Invoke(CurrentState,_nextState);
CurrentState = _nextState;
_nextState = default;
}
break;
case AsyncState.InUpdating:
if (_currentTask.IsCompleted)
{
_currentTask = null;
_currentState = AsyncState.None;
}
break;
}
}
public void DisposeState()
{
}
public void TransitionState<State>() where State : T
{
throw new InvalidOperationException("动态异步状态机不支持,请使用TransitionAsync");
}
public void TransitionState(T state)
{
}
public void Dispose()
{
_cancellationTokenSource.Cancel();
_cancellationTokenSource.Dispose();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0c936e10927235d499efcbbf19c45f90
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Cysharp.Threading.Tasks;
namespace BITKit.StateMachine namespace BITKit.StateMachine
{ {
@@ -31,6 +32,18 @@ namespace BITKit.StateMachine
void OnStateExit(IState old, IState newState); 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 public interface IStateMachine<T> where T:IState
{ {
bool Enabled { get; set; } bool Enabled { get; set; }
@@ -50,7 +63,6 @@ namespace BITKit.StateMachine
void InvokeOnStateRegistered(T state){} void InvokeOnStateRegistered(T state){}
void InvokeOnStateUnRegistered(T state){} void InvokeOnStateUnRegistered(T state){}
} }
public static class StateMachineUtils public static class StateMachineUtils
{ {
public static void Register<T>(this IStateMachine<T> stateMachine, T newState) where T : IState public static void Register<T>(this IStateMachine<T> stateMachine, T newState) where T : IState

View File

@@ -43,6 +43,14 @@ namespace BITKit.UX
/// </summary> /// </summary>
/// <param name="panelName">面板名称</param> /// <param name="panelName">面板名称</param>
void Entry(string panelName); void Entry(string panelName);
/// <summary>
/// 当前面板
/// </summary>
IUXPanel CurrentPanel { get; }
/// <summary>
/// 面板改变回调
/// </summary>
public event Action<IUXPanel, IUXPanel> OnPanelChanged;
} }
} }

View File

@@ -13,25 +13,82 @@ using System.Text;
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
using System.Reflection.Emit; using System.Reflection.Emit;
using BITKit.Mod;
using UnityEngine.InputSystem.Interactions; using UnityEngine.InputSystem.Interactions;
using Label = UnityEngine.UIElements.Label; using Label = UnityEngine.UIElements.Label;
using Object = UnityEngine.Object;
// ReSharper disable PossibleMultipleEnumeration // ReSharper disable PossibleMultipleEnumeration
namespace BITKit.Console namespace BITKit.Console
{ {
public class BITConsole : MonoBehaviour public class UXConsole:IDisposable
{ {
[RuntimeInitializeOnLoadMethod]
private static void Reload()
{
Application.logMessageReceivedThreaded += EnqueueLog;
}
[BITCommand] [BITCommand]
// ReSharper disable once UnusedMember.Global
public static void Console_Exception_Print_StackTrace(int allow) 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 private class CommandSelector
{ {
public VisualElement Container { get; set; } public VisualElement Container { get; set; }
@@ -100,111 +157,58 @@ namespace BITKit.Console
public static async void Clear() public static async void Clear()
{ {
await BITApp.SwitchToMainThread(); await BITApp.SwitchToMainThread();
singleton.outputString.Clear(); _singleton._outputString.Clear();
singleton.text.text = string.Empty; _singleton._text.text = string.Empty;
} }
private static BITConsole singleton; private static UXConsole _singleton;
[SerializeField] private UIDocument document;
[SerializeReference] private InputActionReference toggleAction;
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) 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() private const int LOGLineLimit = 64;
{
allowGlobalActivation = false
};
public int logLineLimit = 64;
[UXBindPath("commands-container")] [UXBindPath("commands-container")]
private VisualElement commandContainer; private VisualElement _commandContainer;
[UXBindPath("TextField")] [UXBindPath("TextField")]
private TextField textField; private TextField _textField;
[UXBindPath("Text")] [UXBindPath("Text")]
private Label text; private Label _text;
[UXBindPath( "context-scrollview")] [UXBindPath( "context-scrollview")]
private ScrollView scrollView; private ScrollView _scrollView;
private bool _isRunning; private bool _isRunning;
private List<string> outputString = new(); private List<string> _outputString = new();
private CommandSelector _commandSelector; 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() private void OnNextLine()
{ {
if (outputString.Count is not 0 && outputString.Last() != string.Empty) if (_outputString.Count is not 0 && _outputString.Last() != string.Empty)
outputString.Add(string.Empty); _outputString.Add(string.Empty);
} }
private void Toggle(bool active)
private void OnDestroy()
{
_inputActionGroup.allowInput.RemoveElement(this);
}
public async void Toggle(bool active)
{ {
_commandSelector.SetMethods(null); _commandSelector.SetMethods(null);
document.rootVisualElement.SetActive(active); _rootVisualElement.SetActive(active);
_isRunning = active; _isRunning = active;
BITAppForUnity.AllowCursor.SetElements(this,active); BITAppForUnity.AllowCursor.SetElements(this,active);
BITInputSystem.AllowInput.SetDisableElements(this,active); BITInputSystem.AllowInput.SetDisableElements(this,active);
await UniTask.WaitForEndOfFrame(this);
if (active) if (active)
{ {
textField.SetValueWithoutNotify(string.Empty); _textField.SetValueWithoutNotify(string.Empty);
textField.Focus(); _textField.Focus();
scrollView.ScrollToBottom(); _scrollView.ScrollToBottom();
} }
else else
{ {
text.Blur(); _text.Blur();
} }
} }
private void OnTextFieldValueChanged(ChangeEvent<string> callback) private void OnTextFieldValueChanged(ChangeEvent<string> callback)
@@ -215,11 +219,11 @@ namespace BITKit.Console
_commandSelector.SetMethods(commands); _commandSelector.SetMethods(commands);
commandContainer.SetActive(commands.Length is not 0); _commandContainer.SetActive(commands.Length is not 0);
} }
else else
{ {
commandContainer.SetActive(false); _commandContainer.SetActive(false);
_commandSelector.SetMethods(null); _commandSelector.SetMethods(null);
} }
@@ -230,20 +234,17 @@ namespace BITKit.Console
switch (keyDownEvent.keyCode) switch (keyDownEvent.keyCode)
{ {
case KeyCode.Return: case KeyCode.Return:
var cmd = textField.text; var cmd = _textField.text;
LogCallback($">{cmd}", string.Empty, LogType.Log); LogCallback($">{cmd}", string.Empty, LogType.Log);
textField.SetValueWithoutNotify(string.Empty); _textField.SetValueWithoutNotify(string.Empty);
await UniTask.NextFrame(); await UniTask.NextFrame();
if (destroyCancellationToken.IsCancellationRequested) return;
_textField.Blur();
textField.Blur(); _textField.Focus();
textField.Focus();
BITCommands.Excute(cmd); BITCommands.Excute(cmd);
@@ -252,10 +253,10 @@ namespace BITKit.Console
break; break;
case KeyCode.Tab: case KeyCode.Tab:
break; break;
case KeyCode.DownArrow when string.IsNullOrEmpty(textField.text) is false: case KeyCode.DownArrow when string.IsNullOrEmpty(_textField.text) is false:
_commandSelector.Index-=1; _commandSelector.Index-=1;
break; break;
case KeyCode.UpArrow when string.IsNullOrEmpty(textField.text) is false: case KeyCode.UpArrow when string.IsNullOrEmpty(_textField.text) is false:
_commandSelector.Index+=1; _commandSelector.Index+=1;
break; break;
default: default:
@@ -269,16 +270,6 @@ namespace BITKit.Console
keyDownEvent.PreventDefault(); 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) private async void LogCallback(string condition, string stackTrace, LogType type)
{ {
try try
@@ -286,33 +277,33 @@ namespace BITKit.Console
switch (type) switch (type)
{ {
case LogType.Error: case LogType.Error:
outputString.Add($"<color=red>{condition}</color>"); _outputString.Add($"<color=red>{condition}</color>");
break; break;
case LogType.Warning: case LogType.Warning:
outputString.Add($"<color=yellow>{condition}</color>"); _outputString.Add($"<color=yellow>{condition}</color>");
break; break;
case LogType.Exception: case LogType.Exception:
outputString.Add($"<color=red>{condition}</color>"); _outputString.Add($"<color=red>{condition}</color>");
if (exceptionPrintStackTrace) if (_exceptionPrintStackTrace)
outputString.Add($"<color=red>{stackTrace}</color>"); _outputString.Add($"<color=red>{stackTrace}</color>");
break; break;
default: default:
outputString.Add(condition); _outputString.Add(condition);
break; break;
} }
var length = outputString.Count; var length = _outputString.Count;
if (length > logLineLimit) if (length > LOGLineLimit)
{ {
outputString = outputString.GetRange(length - logLineLimit, logLineLimit); _outputString = _outputString.GetRange(length - LOGLineLimit, LOGLineLimit);
} }
StringBuilder stringBuilder = new(); StringBuilder stringBuilder = new();
outputString.ForEach(x => stringBuilder.AppendLine(x)); _outputString.ForEach(x => stringBuilder.AppendLine(x));
try try
{ {
await BITApp.SwitchToMainThread(); await BITApp.SwitchToMainThread();
scrollView.ScrollToBottomAutomatic(); _scrollView.ScrollToBottomAutomatic();
text.text = stringBuilder.ToString(); _text.text = stringBuilder.ToString();
} }
catch (OperationCanceledException) 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); LogCallback(log.condition, log.stackTrace, log.type);
} }
if (_isRunning is false) return; if (_isRunning is false) return;
var pos = textField.worldTransform.GetPosition(); var pos = _textField.worldTransform.GetPosition();
var size = textField.layout.size; var size = _textField.layout.size;
commandContainer.style.left = 0; _commandContainer.style.left = 0;
commandContainer.style.top = 0; _commandContainer.style.top = 0;
pos.y += size.y; 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;
} }
} }
} }

View File

@@ -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);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4c7ee9ea9aa58d94b86a980a58c8140a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 10e55a4b52fd0e443a17f5267823bd92
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View 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;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 58aae7a560a8881468df0f0f3a137f4a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -5,6 +5,7 @@ using AYellowpaper.SerializedCollections;
#if UNITY_EDITOR #if UNITY_EDITOR
using UnityEditor; using UnityEditor;
using YooAsset;
#endif #endif
using UnityEngine; using UnityEngine;
@@ -21,7 +22,15 @@ namespace BITKit
{ {
if (_singleton == null) 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; return _singleton;
} }

View File

@@ -1,12 +1,12 @@
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.Remoting.Contexts; using System.ComponentModel.Design;
using UnityEngine; using UnityEngine;
using UnityEngine.UIElements; using UnityEngine.UIElements;
using UnityEngine.InputSystem; using UnityEngine.InputSystem;
using BITKit; using BITKit.Mod;
using Cysharp.Threading.Tasks; using Object = UnityEngine.Object;
namespace BITKit.UX.Internal namespace BITKit.UX.Internal
{ {
public class ContextMenu : IContextMenu public class ContextMenu : IContextMenu
@@ -20,16 +20,16 @@ namespace BITKit.UX.Internal
{ {
public Label(string text) public Label(string text)
{ {
this.text = text; _text = text;
} }
string text; private readonly string _text;
public override VisualElement GetVisualElement() public override VisualElement GetVisualElement()
{ {
UnityEngine.UIElements.Label label = new(text); UnityEngine.UIElements.Label label = new(_text);
return label; return label;
} }
} }
[System.Serializable] [Serializable]
public class TestContextMenu public class TestContextMenu
{ {
public void Execute() public void Execute()
@@ -45,19 +45,20 @@ namespace BITKit.UX.Internal
{ {
public Button(string text, params Action[] actions) public Button(string text, params Action[] actions)
{ {
this.text = text; _text = text;
foreach (var x in actions) 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() public override VisualElement GetVisualElement()
{ {
UnityEngine.UIElements.Button button = new(); UnityEngine.UIElements.Button button = new();
button.clicked += action; button.clicked += _action;
button.text = text; button.text = _text;
return button; return button;
} }
} }
@@ -77,14 +78,14 @@ namespace BITKit.UX
} }
public static ContextMenuBuilder BuildAction(this ContextMenuBuilder self, string text, Action action) 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; return self;
} }
} }
public class ContextMenuBuilder public class ContextMenuBuilder
{ {
readonly List<IContextMenu> contexts = new(); private readonly List<IContextMenu> _contexts = new();
public event Action OnExcuted; public event Action OnExecuted;
private ContextMenuBuilder() private ContextMenuBuilder()
{ {
@@ -97,25 +98,48 @@ namespace BITKit.UX
{ {
UXContextMenu.Singleton.Create(this); UXContextMenu.Singleton.Create(this);
} }
internal void Excute() internal void Execute()
{ {
OnExcuted?.Invoke(); OnExecuted?.Invoke();
} }
public void Add(IContextMenu x) => contexts.Add(x); public void Add(IContextMenu x) => _contexts.Add(x);
public IEnumerable<IContextMenu> GetContextMenus() => contexts.ToArray(); public IEnumerable<IContextMenu> GetContextMenus() => _contexts.ToArray();
} }
public class UXContextMenu : MonoBehaviour public class UXContextMenu:IDisposable
{ {
internal static UXContextMenu Singleton; internal static UXContextMenu Singleton;
[SerializeField] private UIDocument document; private readonly IUXService _uxService;
private VisualElement root; private VisualElement _root;
private VisualElement container; private VisualElement _container;
private void Awake() public UXContextMenu(IUXService uxService)
{ {
_uxService = uxService;
Singleton = this; Singleton = this;
root = document.rootVisualElement; uxService.OnPanelChanged += OnPanelChanged;
container = document.rootVisualElement[1]; InitializeAsync();
root.Q("background-image").RegisterCallback<MouseDownEvent>(x => }
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)
{
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(); Close();
}); });
@@ -125,25 +149,35 @@ namespace BITKit.UX
{ {
var pos = Mouse.current.position.ReadValue(); var pos = Mouse.current.position.ReadValue();
pos.y = Screen.height - pos.y; 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.position = Position.Absolute;
container.style.left = pos.x; _container.style.left = pos.x;
container.style.top = pos.y; _container.style.top = pos.y;
container.Clear(); _container.Clear();
root.SetActive(true); _root.SetActive(true);
foreach (var context in builder.GetContextMenus()) 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(); _container.Clear();
root.SetActive(false); _root.SetActive(false);
}
public void Dispose()
{
_uxService.OnPanelChanged -= OnPanelChanged;
}
private void OnPanelChanged(IUXPanel arg1, IUXPanel arg2)
{
Close();
} }
} }
} }

View File

@@ -103,7 +103,9 @@ namespace BITKit.UX
public void Entry(IUXPanel panel) => _entryQueue.Push(panel); public void Entry(IUXPanel panel) => _entryQueue.Push(panel);
public void Entry(string panelName) => _entryQueueByName.TryAdd(panelName); public void Entry(string panelName) => _entryQueueByName.TryAdd(panelName);
public IUXPanel CurrentPanel => _currentPanel;
public event Action<IUXPanel, IUXPanel> OnPanelChanged;
public void Return() public void Return()
{ {
if(_windowEntryGroup.TryGetEntried(out _)) if(_windowEntryGroup.TryGetEntried(out _))
@@ -124,6 +126,7 @@ namespace BITKit.UX
private void OnEntry(IUXPanel obj) private void OnEntry(IUXPanel obj)
{ {
OnPanelChanged?.Invoke(_currentPanel,obj);
_currentPanel = obj; _currentPanel = obj;
} }
private void OnTick(float delta) private void OnTick(float delta)

View File

@@ -2,15 +2,15 @@
<Style src="project://database/Assets/BITKit/Unity/UX/Common/Common.uss?fileID=7433441132597879392&amp;guid=a3a69d3518fd02b489e721f3c5b0b539&amp;type=3#Common" /> <Style src="project://database/Assets/BITKit/Unity/UX/Common/Common.uss?fileID=7433441132597879392&amp;guid=a3a69d3518fd02b489e721f3c5b0b539&amp;type=3#Common" />
<Style src="project://database/Assets/BITKit/Unity/UX/BITConsole.uss?fileID=7433441132597879392&amp;guid=9175bfb75cd47b9488b9f12bd34e50fd&amp;type=3#BITConsole" /> <Style src="project://database/Assets/BITKit/Unity/UX/BITConsole.uss?fileID=7433441132597879392&amp;guid=9175bfb75cd47b9488b9f12bd34e50fd&amp;type=3#BITConsole" />
<ui:VisualElement name="Root" style="position: absolute; left: 0; top: 0; right: 0; bottom: 0;"> <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: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: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:ScrollView name="context-scrollview" class="console-container" style="height: auto; flex-grow: 1; display: flex;">
<ui:Label text="This is Console&#10;This is &lt;size=24&gt;Rich Text&lt;/size&gt;&#10;This is &lt;color=yellow&gt;Warning&lt;/color&gt;&#10;This is &lt;color=red&gt;Error&lt;/color&gt;&#10;This is loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong text&#10;This is Console&#10;This is &lt;size=24&gt;Rich Text&lt;/size&gt;&#10;This is &lt;color=yellow&gt;Warning&lt;/color&gt;&#10;This is &lt;color=red&gt;Error&lt;/color&gt;&#10;This is loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong text&#10;&lt;color=#add8e6ff&gt;&lt;b&gt;TypeName&lt;/b&gt;&lt;/color&gt;: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:Label text="This is console&#10;This is &lt;size=24&gt;Rich Text&lt;/size&gt;&#10;This is &lt;color=yellow&gt;Warning&lt;/color&gt;&#10;This is &lt;color=red&gt;Error&lt;/color&gt;&#10;This is loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong text&#10;This is console&#10;This is &lt;size=24&gt;Rich Text&lt;/size&gt;&#10;This is &lt;color=yellow&gt;Warning&lt;/color&gt;&#10;This is &lt;color=red&gt;Error&lt;/color&gt;&#10;This is loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong text&#10;&lt;color=#add8e6ff&gt;&lt;b&gt;TypeName&lt;/b&gt;&lt;/color&gt;: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: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> </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" /> <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" />

View File

@@ -1,19 +1,16 @@
Button { Button {
background-color: rgba(0, 0, 0, 0); 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-left-width: 0;
border-right-width: 0; border-right-width: 0;
border-top-width: 0; border-top-width: 0;
border-bottom-width: 0; border-bottom-width: 0;
-unity-text-align: middle-left;
} }
Button:hover { Button:hover {
background-color: rgba(64, 64, 64, 255); background-color: rgb(64, 64, 64);
} }
Button:active { Button:active {
background-color: rgba(125, 125, 125, 255); background-color: rgb(125, 125, 125);
color: rgba(29, 29, 29, 255); color: rgb(29, 29, 29);
} }

View File

@@ -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);
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 593dc4fe0d60e4e49946531158455923
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: