This commit is contained in:
MuHua-123
2025-06-17 10:53:45 +08:00
parent c6d9b71e95
commit 5374616980
87 changed files with 2265 additions and 1019 deletions
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: c687fc5835bae974e9dc3fab6bc13af3
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -44,21 +44,32 @@ namespace MuHua {
element.generateVisualContent += ElementGenerateVisualContent;
if (sh == UIDirection.FromLeftToRight) { horizontal = new UIScroller(ScrollerHorizontal, canvas, sh); }
if (sh == UIDirection.FromRightToLeft) { horizontal = new UIScroller(ScrollerHorizontal, canvas, sh); }
if (sh == UIDirection.FromLeftToRight) {
horizontal = new UIScroller(ScrollerHorizontal, canvas, sh);
Viewport.style.flexDirection = FlexDirection.Row;
}
if (sh == UIDirection.FromRightToLeft) {
horizontal = new UIScroller(ScrollerHorizontal, canvas, sh);
Viewport.style.flexDirection = FlexDirection.RowReverse;
}
if (sv == UIDirection.FromTopToBottom) { vertical = new UIScroller(ScrollerVertical, canvas, sv); }
if (sv == UIDirection.FromBottomToTop) { vertical = new UIScroller(ScrollerVertical, canvas, sv); }
if (sv == UIDirection.FromTopToBottom) {
vertical = new UIScroller(ScrollerVertical, canvas, sv);
Viewport.style.flexDirection = FlexDirection.Column;
}
if (sv == UIDirection.FromBottomToTop) {
vertical = new UIScroller(ScrollerVertical, canvas, sv);
Viewport.style.flexDirection = FlexDirection.ColumnReverse;
}
//设置事件
// 设置事件
horizontal.ValueChanged += (x) => { UpdateValue(new Vector2(x, value.y)); };
vertical.ValueChanged += (y) => { UpdateValue(new Vector2(value.x, y)); };
Viewport.RegisterCallback<WheelEvent>(ViewportWheel);
Viewport.RegisterCallback<PointerDownEvent>(DraggerDown);
Viewport.RegisterCallback<MouseCaptureEvent>((evt) => isDrag = false);
// 释放
canvas.RegisterCallback<PointerUpEvent>((evt) => isDrag = false);
canvas.RegisterCallback<PointerLeaveEvent>((evt) => isDrag = false);
}
@@ -73,12 +84,9 @@ namespace MuHua {
/// <summary> 视图滚轮滑动 </summary>
private void ViewportWheel(WheelEvent evt) {
float wheel = Mathf.Clamp(evt.delta.y, -1, 1);
if (direction == UIDirection.Horizontal) {
UpdateValue(new Vector2(value.x - wheel, value.y));
}
else {
UpdateValue(new Vector2(value.x, value.y - wheel));
}
Vector2 offset = new Vector2(0, wheel);
if (direction == UIDirection.Horizontal) { offset = new Vector2(wheel, 0); }
UpdateValue(new Vector2(value.x, value.y) - offset);
}
private void DraggerDown(PointerDownEvent evt) {
isDrag = true;
@@ -111,6 +119,8 @@ namespace MuHua {
float x = offset.x / maxWidth;
float y = offset.y / maxHeight;
x *= sh == UIDirection.FromLeftToRight ? 1 : -1;
y *= sv == UIDirection.FromTopToBottom ? 1 : -1;
UpdateValue(new Vector2(x, y));
}
/// <summary> 更新值(0-1) </summary>
@@ -0,0 +1,117 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
namespace MuHua {
/// <summary>
/// 滚动视图 - 垂直
/// </summary>
public class UIScrollViewV : ModuleUIPanel {
/// <summary> 绑定的画布 </summary>
public readonly VisualElement canvas;
/// <summary> 元素方向 </summary>
public readonly UIDirection direction;
/// <summary> 垂直滑块 </summary>
public readonly UIScroller vertical;
/// <summary> 值改变时 </summary>
public event Action<float> ValueChanged;
/// <summary>
/// 方向
/// </summary>
public enum UIDirection {
FromTopToBottom = 0,
FromBottomToTop = 1,
}
public float value;
public bool isDrag;
public Vector3 originalPosition;
public Vector3 pointerPosition;
public VisualElement Viewport => Q<VisualElement>("Viewport");
public VisualElement Container => Q<VisualElement>("Container");
public VisualElement ScrollerVertical => Q<VisualElement>("ScrollerVertical");
public UIScrollViewV(VisualElement element, VisualElement canvas, UIDirection direction) : base(element) {
this.canvas = canvas;
this.direction = direction;
if (direction == UIDirection.FromTopToBottom) {
// vertical = new UIScroller(ScrollerVertical, canvas, direction);
Viewport.style.flexDirection = FlexDirection.Column;
}
if (direction == UIDirection.FromBottomToTop) {
// vertical = new UIScroller(ScrollerVertical, canvas, direction);
Viewport.style.flexDirection = FlexDirection.ColumnReverse;
}
// 设置事件
vertical.ValueChanged += (y) => { UpdateValue(y); };
Viewport.RegisterCallback<WheelEvent>(ViewportWheel);
Viewport.RegisterCallback<PointerDownEvent>(DraggerDown);
Viewport.RegisterCallback<MouseCaptureEvent>((evt) => isDrag = false);
// 释放
canvas.RegisterCallback<PointerUpEvent>((evt) => isDrag = false);
canvas.RegisterCallback<PointerLeaveEvent>((evt) => isDrag = false);
// 视图原始更新
element.generateVisualContent += ElementGenerateVisualContent;
}
/// <summary> 原始更新 </summary>
private void ElementGenerateVisualContent(MeshGenerationContext context) {
float height = Mathf.Clamp01(Viewport.resolvedStyle.height / Container.resolvedStyle.height);
vertical.Dragger.style.height = Length.Percent(height * 100);
}
/// <summary> 滚轮滑动 </summary>
private void ViewportWheel(WheelEvent evt) {
float wheel = Mathf.Clamp(evt.delta.y, -1, 1);
UpdateValue(value - wheel);
}
/// <summary> 拖拽按下 </summary>
private void DraggerDown(PointerDownEvent evt) {
isDrag = true;
originalPosition = Container.transform.position;
Vector3 mousePosition = UITool.GetMousePosition();
pointerPosition = new Vector3(mousePosition.x, Screen.height - mousePosition.y);
}
/// <summary> 拖拽滑动 </summary>
private void Dragger() {
Vector3 mousePosition = UITool.GetMousePosition();
Vector3 differ = new Vector3(mousePosition.x, Screen.height - mousePosition.y) - pointerPosition;
Vector3 offset = differ + originalPosition;
float maxHeight = Viewport.resolvedStyle.height - Container.resolvedStyle.height;
float y = offset.y / maxHeight;
y *= direction == UIDirection.FromTopToBottom ? 1 : -1;
UpdateValue(y);
}
/// <summary> 滑动弹性 </summary>
private void SlidingElasticity() {
float original = value;
float max = Viewport.resolvedStyle.height < Container.resolvedStyle.height ? 1 : 0;
if (value < 0) { value = Mathf.Lerp(value, 0, Time.deltaTime * 10); }
if (value > max) { value = Mathf.Lerp(value, max, Time.deltaTime * 10); }
if (original != value) { UpdateValue(value); }
}
/// <summary> 更新状态 </summary>
public virtual void Update() {
vertical.Update();
SlidingElasticity();
if (isDrag) { Dragger(); }
}
/// <summary> 更新值(0-1) </summary>
public virtual void UpdateValue(float value, bool send = true) {
this.value = value;
if (send) { ValueChanged?.Invoke(value); }
float maxHeight = Viewport.resolvedStyle.height - Container.resolvedStyle.height;
float position = maxHeight * value;
position *= direction == UIDirection.FromTopToBottom ? 1 : -1;
Container.transform.position = new Vector3(0, position);
if (vertical.value != value) { vertical.UpdateValue(value, false); }
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ecfd61355ce764642865903953329f1e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: a21c9c3bc30a9cf4091edd963a99f7af
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -41,8 +41,9 @@ namespace MuHua {
canvas.RegisterCallback<PointerUpEvent>((evt) => isDragger = false);
canvas.RegisterCallback<PointerLeaveEvent>((evt) => isDragger = false);
}
/// <summary> 拖拽元素 </summary>
private void DraggerDown(PointerDownEvent evt) => scrollerFunc.DraggerDown(evt);
/// <summary> 按下元素 </summary>
private void ElementDown(PointerDownEvent evt) => scrollerFunc.ElementDown(evt);
/// <summary> 更新状态 </summary>
public void Update() => scrollerFunc.Update();
@@ -52,17 +53,22 @@ namespace MuHua {
public abstract class UIScrollerFunc {
public readonly UIScroller scroller;
public UIScrollerFunc(UIScroller scroller) => this.scroller = scroller;
/// <summary> 拖拽元素 </summary>
public abstract void DraggerDown(PointerDownEvent evt);
/// <summary> 按下元素 </summary>
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);
}
/// <summary>
/// 滑块从左到右
/// </summary>
public class FromLeftToRight : UIScrollerFunc {
public FromLeftToRight(UIScroller scroller) : base(scroller) { }
public FromLeftToRight(UIScroller scroller) : base(scroller) {
scroller.element.style.flexDirection = FlexDirection.Row;
}
public override void DraggerDown(PointerDownEvent evt) {
scroller.isDragger = true;
scroller.originalPosition = scroller.Dragger.transform.position.x;
@@ -90,9 +96,13 @@ namespace MuHua {
scroller.Dragger.transform.position = new Vector3(x, 0);
}
}
/// <summary>
/// 滑块从右到左
/// </summary>
public class FromRightToLeft : UIScrollerFunc {
public FromRightToLeft(UIScroller scroller) : base(scroller) { }
public FromRightToLeft(UIScroller scroller) : base(scroller) {
scroller.element.style.flexDirection = FlexDirection.RowReverse;
}
public override void DraggerDown(PointerDownEvent evt) {
scroller.isDragger = true;
scroller.originalPosition = scroller.Dragger.transform.position.x;
@@ -120,9 +130,13 @@ namespace MuHua {
scroller.Dragger.transform.position = new Vector3(x, 0);
}
}
/// <summary>
/// 滑块从上到下
/// </summary>
public class FromTopToBottom : UIScrollerFunc {
public FromTopToBottom(UIScroller scroller) : base(scroller) { }
public FromTopToBottom(UIScroller scroller) : base(scroller) {
scroller.element.style.flexDirection = FlexDirection.Column;
}
public override void DraggerDown(PointerDownEvent evt) {
scroller.isDragger = true;
scroller.originalPosition = scroller.Dragger.transform.position.y;
@@ -150,9 +164,13 @@ namespace MuHua {
scroller.Dragger.transform.position = new Vector3(0, y);
}
}
/// <summary>
/// 滑块从下到上
/// </summary>
public class FromBottomToTop : UIScrollerFunc {
public FromBottomToTop(UIScroller scroller) : base(scroller) { }
public FromBottomToTop(UIScroller scroller) : base(scroller) {
scroller.element.style.flexDirection = FlexDirection.ColumnReverse;
}
public override void DraggerDown(PointerDownEvent evt) {
scroller.isDragger = true;
scroller.originalPosition = scroller.Dragger.transform.position.y;
@@ -0,0 +1,22 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
namespace MuHua {
/// <summary>
/// 滚动条 - 垂直
/// </summary>
public class UIScrollerV : MonoBehaviour {
// Start is called before the first frame update
void Start() {
}
// Update is called once per frame
void Update() {
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e1f4e6e2ba12e0b46b1ec2698a680e87
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: