修改UITool包

This commit is contained in:
MuHua-123
2025-03-03 17:47:30 +08:00
parent fb849c9c25
commit 850ba360bb
38 changed files with 591 additions and 255 deletions
+46
View File
@@ -0,0 +1,46 @@
# 参考自 https://learn.microsoft.com/zh-cn/dotnet/fundamentals/code-analysis/style-rules/csharp-formatting-options
root = true
# 所有文件
[*]
charset = utf-8
# CS文件
[*.cs]
indent_style = tab
indent_size = 4
tab_width = 4
 
# if (...) {
#     ...
# }
csharp_new_line_before_open_brace = none
# if (...) {
#     ...
# } else {
#     ...
# }
csharp_new_line_before_else = false
# try {
#     ...
# } catch (Exception e) {
#     ...
# }
csharp_new_line_before_catch = false
# try {
#     ...
# } catch (Exception e) {
#     ...
# } finally {
#     ...
# }
csharp_new_line_before_finally = false
# 错误提示,如果不正常可以删掉
# dotnet_diagnostic.IDE0055.severity = error
@@ -1,7 +1,12 @@
@import url("unity-theme://default");
@import url("/Assets/MuHua/UITool/FontLibrary/Fonts.uss");
@import url("/Assets/MuHua/UITool/UIScroller/ScrollerHorizontal.uss");
@import url("/Assets/MuHua/UITool/UIFloatingWindow/FloatingWindow.uss");
@import url("/Assets/MuHua/UITool/UIScroller/Scroller.uss");
@import url("/Assets/MuHua/UITool/UIScrollView/ScrollView.uss");
File diff suppressed because one or more lines are too long
@@ -0,0 +1,47 @@
.scrollview {
}
.scrollview-viewport {
background-color: rgba(255, 255, 255, 0.6);
overflow: hidden;
margin-right: 30px;
margin-bottom: 30px;
flex-grow: 1;
}
.scrollview-container {
background-color: rgba(0, 0, 0, 0.2);
transition-duration: 0.1s;
flex-direction: column;
align-self: flex-start;
flex-shrink: 0;
}
.scrollview-horizontal-scroller {
height: 30px;
width: auto;
background-color: rgb(255, 255, 255);
position: absolute;
left: 0;
right: 30px;
bottom: 0;
}
.scrollview-horizontal-scroller-dragger {
width: 30px;
height: 100%;
background-color: rgb(255, 141, 141);
}
.scrollview-vertical-scroller {
position: absolute;
top: 0;
right: 0;
bottom: 0;
}
.scrollview-vertical-scroller-dragger {
background-color: rgb(255, 126, 126);
width: 100%;
height: 30px;
}
@@ -1,4 +1,31 @@
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="False">
<Style src="project://database/Assets/MuHua/UITool/USS/ScrollView.uss?fileID=7433441132597879392&amp;guid=d8db2d69206657346a4fe9aebc592327&amp;type=3#ScrollView" />
<MuHua.MUScrollViewVertical />
<Style src="project://database/Assets/MuHua/UITool/UIScrollView/ScrollView.uss?fileID=7433441132597879392&amp;guid=d8db2d69206657346a4fe9aebc592327&amp;type=3#ScrollView" />
<ui:VisualElement name="ScrollView" class="scrollview" style="width: 350px; height: 450px;">
<ui:VisualElement name="Viewport" class="scrollview-viewport">
<ui:VisualElement name="Container" class="scrollview-container" style="width: 1000px; height: 1000px;">
<ui:Button text="1" parse-escape-sequences="true" display-tooltip-when-elided="true" />
<ui:Button text="2" parse-escape-sequences="true" display-tooltip-when-elided="true" />
<ui:Button text="3" parse-escape-sequences="true" display-tooltip-when-elided="true" />
<ui:Button text="4" parse-escape-sequences="true" display-tooltip-when-elided="true" />
<ui:Button text="5" parse-escape-sequences="true" display-tooltip-when-elided="true" />
<ui:Button text="6" parse-escape-sequences="true" display-tooltip-when-elided="true" />
<ui:Button text="7" parse-escape-sequences="true" display-tooltip-when-elided="true" />
<ui:Button text="8" parse-escape-sequences="true" display-tooltip-when-elided="true" />
<ui:Button text="9" parse-escape-sequences="true" display-tooltip-when-elided="true" />
<ui:Button text="10" 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" />
<ui:Button text="Button" parse-escape-sequences="true" display-tooltip-when-elided="true" />
</ui:VisualElement>
</ui:VisualElement>
<ui:VisualElement name="ScrollerHorizontal" class="scroller-horizontal scrollview-horizontal-scroller">
<ui:VisualElement name="Dragger" class="scroller-horizontal-dragger scrollview-horizontal-scroller-dragger" />
</ui:VisualElement>
<ui:VisualElement name="ScrollerVertical" class="scroller-vertical scrollview-vertical-scroller">
<ui:VisualElement name="Dragger" class="scroller-vertical-dragger scrollview-vertical-scroller-dragger" />
</ui:VisualElement>
</ui:VisualElement>
</ui:UXML>
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: b0a55937e12435144b4b7c36d1fd6e95
ScriptedImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 2
userData:
assetBundleName:
assetBundleVariant:
script: {fileID: 12385, guid: 0000000000000000e000000000000000, type: 0}
disableValidation: 0
@@ -1,3 +1,31 @@
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="False">
<Style src="project://database/Assets/MuHua/UITool/UIScrollView/ScrollViewHorizontal.uss?fileID=7433441132597879392&amp;guid=b0a55937e12435144b4b7c36d1fd6e95&amp;type=3#ScrollViewHorizontal" />
<Style src="project://database/Assets/MuHua/UITool/UIScrollView/ScrollView.uss?fileID=7433441132597879392&amp;guid=d8db2d69206657346a4fe9aebc592327&amp;type=3#ScrollView" />
<ui:VisualElement name="ScrollView" class="scrollview" style="width: 350px; height: 450px;">
<ui:VisualElement name="Viewport" class="scrollview-viewport" style="margin-right: 0;">
<ui:VisualElement name="Container" class="scrollview-container" style="width: 1000px; height: 100%;">
<ui:Button text="1" parse-escape-sequences="true" display-tooltip-when-elided="true" />
<ui:Button text="2" parse-escape-sequences="true" display-tooltip-when-elided="true" />
<ui:Button text="3" parse-escape-sequences="true" display-tooltip-when-elided="true" />
<ui:Button text="4" parse-escape-sequences="true" display-tooltip-when-elided="true" />
<ui:Button text="5" parse-escape-sequences="true" display-tooltip-when-elided="true" />
<ui:Button text="6" parse-escape-sequences="true" display-tooltip-when-elided="true" />
<ui:Button text="7" parse-escape-sequences="true" display-tooltip-when-elided="true" />
<ui:Button text="8" parse-escape-sequences="true" display-tooltip-when-elided="true" />
<ui:Button text="9" parse-escape-sequences="true" display-tooltip-when-elided="true" />
<ui:Button text="10" 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" />
<ui:Button text="Button" parse-escape-sequences="true" display-tooltip-when-elided="true" />
</ui:VisualElement>
</ui:VisualElement>
<ui:VisualElement name="ScrollerHorizontal" class="scroller-horizontal scrollview-horizontal-scroller" style="right: 0;">
<ui:VisualElement name="Dragger" class="scroller-horizontal-dragger scrollview-horizontal-scroller-dragger" />
</ui:VisualElement>
<ui:VisualElement name="ScrollerVertical" class="scroller-vertical scrollview-vertical-scroller" style="display: none;">
<ui:VisualElement name="Dragger" class="scroller-vertical-dragger scrollview-vertical-scroller-dragger" />
</ui:VisualElement>
</ui:VisualElement>
</ui:UXML>
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: fd72798a2f7a89b4b96b9d8249af66ad
ScriptedImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 2
userData:
assetBundleName:
assetBundleVariant:
script: {fileID: 12385, guid: 0000000000000000e000000000000000, type: 0}
disableValidation: 0
@@ -1,3 +1,22 @@
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="False">
<Style src="project://database/Assets/MuHua/UITool/UIScrollView/ScrollViewVertical.uss?fileID=7433441132597879392&amp;guid=fd72798a2f7a89b4b96b9d8249af66ad&amp;type=3#ScrollViewVertical" />
<Style src="project://database/Assets/MuHua/UITool/UIScrollView/ScrollView.uss?fileID=7433441132597879392&amp;guid=d8db2d69206657346a4fe9aebc592327&amp;type=3#ScrollView" />
<ui:VisualElement name="ScrollView" class="scrollview" style="width: 350px; height: 450px;">
<ui:VisualElement name="Viewport" class="scrollview-viewport" style="margin-right: 30px; margin-bottom: 0;">
<ui:VisualElement name="Container" class="scrollview-container" style="width: 100%; height: 1000px;">
<ui:Label tabindex="-1" text="Label" parse-escape-sequences="true" display-tooltip-when-elided="true" />
<ui:Button text="Button" parse-escape-sequences="true" display-tooltip-when-elided="true" />
<ui:Toggle label="Toggle" />
<ui:Toggle label="Toggle" />
<ui:DropdownField label="Dropdown" />
<ui:TextField picking-mode="Ignore" label="Text Field" value="filler text" />
<ui:FloatField label="Float Field" value="42.2" />
</ui:VisualElement>
</ui:VisualElement>
<ui:VisualElement name="ScrollerHorizontal" class="scroller-horizontal scrollview-horizontal-scroller" style="right: 0; display: none;">
<ui:VisualElement name="Dragger" class="scroller-horizontal-dragger scrollview-horizontal-scroller-dragger" />
</ui:VisualElement>
<ui:VisualElement name="ScrollerVertical" class="scroller-vertical scrollview-vertical-scroller">
<ui:VisualElement name="Dragger" class="scroller-vertical-dragger scrollview-vertical-scroller-dragger" />
</ui:VisualElement>
</ui:VisualElement>
</ui:UXML>
@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: a75122cc6293a3d4f9ba44b98f4bed9e
guid: df824968495905e4db8daa1a4a942d6f
ScriptedImporter:
internalIDToNameTable: []
externalObjects: {}
@@ -9,3 +9,15 @@
width: 30px;
height: 100%;
}
.scroller-vertical {
width: 30px;
height: 100%;
background-color: rgb(255, 255, 255);
}
.scroller-vertical-dragger {
background-color: rgb(255, 126, 126);
width: 100%;
height: 30px;
}
@@ -3,4 +3,7 @@
<ui:VisualElement name="ScrollerHorizontal" class="scroller-horizontal">
<ui:VisualElement name="Dragger" class="scroller-horizontal-dragger" />
</ui:VisualElement>
<ui:VisualElement name="ScrollerVertical" class="scroller-vertical">
<ui:VisualElement name="Dragger" class="scroller-vertical-dragger" />
</ui:VisualElement>
</ui:UXML>
@@ -1,11 +0,0 @@
.vertical-scroller {
width: 30px;
height: 100%;
background-color: rgb(255, 255, 255);
}
.vertical-scroller-dragger {
background-color: rgb(255, 126, 126);
width: 100%;
height: 30px;
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: a7fd7497066a90d4cad4bb96a3d9cf68
ScriptedImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 2
userData:
assetBundleName:
assetBundleVariant:
script: {fileID: 12385, guid: 0000000000000000e000000000000000, type: 0}
disableValidation: 0
@@ -1,8 +0,0 @@
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="False">
<Style src="project://database/Assets/MuHua/UITool/USS/Scroller.uss?fileID=7433441132597879392&amp;guid=a7fd7497066a90d4cad4bb96a3d9cf68&amp;type=3#Scroller" />
<MuHua.MUScrollerHorizontal />
<MuHua.MUScrollerVertical />
<ui:VisualElement name="ScrollerVertical" style="flex-grow: 1;">
<ui:VisualElement name="Dragger" style="flex-grow: 1;" />
</ui:VisualElement>
</ui:UXML>
@@ -1,10 +0,0 @@
fileFormatVersion: 2
guid: f1b09f67b8d868f48ae3011621da6b73
ScriptedImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 2
userData:
assetBundleName:
assetBundleVariant:
script: {fileID: 13804, guid: 0000000000000000e000000000000000, type: 0}
+1
View File
@@ -152,6 +152,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 48270532e0731284a92ff8556d892c7e, type: 3}
m_Name:
m_EditorClassIdentifier:
document: {fileID: 36355709}
--- !u!114 &36355709
MonoBehaviour:
m_ObjectHideFlags: 0
+23 -12
View File
@@ -4,17 +4,28 @@ using UnityEngine;
using UnityEngine.UIElements;
using MuHua;
public class TestUI : MonoBehaviour
{
/// <summary> 绑定文档 </summary>
public UIDocument document;
/// <summary> 根目录文档 </summary>
public VisualElement root => document.rootVisualElement;
public class TestUI : MonoBehaviour {
/// <summary> 绑定文档 </summary>
public UIDocument document;
/// <summary> 根目录文档 </summary>
public VisualElement root => document.rootVisualElement;
private UIScrollerHorizontal scroller;
private void Awake()
{
VisualElement Scroller = root.Q<VisualElement>();
scroller = new UIScrollerHorizontal(Scroller, root);
}
private UIScrollView scrollView;
private UIScrollView scrollViewHorizontal;
private UIScrollView scrollViewVertical;
private void Awake() {
VisualElement ScrollView = root.Q<VisualElement>("ScrollView");
scrollView = new UIScrollView(ScrollView, root, UIDirection.HorizontalAndVertical);
VisualElement ScrollViewHorizontal = root.Q<VisualElement>("ScrollViewHorizontal");
scrollViewHorizontal = new UIScrollView(ScrollViewHorizontal, root, UIDirection.Horizontal);
VisualElement ScrollViewVertical = root.Q<VisualElement>("ScrollViewVertical");
scrollViewVertical = new UIScrollView(ScrollViewVertical, root, UIDirection.Vertical);
}
private void Update() {
scrollView.Update();
scrollViewHorizontal.Update();
scrollViewVertical.Update();
}
}
+7 -2
View File
@@ -1,5 +1,10 @@
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../UIElementsSchema/UIElements.xsd" editor-extension-mode="False">
<ui:VisualElement name="ScrollerHorizontal" class="scroller-horizontal" style="width: 300px; position: absolute; left: 50px; top: 50px;">
<ui:VisualElement name="Dragger" class="scroller-horizontal-dragger" />
<ui:Template name="ScrollView" src="project://database/Assets/MuHua/UITool/UIScrollView/ScrollView.uxml?fileID=9197481963319205126&amp;guid=e95fad6397f22504eb724a8ea0c8d926&amp;type=3#ScrollView" />
<ui:Template name="ScrollViewHorizontal" src="project://database/Assets/MuHua/UITool/UIScrollView/ScrollViewHorizontal.uxml?fileID=9197481963319205126&amp;guid=ac460a2d93de869499c04a4295795595&amp;type=3#ScrollViewHorizontal" />
<ui:Template name="ScrollViewVertical" src="project://database/Assets/MuHua/UITool/UIScrollView/ScrollViewVertical.uxml?fileID=9197481963319205126&amp;guid=df824968495905e4db8daa1a4a942d6f&amp;type=3#ScrollViewVertical" />
<ui:VisualElement style="flex-grow: 1; flex-direction: row;">
<ui:Instance template="ScrollView" name="ScrollView" />
<ui:Instance template="ScrollViewHorizontal" name="ScrollViewHorizontal" />
<ui:Instance template="ScrollViewVertical" name="ScrollViewVertical" />
</ui:VisualElement>
</ui:UXML>
@@ -0,0 +1,124 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
namespace MuHua {
/// <summary>
/// 滚动视图
/// </summary>
public class UIScrollView {
/// <summary> 绑定的元素 </summary>
public readonly VisualElement element;
/// <summary> 绑定的画布 </summary>
public readonly VisualElement canvas;
/// <summary> 元素方向 </summary>
public readonly UIDirection direction;
/// <summary> 值改变时 </summary>
public event Action<Vector2> ValueChanged;
public Vector2 value;
public bool isDrag;
public Vector3 originalPosition;
public Vector3 pointerPosition;
private UIScroller horizontal;
private UIScroller vertical;
public VisualElement Viewport => element.Q<VisualElement>("Viewport");
public VisualElement Container => element.Q<VisualElement>("Container");
public VisualElement ScrollerHorizontal => element.Q<VisualElement>("ScrollerHorizontal");
public VisualElement ScrollerVertical => element.Q<VisualElement>("ScrollerVertical");
public UIScrollView(VisualElement element, VisualElement canvas, UIDirection direction = UIDirection.HorizontalAndVertical, UIDirection sh = UIDirection.FromLeftToRight, UIDirection sv = UIDirection.FromTopToBottom) {
this.element = element;
this.canvas = canvas;
this.direction = direction;
element.generateVisualContent += ElementGenerateVisualContent;
if (sh == UIDirection.FromLeftToRight) { horizontal = new UIScroller(ScrollerHorizontal, canvas, sh); }
if (sh == UIDirection.FromRightToLeft) { horizontal = new UIScroller(ScrollerHorizontal, canvas, sh); }
if (sv == UIDirection.FromTopToBottom) { vertical = new UIScroller(ScrollerVertical, canvas, sv); }
if (sv == UIDirection.FromBottomToTop) { vertical = new UIScroller(ScrollerVertical, canvas, sv); }
//设置事件
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);
}
/// <summary> 视图原始更新 </summary>
private void ElementGenerateVisualContent(MeshGenerationContext context) {
float width = Mathf.Clamp01(Viewport.resolvedStyle.width / Container.resolvedStyle.width);
float height = Mathf.Clamp01(Viewport.resolvedStyle.height / Container.resolvedStyle.height);
horizontal.dragger.style.width = Length.Percent(width * 100);
vertical.dragger.style.height = Length.Percent(height * 100);
}
/// <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));
}
}
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>
public void Update() {
horizontal.Update();
vertical.Update();
Vector2 original = value;
float maxX = Viewport.resolvedStyle.width < Container.resolvedStyle.width ? 1 : 0;
float maxY = Viewport.resolvedStyle.height < Container.resolvedStyle.height ? 1 : 0;
if (value.x < 0) { value.x = Mathf.Lerp(value.x, 0, Time.deltaTime * 10); }
if (value.x > maxX) { value.x = Mathf.Lerp(value.x, maxX, Time.deltaTime * 10); }
if (value.y < 0) { value.y = Mathf.Lerp(value.y, 0, Time.deltaTime * 10); }
if (value.y > maxY) { value.y = Mathf.Lerp(value.y, maxY, Time.deltaTime * 10); }
if (original != value) { UpdateValue(value); }
if (!isDrag) { return; }
Vector3 mousePosition = UITool.GetMousePosition();
Vector3 differ = new Vector3(mousePosition.x, Screen.height - mousePosition.y) - pointerPosition;
Vector3 offset = differ + originalPosition;
float maxWidth = Viewport.resolvedStyle.width - Container.resolvedStyle.width;
float maxHeight = Viewport.resolvedStyle.height - Container.resolvedStyle.height;
float x = offset.x / maxWidth;
float y = offset.y / maxHeight;
UpdateValue(new Vector2(x, y));
}
/// <summary> 更新值(0-1) </summary>
public void UpdateValue(Vector2 value, bool send = true) {
if (direction == UIDirection.Horizontal) { value.y = 0; }
if (direction == UIDirection.Vertical) { value.x = 0; }
this.value = value;
if (send) { ValueChanged?.Invoke(value); }
float maxWidth = Viewport.resolvedStyle.width - Container.resolvedStyle.width;
float maxHeight = Viewport.resolvedStyle.height - Container.resolvedStyle.height;
float xPos = maxWidth * value.x;
float yPos = maxHeight * value.y;
Container.transform.position = new Vector3(xPos, yPos);
if (horizontal.value != value.x) { horizontal.UpdateValue(value.x, false); }
if (vertical.value != value.y) { vertical.UpdateValue(value.y, false); }
}
}
}
@@ -0,0 +1,187 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
namespace MuHua {
/// <summary>
/// 滑块
/// </summary>
public class UIScroller {
/// <summary> 绑定的元素 </summary>
public readonly VisualElement element;
/// <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;
private UIScrollerFunc scrollerFunc;
public VisualElement dragger => element.Q<VisualElement>("Dragger");
public UIScroller(VisualElement element, VisualElement canvas, UIDirection direction = UIDirection.FromLeftToRight) {
this.element = element;
this.canvas = canvas;
this.direction = direction;
if (direction == UIDirection.FromLeftToRight) { scrollerFunc = new FromLeftToRight(this); }
if (direction == UIDirection.FromRightToLeft) { scrollerFunc = new FromRightToLeft(this); }
if (direction == UIDirection.FromTopToBottom) { scrollerFunc = new FromTopToBottom(this); }
if (direction == UIDirection.FromBottomToTop) { scrollerFunc = new FromBottomToTop(this); }
//设置事件
dragger.RegisterCallback<PointerDownEvent>(DraggerDown);
element.RegisterCallback<PointerDownEvent>(ElementDown);
canvas.RegisterCallback<PointerUpEvent>((evt) => isDragger = false);
canvas.RegisterCallback<PointerLeaveEvent>((evt) => isDragger = false);
}
public virtual void DraggerDown(PointerDownEvent evt) => scrollerFunc.DraggerDown(evt);
public virtual void ElementDown(PointerDownEvent evt) => scrollerFunc.ElementDown(evt);
/// <summary> 更新状态 </summary>
public virtual void Update() => scrollerFunc.Update();
/// <summary> 更新值(0-1) </summary>
public virtual void UpdateValue(float value, bool send = true) => scrollerFunc.UpdateValue(value, send);
public abstract class UIScrollerFunc {
public readonly UIScroller scroller;
public UIScrollerFunc(UIScroller scroller) => this.scroller = scroller;
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 : UIScrollerFunc {
public FromLeftToRight(UIScroller scroller) : base(scroller) { }
public override void DraggerDown(PointerDownEvent evt) {
scroller.isDragger = true;
scroller.originalPosition = scroller.dragger.transform.position.x;
scroller.pointerPosition = UITool.GetMousePosition().x;
}
public override void ElementDown(PointerDownEvent evt) {
float offset = evt.localPosition.x - scroller.dragger.resolvedStyle.width * 0.5f;
float max = scroller.element.resolvedStyle.width - scroller.dragger.resolvedStyle.width;
float value = Mathf.InverseLerp(0, max, offset);
UpdateValue(value);
}
public override void Update() {
if (!scroller.isDragger) { return; }
float differ = UITool.GetMousePosition().x - scroller.pointerPosition;
float offset = differ + scroller.originalPosition;
float max = scroller.element.resolvedStyle.width - scroller.dragger.resolvedStyle.width;
float value = Mathf.InverseLerp(0, max, offset);
UpdateValue(value);
}
public override void UpdateValue(float value, bool send = true) {
scroller.value = value;
if (send) { scroller.ValueChanged?.Invoke(value); }
float max = scroller.element.resolvedStyle.width - scroller.dragger.resolvedStyle.width;
float x = Mathf.Lerp(0, max, value);
scroller.dragger.transform.position = new Vector3(x, 0);
}
}
public class FromRightToLeft : UIScrollerFunc {
public FromRightToLeft(UIScroller scroller) : base(scroller) { }
public override void DraggerDown(PointerDownEvent evt) {
scroller.isDragger = true;
scroller.originalPosition = scroller.dragger.transform.position.x;
scroller.pointerPosition = UITool.GetMousePosition().x;
}
public override void ElementDown(PointerDownEvent evt) {
float offset = evt.localPosition.x - scroller.dragger.resolvedStyle.width * 0.5f;
float max = scroller.element.resolvedStyle.width - scroller.dragger.resolvedStyle.width;
float value = Mathf.InverseLerp(max, 0, offset);
UpdateValue(value);
}
public override void Update() {
if (!scroller.isDragger) { return; }
float differ = UITool.GetMousePosition().x - scroller.pointerPosition;
float offset = differ + scroller.originalPosition;
float max = scroller.element.resolvedStyle.width - scroller.dragger.resolvedStyle.width;
float value = Mathf.InverseLerp(max, 0, offset);
UpdateValue(value);
}
public override void UpdateValue(float value, bool send = true) {
scroller.value = value;
if (send) { scroller.ValueChanged?.Invoke(value); }
float max = scroller.element.resolvedStyle.width - scroller.dragger.resolvedStyle.width;
float x = Mathf.Lerp(max, 0, value);
scroller.dragger.transform.position = new Vector3(x, 0);
}
}
public class FromTopToBottom : UIScrollerFunc {
public FromTopToBottom(UIScroller scroller) : base(scroller) { }
public override void DraggerDown(PointerDownEvent evt) {
scroller.isDragger = true;
scroller.originalPosition = scroller.dragger.transform.position.y;
scroller.pointerPosition = Screen.height - UITool.GetMousePosition().y;
}
public override void ElementDown(PointerDownEvent evt) {
float offset = evt.localPosition.y - scroller.dragger.resolvedStyle.height * 0.5f;
float max = scroller.element.resolvedStyle.height - scroller.dragger.resolvedStyle.height;
float value = Mathf.InverseLerp(0, max, offset);
UpdateValue(value);
}
public override void Update() {
if (!scroller.isDragger) { return; }
float differ = Screen.height - UITool.GetMousePosition().y - scroller.pointerPosition;
float offset = differ + scroller.originalPosition;
float max = scroller.element.resolvedStyle.height - scroller.dragger.resolvedStyle.height;
float value = Mathf.InverseLerp(0, max, offset);
UpdateValue(value);
}
public override void UpdateValue(float value, bool send = true) {
scroller.value = value;
if (send) { scroller.ValueChanged?.Invoke(value); }
float max = scroller.element.resolvedStyle.height - scroller.dragger.resolvedStyle.height;
float y = Mathf.Lerp(0, max, value);
scroller.dragger.transform.position = new Vector3(0, y);
}
}
public class FromBottomToTop : UIScrollerFunc {
public FromBottomToTop(UIScroller scroller) : base(scroller) { }
public override void DraggerDown(PointerDownEvent evt) {
scroller.isDragger = true;
scroller.originalPosition = scroller.dragger.transform.position.y;
scroller.pointerPosition = Screen.height - UITool.GetMousePosition().y;
}
public override void ElementDown(PointerDownEvent evt) {
float offset = evt.localPosition.y - scroller.dragger.resolvedStyle.height * 0.5f;
float max = scroller.element.resolvedStyle.height - scroller.dragger.resolvedStyle.height;
float value = Mathf.InverseLerp(max, 0, offset);
UpdateValue(value);
}
public override void Update() {
if (!scroller.isDragger) { return; }
float differ = Screen.height - UITool.GetMousePosition().y - scroller.pointerPosition;
float offset = differ + scroller.originalPosition;
float max = scroller.element.resolvedStyle.height - scroller.dragger.resolvedStyle.height;
float value = Mathf.InverseLerp(max, 0, offset);
UpdateValue(value);
}
public override void UpdateValue(float value, bool send = true) {
scroller.value = value;
if (send) { scroller.ValueChanged?.Invoke(value); }
float max = scroller.element.resolvedStyle.height - scroller.dragger.resolvedStyle.height;
float y = Mathf.Lerp(max, 0, value);
scroller.dragger.transform.position = new Vector3(0, y);
}
}
}
}
@@ -0,0 +1,19 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
/// <summary>
/// 方向
/// </summary>
public enum UIDirection {
FromLeftToRight = 0,
FromRightToLeft = 1,
FromTopToBottom = 2,
FromBottomToTop = 3,
HorizontalAndVertical = 4,
Horizontal = 5,
Vertical = 6,
}
}
@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: d1f12d534a6e94f4d93a1a91f85e0519
guid: c2b0340184fec6c45815d13d6a1f7321
MonoImporter:
externalObjects: {}
serializedVersion: 2
@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 347543776b7c9134c8f51ee8930b55ea
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,14 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua
{
/// <summary>
/// 滚动视图
/// </summary>
public class UIScrollView
{
}
}
@@ -1,15 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua
{
/// <summary>
/// 滚动视图(水平)
/// </summary>
public class UIScrollViewHorizontal
{
}
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: cce3cdf2fe4ebe14e8afcf78f68c9469
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,14 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua
{
/// <summary>
/// 滚动视图(垂直)
/// </summary>
public class UIScrollViewVertical
{
}
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: d15576153e2ca2d40b07796a726007bf
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,76 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
namespace MuHua
{
/// <summary>
/// 滑块(水平)
/// </summary>
public class UIScrollerHorizontal
{
/// <summary> 绑定的元素 </summary>
public readonly VisualElement element;
/// <summary> 绑定的画布 </summary>
public readonly VisualElement canvas;
/// <summary> 值改变时 </summary>
public event Action<float> ValueChanged;
public float value;
public bool isDragger;
public float originalPosition;
public float pointerPosition;
public VisualElement dragger => element.Q<VisualElement>("Dragger");
public UIScrollerHorizontal(VisualElement element, VisualElement canvas)
{
this.element = element;
this.canvas = canvas;
//设置事件
dragger.RegisterCallback<PointerDownEvent>(DraggerDown);
element.RegisterCallback<PointerDownEvent>(ElementDown);
canvas.RegisterCallback<PointerUpEvent>((evt) => isDragger = false);
canvas.RegisterCallback<PointerLeaveEvent>((evt) => isDragger = false);
}
public void DraggerDown(PointerDownEvent evt)
{
isDragger = true;
originalPosition = dragger.transform.position.x;
pointerPosition = UITool.GetMousePosition().x;
}
public void ElementDown(PointerDownEvent evt)
{
float offset = evt.localPosition.x - dragger.resolvedStyle.width * 0.5f;
float max = element.resolvedStyle.width - dragger.resolvedStyle.width;
float value = Mathf.InverseLerp(0, max, offset);
UpdateValue(value);
}
/// <summary> 更新状态 </summary>
public virtual void Update()
{
if (!isDragger) { return; }
float differ = UITool.GetMousePosition().x - pointerPosition;
float offset = differ + originalPosition;
float max = element.resolvedStyle.width - dragger.resolvedStyle.width;
float value = Mathf.InverseLerp(0, max, offset);
UpdateValue(value);
}
/// <summary> 更新值(0-1) </summary>
public void UpdateValue(float value)
{
this.value = value;
ValueChanged?.Invoke(value);
float max = element.resolvedStyle.width - dragger.resolvedStyle.width;
float x = Mathf.Lerp(0, max, value);
dragger.transform.position = new Vector3(x, 0);
}
}
}
@@ -1,14 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua
{
/// <summary>
/// 滑块(垂直)
/// </summary>
public class UIScrollerVertical
{
}
}