修复BUG

This commit is contained in:
MuHua-123
2025-06-17 16:18:54 +08:00
parent 5e7fa2ef57
commit e14ba6d45f
71 changed files with 6471 additions and 214 deletions
@@ -0,0 +1,180 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
namespace MuHua {
/// <summary>
/// 滑块
/// </summary>
[Obsolete("使用 UISliderV 或者 UISliderH 替换")]
public class UISlider : ModuleUIPanel {
/// <summary> 绑定的画布 </summary>
public readonly VisualElement canvas;
/// <summary> 元素方向 </summary>
public readonly UIDirection direction;
/// <summary> 值改变时 </summary>
public event Action<float> ValueChanged;
public float value;
public bool isDragger;
public float originalPosition;
public float pointerPosition;
public readonly UISliderFunc sliderFunc;
public VisualElement Title => Q<VisualElement>("Title");
public VisualElement Container => Q<VisualElement>("Container");
public VisualElement Tracker => Q<VisualElement>("Tracker");
public VisualElement Dragger => Q<VisualElement>("Dragger");
public UISlider(VisualElement element, VisualElement canvas, UIDirection direction = UIDirection.FromLeftToRight) : base(element) {
this.canvas = canvas;
this.direction = direction;
if (direction == UIDirection.FromLeftToRight) { sliderFunc = new FromLeftToRight(this); }
if (direction == UIDirection.FromRightToLeft) { sliderFunc = new FromRightToLeft(this); }
if (direction == UIDirection.FromTopToBottom) { sliderFunc = new FromTopToBottom(this); }
if (direction == UIDirection.FromBottomToTop) { sliderFunc = new FromBottomToTop(this); }
//设置事件
Dragger.RegisterCallback<PointerDownEvent>(DraggerDown);
Container.RegisterCallback<PointerDownEvent>(ElementDown);
canvas.RegisterCallback<PointerUpEvent>((evt) => isDragger = false);
canvas.RegisterCallback<PointerLeaveEvent>((evt) => isDragger = false);
}
private void DraggerDown(PointerDownEvent evt) => sliderFunc.DraggerDown(evt);
private void ElementDown(PointerDownEvent evt) => sliderFunc.ElementDown(evt);
/// <summary> 更新状态 </summary>
public void Update() => sliderFunc.Update();
/// <summary> 更新值(0-1) </summary>
public void UpdateValue(float value, bool send = true) => sliderFunc.UpdateValue(value, send);
public abstract class UISliderFunc {
public readonly UISlider slider;
public UISliderFunc(UISlider slider) => this.slider = slider;
public abstract void DraggerDown(PointerDownEvent evt);
public abstract void ElementDown(PointerDownEvent evt);
/// <summary> 更新状态 </summary>
public abstract void Update();
/// <summary> 更新值(0-1) </summary>
public abstract void UpdateValue(float value, bool send = true);
}
public class FromLeftToRight : UISliderFunc {
public FromLeftToRight(UISlider slider) : base(slider) { }
public override void DraggerDown(PointerDownEvent evt) {
slider.isDragger = true;
slider.originalPosition = slider.Tracker.resolvedStyle.width;
slider.pointerPosition = UITool.GetMousePosition().x;
}
public override void ElementDown(PointerDownEvent evt) {
float offset = evt.localPosition.x;
float max = slider.Container.resolvedStyle.width;
float value = Mathf.InverseLerp(0, max, offset);
UpdateValue(value);
}
public override void Update() {
if (!slider.isDragger) { return; }
float differ = UITool.GetMousePosition().x - slider.pointerPosition;
float offset = differ + slider.originalPosition;
float max = slider.Container.resolvedStyle.width;
float value = Mathf.InverseLerp(0, max, offset);
UpdateValue(value);
}
public override void UpdateValue(float value, bool send = true) {
slider.value = value;
if (send) { slider.ValueChanged?.Invoke(value); }
slider.Tracker.style.width = Length.Percent(value * 100);
}
}
public class FromRightToLeft : UISliderFunc {
public FromRightToLeft(UISlider slider) : base(slider) { }
public override void DraggerDown(PointerDownEvent evt) {
slider.isDragger = true;
slider.originalPosition = slider.Container.resolvedStyle.width - slider.Tracker.resolvedStyle.width;
slider.pointerPosition = UITool.GetMousePosition().x;
}
public override void ElementDown(PointerDownEvent evt) {
float offset = evt.localPosition.x;
float max = slider.Container.resolvedStyle.width;
float value = Mathf.InverseLerp(max, 0, offset);
UpdateValue(value);
}
public override void Update() {
if (!slider.isDragger) { return; }
float differ = UITool.GetMousePosition().x - slider.pointerPosition;
float offset = differ + slider.originalPosition;
float max = slider.Container.resolvedStyle.width;
float value = Mathf.InverseLerp(max, 0, offset);
UpdateValue(value);
}
public override void UpdateValue(float value, bool send = true) {
slider.value = value;
if (send) { slider.ValueChanged?.Invoke(value); }
slider.Tracker.style.width = Length.Percent(value * 100);
}
}
public class FromTopToBottom : UISliderFunc {
public FromTopToBottom(UISlider slider) : base(slider) { }
public override void DraggerDown(PointerDownEvent evt) {
slider.isDragger = true;
slider.originalPosition = slider.Tracker.resolvedStyle.height;
slider.pointerPosition = Screen.height - UITool.GetMousePosition().y;
}
public override void ElementDown(PointerDownEvent evt) {
float offset = evt.localPosition.y;
float max = slider.Container.resolvedStyle.height;
float value = Mathf.InverseLerp(0, max, offset);
UpdateValue(value);
}
public override void Update() {
if (!slider.isDragger) { return; }
float differ = Screen.height - UITool.GetMousePosition().y - slider.pointerPosition;
float offset = differ + slider.originalPosition;
float max = slider.Container.resolvedStyle.height;
float value = Mathf.InverseLerp(0, max, offset);
UpdateValue(value);
}
public override void UpdateValue(float value, bool send = true) {
slider.value = value;
if (send) { slider.ValueChanged?.Invoke(value); }
slider.Tracker.style.height = Length.Percent(value * 100);
}
}
public class FromBottomToTop : UISliderFunc {
public FromBottomToTop(UISlider slider) : base(slider) { }
public override void DraggerDown(PointerDownEvent evt) {
slider.isDragger = true;
slider.originalPosition = slider.Container.resolvedStyle.height - slider.Tracker.resolvedStyle.height;
slider.pointerPosition = Screen.height - UITool.GetMousePosition().y;
}
public override void ElementDown(PointerDownEvent evt) {
float offset = evt.localPosition.y;
float max = slider.Container.resolvedStyle.height;
float value = Mathf.InverseLerp(max, 0, offset);
UpdateValue(value);
}
public override void Update() {
if (!slider.isDragger) { return; }
float differ = Screen.height - UITool.GetMousePosition().y - slider.pointerPosition;
float offset = differ + slider.originalPosition;
float max = slider.Container.resolvedStyle.height;
float value = Mathf.InverseLerp(max, 0, offset);
UpdateValue(value);
}
public override void UpdateValue(float value, bool send = true) {
slider.value = value;
if (send) { slider.ValueChanged?.Invoke(value); }
slider.Tracker.style.height = Length.Percent(value * 100);
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0721892edd97c594d80af856cbf293c8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,100 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
namespace MuHua {
/// <summary>
/// 滑块 - 水平
/// </summary>
public class UISliderH : ModuleUIPanel, IDisposable {
/// <summary> 绑定的画布 </summary>
public readonly VisualElement canvas;
/// <summary> 元素方向 </summary>
public readonly UIDirection direction;
/// <summary> 值改变时 </summary>
public event Action<float> ValueChanged;
/// <summary>
/// 方向
/// </summary>
public enum UIDirection {
FromLeftToRight = 0,
FromRightToLeft = 1,
}
public float value;
public bool isDragger;
public float originalPosition;
public float pointerPosition;
public readonly VisualElement Title;
public readonly VisualElement Container;
public readonly VisualElement Tracker;
public readonly VisualElement Dragger;
public UISliderH(VisualElement element, VisualElement canvas, UIDirection direction = UIDirection.FromLeftToRight) : base(element) {
this.canvas = canvas;
this.direction = direction;
Title = Q<VisualElement>("Title");
Container = Q<VisualElement>("Container");
Tracker = Q<VisualElement>("Tracker");
Dragger = Q<VisualElement>("Dragger");
//设置事件
Dragger.RegisterCallback<PointerDownEvent>(DraggerDown);
Container.RegisterCallback<PointerDownEvent>(ElementDown);
canvas.RegisterCallback<PointerUpEvent>(DraggerUpOrLeave);
canvas.RegisterCallback<PointerLeaveEvent>(DraggerUpOrLeave);
}
/// <summary> 解绑事件,防止内存泄漏 </summary>
public void Dispose() {
Dragger.UnregisterCallback<PointerDownEvent>(DraggerDown);
element.UnregisterCallback<PointerDownEvent>(ElementDown);
canvas.UnregisterCallback<PointerUpEvent>(DraggerUpOrLeave);
canvas.UnregisterCallback<PointerLeaveEvent>(DraggerUpOrLeave);
}
/// <summary> 拖拽按下 </summary>
private void DraggerDown(PointerDownEvent evt) {
isDragger = true;
float value1 = Tracker.resolvedStyle.width;
float value2 = Container.resolvedStyle.width - Tracker.resolvedStyle.width;
originalPosition = direction == UIDirection.FromLeftToRight ? value1 : value2;
pointerPosition = UITool.GetMousePosition().x;
}
/// <summary> 元素按下 </summary>
private void ElementDown(PointerDownEvent evt) {
float offset = evt.localPosition.x;
float max = Container.resolvedStyle.width;
float value1 = Mathf.InverseLerp(0, max, offset);
float value2 = Mathf.InverseLerp(max, 0, offset);
float value = direction == UIDirection.FromLeftToRight ? value1 : value2;
UpdateValue(value);
}
/// <summary> 鼠标松开或离开 </summary>
private void DraggerUpOrLeave(EventBase evt) {
isDragger = false;
}
/// <summary> 更新状态 </summary>
public void Update() {
if (!isDragger) { return; }
float differ = UITool.GetMousePosition().x - pointerPosition;
float offset = differ + originalPosition;
float max = Container.resolvedStyle.width;
float value1 = Mathf.InverseLerp(0, max, offset);
float value2 = Mathf.InverseLerp(max, 0, offset);
float value = direction == UIDirection.FromLeftToRight ? value1 : value2;
UpdateValue(value);
}
/// <summary> 更新值(0-1) </summary>
public void UpdateValue(float obj, bool send = true) {
value = (float)Math.Round(obj, 3);
if (send) { ValueChanged?.Invoke(value); }
Tracker.style.width = Length.Percent(value * 100);
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 444651e6c05c5bc4cbf72b8740f2c429
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,100 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
namespace MuHua {
/// <summary>
/// 滑块 - 垂直
/// </summary>
public class UISliderV : ModuleUIPanel, IDisposable {
/// <summary> 绑定的画布 </summary>
public readonly VisualElement canvas;
/// <summary> 元素方向 </summary>
public readonly UIDirection direction;
/// <summary> 值改变时 </summary>
public event Action<float> ValueChanged;
/// <summary>
/// 方向
/// </summary>
public enum UIDirection {
FromTopToBottom = 0,
FromBottomToTop = 1,
}
public float value;
public bool isDragger;
public float originalPosition;
public float pointerPosition;
public readonly VisualElement Title;
public readonly VisualElement Container;
public readonly VisualElement Tracker;
public readonly VisualElement Dragger;
public UISliderV(VisualElement element, VisualElement canvas, UIDirection direction = UIDirection.FromTopToBottom) : base(element) {
this.canvas = canvas;
this.direction = direction;
Title = Q<VisualElement>("Title");
Container = Q<VisualElement>("Container");
Tracker = Q<VisualElement>("Tracker");
Dragger = Q<VisualElement>("Dragger");
//设置事件
Dragger.RegisterCallback<PointerDownEvent>(DraggerDown);
Container.RegisterCallback<PointerDownEvent>(ElementDown);
canvas.RegisterCallback<PointerUpEvent>(DraggerUpOrLeave);
canvas.RegisterCallback<PointerLeaveEvent>(DraggerUpOrLeave);
}
/// <summary> 解绑事件,防止内存泄漏 </summary>
public void Dispose() {
Dragger.UnregisterCallback<PointerDownEvent>(DraggerDown);
element.UnregisterCallback<PointerDownEvent>(ElementDown);
canvas.UnregisterCallback<PointerUpEvent>(DraggerUpOrLeave);
canvas.UnregisterCallback<PointerLeaveEvent>(DraggerUpOrLeave);
}
/// <summary> 拖拽按下 </summary>
private void DraggerDown(PointerDownEvent evt) {
isDragger = true;
float value1 = Tracker.resolvedStyle.height;
float value2 = Container.resolvedStyle.height - Tracker.resolvedStyle.height;
originalPosition = direction == UIDirection.FromTopToBottom ? value1 : value2;
pointerPosition = Screen.height - UITool.GetMousePosition().y;
}
/// <summary> 元素按下 </summary>
private void ElementDown(PointerDownEvent evt) {
float offset = evt.localPosition.y;
float max = Container.resolvedStyle.height;
float value1 = Mathf.InverseLerp(0, max, offset);
float value2 = Mathf.InverseLerp(max, 0, offset);
float value = direction == UIDirection.FromTopToBottom ? value1 : value2;
UpdateValue(value);
}
/// <summary> 鼠标松开或离开 </summary>
private void DraggerUpOrLeave(EventBase evt) {
isDragger = false;
}
/// <summary> 更新状态 </summary>
public void Update() {
if (!isDragger) { return; }
float differ = Screen.height - UITool.GetMousePosition().y - pointerPosition;
float offset = differ + originalPosition;
float max = Container.resolvedStyle.height;
float value1 = Mathf.InverseLerp(0, max, offset);
float value2 = Mathf.InverseLerp(max, 0, offset);
float value = direction == UIDirection.FromTopToBottom ? value1 : value2;
UpdateValue(value);
}
/// <summary> 更新值(0-1) </summary>
public void UpdateValue(float obj, bool send = true) {
value = (float)Math.Round(obj, 3);
if (send) { ValueChanged?.Invoke(value); }
Tracker.style.height = Length.Percent(value * 100);
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f80b8dbc68fee4444bbce14f02dafe44
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: