合并代码
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 83b14667ab3d30c419e00ca29d617be8
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,30 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace MuHua {
|
||||
public class MUFloatField : FloatField {
|
||||
public new class UxmlFactory : UxmlFactory<MUFloatField, UxmlTraits> { }
|
||||
public new class UxmlTraits : FloatField.UxmlTraits { }
|
||||
public VisualElement inputElement;
|
||||
public VisualElement textElement;
|
||||
public MUFloatField() {
|
||||
ClearClassList();
|
||||
AddToClassList("input-field");
|
||||
|
||||
labelElement.ClearClassList();
|
||||
labelElement.AddToClassList("unity-text-element");
|
||||
labelElement.AddToClassList("input-field-label");
|
||||
|
||||
inputElement = this.Q<VisualElement>("unity-text-input");
|
||||
inputElement.ClearClassList();
|
||||
inputElement.AddToClassList("input-field-box");
|
||||
|
||||
textElement = inputElement.Q<VisualElement>("");
|
||||
textElement.ClearClassList();
|
||||
textElement.AddToClassList("unity-text-element");
|
||||
textElement.AddToClassList("input-field-text");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1723fbf2e9a4ff74c8fdbc5388ca21a1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,28 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace MuHua{
|
||||
public class MULongField : LongField {
|
||||
public new class UxmlFactory : UxmlFactory<MULongField, UxmlTraits> { }
|
||||
public new class UxmlTraits : LongField.UxmlTraits { }
|
||||
public MULongField() {
|
||||
ClearClassList();
|
||||
AddToClassList("input-field");
|
||||
|
||||
labelElement.ClearClassList();
|
||||
labelElement.AddToClassList("unity-text-element");
|
||||
labelElement.AddToClassList("input-field-label");
|
||||
|
||||
VisualElement inputElement = this.Q<VisualElement>("unity-text-input");
|
||||
inputElement.ClearClassList();
|
||||
inputElement.AddToClassList("input-field-box");
|
||||
|
||||
VisualElement textElement = inputElement.Q<VisualElement>("");
|
||||
textElement.ClearClassList();
|
||||
textElement.AddToClassList("unity-text-element");
|
||||
textElement.AddToClassList("input-field-text");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e80321332164d2b45b486ad26b5be961
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,55 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace MuHua {
|
||||
public class MUTextField : TextField {
|
||||
public new class UxmlFactory : UxmlFactory<MUTextField, UxmlTraits> { }
|
||||
public new class UxmlTraits : TextField.UxmlTraits {
|
||||
public UxmlStringAttributeDescription DefaultPrompt = new UxmlStringAttributeDescription {
|
||||
name = "default-prompt"
|
||||
};
|
||||
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc) {
|
||||
base.Init(ve, bag, cc);
|
||||
MUTextField textField = (MUTextField)ve;
|
||||
textField.DefaultPrompt = DefaultPrompt.GetValueFromBag(bag, cc);
|
||||
textField.SetDefaultPrompt();
|
||||
}
|
||||
}
|
||||
public string DefaultPrompt { get; set; }
|
||||
|
||||
public VisualElement inputElement => this.Q<VisualElement>("unity-text-input");
|
||||
public VisualElement textElement => inputElement.Q<VisualElement>("");
|
||||
|
||||
public MUTextField() {
|
||||
ClearClassList();
|
||||
AddToClassList("input-field");
|
||||
|
||||
labelElement.ClearClassList();
|
||||
labelElement.AddToClassList("unity-text-element");
|
||||
labelElement.AddToClassList("input-field-label");
|
||||
|
||||
inputElement.ClearClassList();
|
||||
inputElement.AddToClassList("input-field-box");
|
||||
|
||||
textElement.ClearClassList();
|
||||
textElement.AddToClassList("unity-text-element");
|
||||
textElement.AddToClassList("input-field-text");
|
||||
|
||||
RegisterCallback<FocusInEvent>((evt) => { PrepareInput(); });
|
||||
RegisterCallback<FocusOutEvent>((evt) => { SetDefaultPrompt(); });
|
||||
}
|
||||
public void PrepareInput() {
|
||||
textElement.RemoveFromClassList("input-field-text-d");
|
||||
if (text != DefaultPrompt) { return; }
|
||||
text = "";
|
||||
}
|
||||
public void SetDefaultPrompt() {
|
||||
textElement.RemoveFromClassList("input-field-text-d");
|
||||
if (value != "") { return; }
|
||||
text = DefaultPrompt;
|
||||
textElement.AddToClassList("input-field-text-d");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9d99552e73f58694f9dde4009dc6f0fb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,31 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace MuHua {
|
||||
public class MUDropdown : DropdownField {
|
||||
public new class UxmlFactory : UxmlFactory<MUDropdown, UxmlTraits> { }
|
||||
public new class UxmlTraits : DropdownField.UxmlTraits { }
|
||||
public MUDropdown() {
|
||||
ClearClassList();
|
||||
AddToClassList("dropdown");
|
||||
|
||||
labelElement.ClearClassList();
|
||||
labelElement.AddToClassList("unity-text-element");
|
||||
labelElement.AddToClassList("dropdown-label");
|
||||
|
||||
VisualElement inputElement = this.Query<VisualElement>(null, "unity-popup-field__input");
|
||||
inputElement.ClearClassList();
|
||||
inputElement.AddToClassList("dropdown-box");
|
||||
|
||||
VisualElement textElement = this.Query<VisualElement>(null, "unity-base-popup-field__text");
|
||||
textElement.ClearClassList();
|
||||
textElement.AddToClassList("unity-text-element");
|
||||
textElement.AddToClassList("dropdown-text");
|
||||
|
||||
VisualElement arrowElement = this.Query<VisualElement>(null, "unity-base-popup-field__arrow");
|
||||
arrowElement.AddToClassList("dropdown-arrow");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 17d0169e7671d5442a79e525ee388bd7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,95 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
#endif
|
||||
|
||||
namespace MuHua {
|
||||
public class MUFoldout : VisualElement {
|
||||
public new class UxmlFactory : UxmlFactory<MUFoldout, UxmlTraits> { }
|
||||
public new class UxmlTraits : VisualElement.UxmlTraits {
|
||||
private UxmlStringAttributeDescription Text = new UxmlStringAttributeDescription {
|
||||
name = "text",
|
||||
defaultValue = "标题"
|
||||
};
|
||||
private UxmlBoolAttributeDescription Active = new UxmlBoolAttributeDescription {
|
||||
name = "active",
|
||||
defaultValue = false
|
||||
};
|
||||
public UxmlIntAttributeDescription Count = new UxmlIntAttributeDescription {
|
||||
name = "count"
|
||||
};
|
||||
public UxmlStringAttributeDescription AssetPath = new UxmlStringAttributeDescription {
|
||||
name = "asset-path"
|
||||
};
|
||||
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc) {
|
||||
base.Init(ve, bag, cc);
|
||||
MUFoldout foldout = (MUFoldout)ve;
|
||||
foldout.Text = Text.GetValueFromBag(bag, cc);
|
||||
foldout.Active = Active.GetValueFromBag(bag, cc);
|
||||
foldout.AssetPath = AssetPath.GetValueFromBag(bag, cc);
|
||||
#if UNITY_EDITOR
|
||||
VisualTreeAsset asset = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(foldout.AssetPath);
|
||||
if (asset == null) { return; }
|
||||
foldout.ClearContainer();
|
||||
int count = Count.GetValueFromBag(bag, cc);
|
||||
for (int i = 0; i < count; i++) { foldout.AddContainer(asset); }
|
||||
#endif
|
||||
}
|
||||
}
|
||||
public Label labelElement = new Label();
|
||||
public VisualElement title = new VisualElement();
|
||||
public VisualElement image = new VisualElement();
|
||||
public VisualElement container = new VisualElement();
|
||||
|
||||
public string Text {
|
||||
get => labelElement.text;
|
||||
set => labelElement.text = value;
|
||||
}
|
||||
public bool Active {
|
||||
get => isActive;
|
||||
set => SetActive(value);
|
||||
}
|
||||
|
||||
public void AddContainer(VisualTreeAsset asset) {
|
||||
AddContainer(asset.Instantiate());
|
||||
}
|
||||
public void AddContainer(VisualElement element) {
|
||||
container.Add(element);
|
||||
}
|
||||
public void ClearContainer() {
|
||||
container.Clear();
|
||||
}
|
||||
|
||||
internal bool isActive;
|
||||
internal string AssetPath { get; set; }
|
||||
internal void SetActive(bool value) {
|
||||
isActive = value;
|
||||
container.style.display = Active ? DisplayStyle.Flex : DisplayStyle.None;
|
||||
}
|
||||
|
||||
public MUFoldout() {
|
||||
//设置名称
|
||||
title.name = "title";
|
||||
image.name = "image";
|
||||
labelElement.name = "label";
|
||||
container.name = "container";
|
||||
//设置USS类名
|
||||
AddToClassList("foldout");
|
||||
title.AddToClassList("foldout-title");
|
||||
image.AddToClassList("foldout-title-image");
|
||||
labelElement.AddToClassList("foldout-title-label");
|
||||
container.AddToClassList("foldout-container");
|
||||
//设置层级结构
|
||||
hierarchy.Add(title);
|
||||
hierarchy.Add(container);
|
||||
title.Add(image);
|
||||
title.Add(labelElement);
|
||||
//设置事件
|
||||
title.RegisterCallback<ClickEvent>((evt) => Active = !Active);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a638c4a7cfde51d4cbc15810d986605c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,186 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace MuHua {
|
||||
public class MUSliderHorizontal : VisualElement {
|
||||
public enum RoundDataType {
|
||||
保留两位小数 = 0,
|
||||
小数 = 1,
|
||||
整数 = 2,
|
||||
}
|
||||
public new class UxmlFactory : UxmlFactory<MUSliderHorizontal, UxmlTraits> { }
|
||||
public new class UxmlTraits : VisualElement.UxmlTraits {
|
||||
private UxmlStringAttributeDescription Text = new UxmlStringAttributeDescription {
|
||||
name = "text"
|
||||
};
|
||||
private UxmlFloatAttributeDescription MinValue = new UxmlFloatAttributeDescription {
|
||||
name = "min-value"
|
||||
};
|
||||
private UxmlFloatAttributeDescription MaxValue = new UxmlFloatAttributeDescription {
|
||||
name = "max-value"
|
||||
};
|
||||
private UxmlFloatAttributeDescription SlidingValue = new UxmlFloatAttributeDescription {
|
||||
name = "sliding-value"
|
||||
};
|
||||
private UxmlBoolAttributeDescription DisplayInput = new UxmlBoolAttributeDescription {
|
||||
name = "display-input"
|
||||
};
|
||||
private UxmlEnumAttributeDescription<RoundDataType> DataType = new UxmlEnumAttributeDescription<RoundDataType> {
|
||||
name = "data-type"
|
||||
};
|
||||
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc) {
|
||||
base.Init(ve, bag, cc);
|
||||
MUSliderHorizontal slider = (MUSliderHorizontal)ve;
|
||||
slider.Text = Text.GetValueFromBag(bag, cc);
|
||||
slider.MinValue = MinValue.GetValueFromBag(bag, cc);
|
||||
slider.MaxValue = MaxValue.GetValueFromBag(bag, cc);
|
||||
slider.SlidingValue = SlidingValue.GetValueFromBag(bag, cc);
|
||||
slider.DisplayInput = DisplayInput.GetValueFromBag(bag, cc);
|
||||
slider.DataType = DataType.GetValueFromBag(bag, cc);
|
||||
}
|
||||
}
|
||||
public event Action<float> SlidingValueChanged;
|
||||
public Label labelElement = new Label();
|
||||
public MUFloatField floatField = new MUFloatField();
|
||||
public VisualElement background = new VisualElement();
|
||||
public VisualElement container = new VisualElement();
|
||||
public VisualElement tracker = new VisualElement();
|
||||
public VisualElement dragger = new VisualElement();
|
||||
|
||||
public string Text {
|
||||
get => labelElement.text;
|
||||
set => UpdateLabelElement(value);
|
||||
}
|
||||
public float MinValue {
|
||||
get => minValue;
|
||||
set { minValue = value; UpdateFloatField(); }
|
||||
}
|
||||
public float MaxValue {
|
||||
get => maxValue;
|
||||
set { maxValue = value; UpdateFloatField(); }
|
||||
}
|
||||
public float SlidingValue {
|
||||
get => slidingValue;
|
||||
set => UpdateSlidingValue(value);
|
||||
}
|
||||
public bool DisplayInput {
|
||||
get => isDisplayInput;
|
||||
set => UpdateFloatField(value);
|
||||
}
|
||||
public RoundDataType DataType {
|
||||
get => dataType;
|
||||
set { dataType = value; UpdateFloatField(); }
|
||||
}
|
||||
public float Value {
|
||||
get => UpdateValue();
|
||||
set => UpdateValue(value);
|
||||
}
|
||||
|
||||
internal float minValue;
|
||||
internal float maxValue;
|
||||
internal float slidingValue;
|
||||
internal bool isDisplayInput;
|
||||
internal bool isDragger;
|
||||
internal float mousePosition;
|
||||
internal float originalPosition;
|
||||
public RoundDataType dataType;
|
||||
|
||||
internal float MaxPosition { get => container.resolvedStyle.width; }
|
||||
internal float CurrentPosition { get => slidingValue * container.resolvedStyle.width; }
|
||||
|
||||
internal void UpdateLabelElement(string value) {
|
||||
bool display = value != "" && value != null;
|
||||
labelElement.text = value;
|
||||
labelElement.style.display = display ? DisplayStyle.Flex : DisplayStyle.None;
|
||||
}
|
||||
internal void UpdateSlidingValue(float value) {
|
||||
UpdateDragger(value);
|
||||
SlidingValueChanged?.Invoke(Value);
|
||||
}
|
||||
internal void UpdateDragger(float value) {
|
||||
slidingValue = value;
|
||||
slidingValue = Mathf.Clamp(slidingValue, 0, 1);
|
||||
tracker.style.width = CurrentPosition;
|
||||
UpdateFloatField();
|
||||
}
|
||||
internal void UpdateFloatField(bool value) {
|
||||
isDisplayInput = value;
|
||||
floatField.style.display = isDisplayInput ? DisplayStyle.Flex : DisplayStyle.None;
|
||||
}
|
||||
internal void UpdateFloatField() {
|
||||
floatField.value = Value;
|
||||
}
|
||||
internal float UpdateValue() {
|
||||
float value = (MaxValue - MinValue) * SlidingValue;
|
||||
if (dataType == RoundDataType.保留两位小数) { value = (float)Math.Round(value, 2); }
|
||||
if (dataType == RoundDataType.整数) { value = Mathf.FloorToInt(value); }
|
||||
return Mathf.Clamp(value, MinValue, MaxValue);
|
||||
}
|
||||
internal void UpdateValue(float value) {
|
||||
slidingValue = value / (MaxValue - MinValue);
|
||||
slidingValue = Mathf.Clamp(slidingValue, 0, 1);
|
||||
tracker.style.width = CurrentPosition;
|
||||
UpdateFloatField();
|
||||
}
|
||||
|
||||
public MUSliderHorizontal() {
|
||||
//设置名称
|
||||
labelElement.name = "Label";
|
||||
floatField.name = "FloatField";
|
||||
background.name = "Background";
|
||||
container.name = "Container";
|
||||
tracker.name = "Tracker";
|
||||
dragger.name = "Dragger";
|
||||
//设置USS类名
|
||||
AddToClassList("horizontal-slider");
|
||||
labelElement.ClearClassList();
|
||||
labelElement.AddToClassList("unity-text-element");
|
||||
labelElement.AddToClassList("horizontal-slider-label");
|
||||
|
||||
background.AddToClassList("horizontal-slider-background");
|
||||
container.AddToClassList("horizontal-slider-container");
|
||||
tracker.AddToClassList("horizontal-slider-tracker");
|
||||
dragger.AddToClassList("horizontal-slider-dragger");
|
||||
|
||||
floatField.ClearClassList();
|
||||
floatField.AddToClassList("horizontal-slider-field");
|
||||
floatField.inputElement.ClearClassList();
|
||||
floatField.inputElement.AddToClassList("horizontal-slider-field-box");
|
||||
floatField.textElement.ClearClassList();
|
||||
floatField.textElement.AddToClassList("unity-text-element");
|
||||
floatField.textElement.AddToClassList("horizontal-slider-field-text");
|
||||
//设置层级结构
|
||||
hierarchy.Add(labelElement);
|
||||
hierarchy.Add(background);
|
||||
hierarchy.Add(floatField);
|
||||
background.Add(container);
|
||||
container.Add(tracker);
|
||||
tracker.Add(dragger);
|
||||
//设置事件
|
||||
dragger.RegisterCallback<PointerDownEvent>(DraggerDown);
|
||||
dragger.RegisterCallback<PointerMoveEvent>(DraggerDrag);
|
||||
dragger.RegisterCallback<PointerUpEvent>((evt) => isDragger = false);
|
||||
dragger.RegisterCallback<PointerLeaveEvent>((evt) => isDragger = false);
|
||||
|
||||
container.RegisterCallback<PointerDownEvent>(ContainerDown);
|
||||
}
|
||||
private void DraggerDown(PointerDownEvent evt) {
|
||||
isDragger = true;
|
||||
mousePosition = evt.position.x;
|
||||
originalPosition = CurrentPosition;
|
||||
}
|
||||
private void DraggerDrag(PointerMoveEvent evt) {
|
||||
if (!isDragger) { return; }
|
||||
float offset = evt.position.x - mousePosition;
|
||||
float value = (originalPosition + offset) / MaxPosition;
|
||||
UpdateSlidingValue(value);
|
||||
}
|
||||
private void ContainerDown(PointerDownEvent evt) {
|
||||
float value = evt.localPosition.x / MaxPosition;
|
||||
UpdateSlidingValue(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1fd74f9e0c7ede342a809b375fd4175b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace MuHua {
|
||||
public class MUToggle : VisualElement {
|
||||
public new class UxmlFactory : UxmlFactory<MUToggle, UxmlTraits> { }
|
||||
public new class UxmlTraits : VisualElement.UxmlTraits {
|
||||
private UxmlBoolAttributeDescription Value = new UxmlBoolAttributeDescription {
|
||||
name = "Value"
|
||||
};
|
||||
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc) {
|
||||
base.Init(ve, bag, cc);
|
||||
MUToggle toggle = (MUToggle)ve;
|
||||
toggle.Value = Value.GetValueFromBag(bag, cc);
|
||||
}
|
||||
}
|
||||
public event Action<bool> OnChange;
|
||||
public VisualElement checkmark = new VisualElement();
|
||||
/// <summary> 设置值 触发事件 </summary>
|
||||
public bool Value {
|
||||
get { return value; }
|
||||
set { SetValue(value); OnChange?.Invoke(value); }
|
||||
}
|
||||
/// <summary> 设置值不会触发事件 </summary>
|
||||
public void SetValue(bool value) {
|
||||
this.value = value;
|
||||
if (value) { checkmark.AddToClassList("toggle-checkmark-a"); }
|
||||
else { checkmark.RemoveFromClassList("toggle-checkmark-a"); }
|
||||
}
|
||||
|
||||
internal bool value;
|
||||
|
||||
public MUToggle() {
|
||||
//设置名称
|
||||
checkmark.name = "Checkmark";
|
||||
//设置USS类名
|
||||
AddToClassList("toggle");
|
||||
checkmark.AddToClassList("toggle-checkmark");
|
||||
//设置层级结构
|
||||
hierarchy.Add(checkmark);
|
||||
//设置事件
|
||||
checkmark.RegisterCallback<ClickEvent>((evt) => Value = !Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cb8deac54a479ee4595f01b00b8403f4
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"name": "MuHua.UIControl"
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f2f8bb6405747de46b4fdb1313592dd5
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ba211e11f722a6e4d985f60d27b7dfc7
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,65 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace MuHua {
|
||||
public class MUPopupPrompt : VisualElement {
|
||||
public new class UxmlFactory : UxmlFactory<MUPopupPrompt, UxmlTraits> { }
|
||||
public new class UxmlTraits : VisualElement.UxmlTraits {
|
||||
public UxmlStringAttributeDescription LabelText = new UxmlStringAttributeDescription {
|
||||
name = "Label-Text", defaultValue = "弹窗内容"
|
||||
};
|
||||
public UxmlStringAttributeDescription ButtonText = new UxmlStringAttributeDescription {
|
||||
name = "Button-Text", defaultValue = "确认"
|
||||
};
|
||||
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc) {
|
||||
base.Init(ve, bag, cc);
|
||||
MUPopupPrompt popupPrompt = ve as MUPopupPrompt;
|
||||
popupPrompt.labelText = LabelText.GetValueFromBag(bag, cc);
|
||||
popupPrompt.buttonText = ButtonText.GetValueFromBag(bag, cc);
|
||||
}
|
||||
}
|
||||
//布局
|
||||
public VisualElement background = new VisualElement();
|
||||
public VisualElement content = new VisualElement();
|
||||
public VisualElement bottom = new VisualElement();
|
||||
//组件
|
||||
public Label label = new Label();
|
||||
public Button button = new Button();
|
||||
//参数
|
||||
public Action callback;
|
||||
public string labelText { get => label.text; set => label.text = value; }
|
||||
public string buttonText { get => button.text; set => button.text = value; }
|
||||
|
||||
public MUPopupPrompt() {
|
||||
//清除原有样式
|
||||
label.ClearClassList();
|
||||
button.ClearClassList();
|
||||
//设置USS类名
|
||||
AddToClassList("popup-prompt");
|
||||
background.AddToClassList("popup-prompt-background");
|
||||
content.AddToClassList("popup-prompt-content");
|
||||
bottom.AddToClassList("popup-prompt-bottom");
|
||||
label.AddToClassList("unity-text-element");
|
||||
label.AddToClassList("popup-prompt-label");
|
||||
button.AddToClassList("unity-text-element");
|
||||
button.AddToClassList("popup-prompt-button");
|
||||
//设置层级结构
|
||||
hierarchy.Add(background);
|
||||
background.Add(content);
|
||||
background.Add(bottom);
|
||||
content.Add(label);
|
||||
bottom.Add(button);
|
||||
//设置事件
|
||||
button.RegisterCallback<ClickEvent>((evt) => Close());
|
||||
}
|
||||
public void Open(string text, Action action) {
|
||||
labelText = text; callback = action; visible = true;
|
||||
}
|
||||
public void Close() {
|
||||
callback?.Invoke(); callback = null; visible = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d03e3806ea383d14f8f2aadeb9c5cd00
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,90 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
#endif
|
||||
|
||||
namespace MuHua {
|
||||
public class MUPopupWindow : VisualElement {
|
||||
public new class UxmlFactory : UxmlFactory<MUPopupWindow, UxmlTraits> { }
|
||||
public new class UxmlTraits : VisualElement.UxmlTraits {
|
||||
public UxmlStringAttributeDescription TitleText = new UxmlStringAttributeDescription {
|
||||
name = "Title-Text", defaultValue = "标题"
|
||||
};
|
||||
public UxmlStringAttributeDescription ButtonText = new UxmlStringAttributeDescription {
|
||||
name = "Button-Text", defaultValue = "确认"
|
||||
};
|
||||
public UxmlStringAttributeDescription AssetPath = new UxmlStringAttributeDescription {
|
||||
name = "Asset-Path"
|
||||
};
|
||||
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc) {
|
||||
base.Init(ve, bag, cc);
|
||||
MUPopupWindow popup = ve as MUPopupWindow;
|
||||
popup.titleText = TitleText.GetValueFromBag(bag, cc);
|
||||
popup.buttonText = ButtonText.GetValueFromBag(bag, cc);
|
||||
|
||||
popup.assetPath = AssetPath.GetValueFromBag(bag, cc);
|
||||
#if UNITY_EDITOR
|
||||
VisualTreeAsset asset = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(popup.assetPath);
|
||||
if (asset != null) { popup.ReplaceContent(asset.Instantiate()); }
|
||||
else { popup.middle.Clear(); }
|
||||
#endif
|
||||
}
|
||||
}
|
||||
//布局
|
||||
public VisualElement background = new VisualElement();
|
||||
public VisualElement top = new VisualElement();
|
||||
public VisualElement middle = new VisualElement();
|
||||
public VisualElement bottom = new VisualElement();
|
||||
//组件
|
||||
public Label title = new Label();
|
||||
public Button button = new Button();
|
||||
//参数
|
||||
public Action callback;
|
||||
public string assetPath { get; set; }
|
||||
public string titleText { get => title.text; set => title.text = value; }
|
||||
public string buttonText { get => button.text; set => button.text = value; }
|
||||
|
||||
public MUPopupWindow() {
|
||||
//清除原有样式
|
||||
title.ClearClassList();
|
||||
button.ClearClassList();
|
||||
//设置USS类名
|
||||
AddToClassList("popup-window");
|
||||
background.AddToClassList("popup-window-background");
|
||||
top.AddToClassList("popup-window-top");
|
||||
middle.AddToClassList("popup-window-middle");
|
||||
bottom.AddToClassList("popup-window-bottom");
|
||||
title.AddToClassList("unity-text-element");
|
||||
title.AddToClassList("popup-window-title");
|
||||
button.AddToClassList("unity-text-element");
|
||||
button.AddToClassList("popup-window-button");
|
||||
//设置层级结构
|
||||
hierarchy.Add(background);
|
||||
background.Add(top);
|
||||
background.Add(middle);
|
||||
background.Add(bottom);
|
||||
top.Add(title);
|
||||
bottom.Add(button);
|
||||
//设置事件
|
||||
button.RegisterCallback<ClickEvent>((evt) => Close());
|
||||
}
|
||||
public void Open(Action action) {
|
||||
callback = action;
|
||||
visible = true;
|
||||
}
|
||||
public void Close() {
|
||||
callback?.Invoke();
|
||||
callback = null;
|
||||
visible = false;
|
||||
}
|
||||
public void ReplaceContent(VisualElement element) {
|
||||
middle.Clear();
|
||||
middle.Add(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b9f7a20f4d66c4a42840fbb21226ce48
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8cdf1edce1f3c1047a25a8845aee7f35
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,33 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
#endif
|
||||
|
||||
namespace MuHua {
|
||||
public class MUScrollView : VisualElement {
|
||||
public new class UxmlFactory : UxmlFactory<MUScrollView, UxmlTraits> { }
|
||||
public new class UxmlTraits : VisualElement.UxmlTraits {
|
||||
|
||||
}
|
||||
public VisualElement viewport = new VisualElement();
|
||||
public VisualElement container = new VisualElement();
|
||||
|
||||
public MUScrollView() {
|
||||
//设置名称
|
||||
viewport.name = "viewport";
|
||||
container.name = "container";
|
||||
//设置USS类名
|
||||
AddToClassList("scroll-view");
|
||||
viewport.AddToClassList("scroll-view-viewport");
|
||||
container.AddToClassList("scroll-view-container");
|
||||
//设置层级结构
|
||||
hierarchy.Add(viewport);
|
||||
viewport.Add(container);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1f44619e724604b47834e3639ffe2947
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,138 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
#endif
|
||||
|
||||
namespace MuHua {
|
||||
public class MUScrollViewHorizontal : VisualElement {
|
||||
public new class UxmlFactory : UxmlFactory<MUScrollViewHorizontal, UxmlTraits> { }
|
||||
public new class UxmlTraits : VisualElement.UxmlTraits {
|
||||
private UxmlIntAttributeDescription MouseWheelScrollSize = new UxmlIntAttributeDescription {
|
||||
name = "mouse-wheel-scroll-size",
|
||||
defaultValue = 18
|
||||
};
|
||||
private UxmlFloatAttributeDescription SlidingValue = new UxmlFloatAttributeDescription {
|
||||
name = "sliding-value",
|
||||
defaultValue = 0
|
||||
};
|
||||
public UxmlIntAttributeDescription Count = new UxmlIntAttributeDescription {
|
||||
name = "count"
|
||||
};
|
||||
public UxmlStringAttributeDescription AssetPath = new UxmlStringAttributeDescription {
|
||||
name = "asset-path"
|
||||
};
|
||||
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc) {
|
||||
base.Init(ve, bag, cc);
|
||||
MUScrollViewHorizontal scrollView = (MUScrollViewHorizontal)ve;
|
||||
scrollView.MouseWheelScrollSize = MouseWheelScrollSize.GetValueFromBag(bag, cc);
|
||||
scrollView.SlidingValue = SlidingValue.GetValueFromBag(bag, cc);
|
||||
scrollView.AssetPath = AssetPath.GetValueFromBag(bag, cc);
|
||||
#if UNITY_EDITOR
|
||||
VisualTreeAsset asset = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(scrollView.AssetPath);
|
||||
if (asset == null) { return; }
|
||||
scrollView.ClearContainer();
|
||||
int count = Count.GetValueFromBag(bag, cc);
|
||||
for (int i = 0; i < count; i++) { scrollView.AddContainer(asset); }
|
||||
#endif
|
||||
scrollView.scroller.MouseWheelScrollSize = scrollView.MouseWheelScrollSize;
|
||||
scrollView.ContainerRelease();
|
||||
}
|
||||
}
|
||||
public MUScrollerHorizontal scroller = new MUScrollerHorizontal();
|
||||
public VisualElement viewport = new VisualElement();
|
||||
public VisualElement container = new VisualElement();
|
||||
|
||||
public string AssetPath { get; set; }
|
||||
public float MouseWheelScrollSize { get; set; }
|
||||
public float SlidingValue { get; set; }
|
||||
|
||||
public void AddContainer(VisualTreeAsset asset) {
|
||||
AddContainer(asset.Instantiate());
|
||||
}
|
||||
public void AddContainer(VisualElement element) {
|
||||
container.Add(element);
|
||||
}
|
||||
public void ClearContainer() {
|
||||
container.Clear();
|
||||
}
|
||||
|
||||
internal bool isDragger;
|
||||
internal float mousePosition;
|
||||
|
||||
internal float ViewportWidth { get => viewport.resolvedStyle.width; }
|
||||
internal float ContainerWidth { get => container.resolvedStyle.width; }
|
||||
internal float MaxPosition { get => ComputeMaxPosition(); }
|
||||
internal float ComputeMaxPosition() {
|
||||
float value = ViewportWidth - ContainerWidth;
|
||||
return value <= 0 ? value : -1;
|
||||
}
|
||||
|
||||
internal void UpdateVisualElement(float value) {
|
||||
SlidingValue = value;
|
||||
container.transform.position = new Vector3(MaxPosition * SlidingValue, 0);
|
||||
scroller.UpdateVisualElement(SlidingValue);
|
||||
}
|
||||
|
||||
public MUScrollViewHorizontal() {
|
||||
//设置名称
|
||||
viewport.name = "viewport";
|
||||
container.name = "container";
|
||||
//设置USS类名
|
||||
AddToClassList("horizontal-scroll-view");
|
||||
viewport.AddToClassList("horizontal-scroll-view-viewport");
|
||||
container.AddToClassList("horizontal-scroll-view-container");
|
||||
|
||||
scroller.ClearClassList();
|
||||
scroller.AddToClassList("horizontal-scroll-view-scroller");
|
||||
|
||||
VisualElement dragger = scroller.dragger;
|
||||
dragger.ClearClassList();
|
||||
dragger.AddToClassList("horizontal-scroll-view-dragger");
|
||||
//设置层级结构
|
||||
hierarchy.Add(viewport);
|
||||
hierarchy.Add(scroller);
|
||||
viewport.Add(container);
|
||||
//设置事件
|
||||
scroller.SlidingValueChanged += UpdateVisualElement;
|
||||
container.RegisterCallback<PointerDownEvent>(ContainerDown);
|
||||
container.RegisterCallback<PointerMoveEvent>(ContainerDrag);
|
||||
container.RegisterCallback<PointerUpEvent>((evt) => ContainerRelease());
|
||||
container.RegisterCallback<PointerLeaveEvent>((evt) => ContainerRelease());
|
||||
container.RegisterCallback<WheelEvent>(ContainerWheel);
|
||||
container.generateVisualContent += ContainerGenerateVisualContent;
|
||||
}
|
||||
private void ContainerDown(PointerDownEvent evt) {
|
||||
isDragger = true;
|
||||
mousePosition = evt.position.x - SlidingValue * MaxPosition;
|
||||
}
|
||||
private void ContainerDrag(PointerMoveEvent evt) {
|
||||
if (!isDragger) { return; }
|
||||
float offset = evt.position.x - mousePosition;
|
||||
float value = offset / MaxPosition;
|
||||
UpdateVisualElement(value);
|
||||
mousePosition = evt.position.x - SlidingValue * MaxPosition;
|
||||
}
|
||||
private void ContainerRelease() {
|
||||
isDragger = false;
|
||||
float value = Mathf.Clamp01(SlidingValue);
|
||||
container.schedule.Execute(() => { UpdateVisualElement(value); }).StartingIn(1);
|
||||
}
|
||||
private void ContainerWheel(WheelEvent evt) {
|
||||
float wheel = Mathf.Clamp(evt.delta.y, -1, 1);
|
||||
float current = SlidingValue * MaxPosition;
|
||||
float offset = current - wheel * MouseWheelScrollSize;
|
||||
float value = offset / MaxPosition;
|
||||
UpdateVisualElement(value);
|
||||
ContainerRelease();
|
||||
}
|
||||
private void ContainerGenerateVisualContent(MeshGenerationContext mgc) {
|
||||
float ratio = Mathf.Clamp01(ViewportWidth / ContainerWidth);
|
||||
scroller.UpdateDragger(ratio);
|
||||
ContainerRelease();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6d083933c2f0cfa448e7c5a5f22c253c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,139 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
#endif
|
||||
|
||||
namespace MuHua {
|
||||
public class MUScrollViewVertical : VisualElement {
|
||||
public new class UxmlFactory : UxmlFactory<MUScrollViewVertical, UxmlTraits> { }
|
||||
public new class UxmlTraits : VisualElement.UxmlTraits {
|
||||
private UxmlIntAttributeDescription MouseWheelScrollSize = new UxmlIntAttributeDescription {
|
||||
name = "mouse-wheel-scroll-size",
|
||||
defaultValue = 18
|
||||
};
|
||||
private UxmlFloatAttributeDescription SlidingValue = new UxmlFloatAttributeDescription {
|
||||
name = "sliding-value",
|
||||
defaultValue = 0
|
||||
};
|
||||
public UxmlIntAttributeDescription Count = new UxmlIntAttributeDescription {
|
||||
name = "count"
|
||||
};
|
||||
public UxmlStringAttributeDescription AssetPath = new UxmlStringAttributeDescription {
|
||||
name = "asset-path"
|
||||
};
|
||||
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc) {
|
||||
base.Init(ve, bag, cc);
|
||||
MUScrollViewVertical scrollView = (MUScrollViewVertical)ve;
|
||||
scrollView.MouseWheelScrollSize = MouseWheelScrollSize.GetValueFromBag(bag, cc);
|
||||
scrollView.SlidingValue = SlidingValue.GetValueFromBag(bag, cc);
|
||||
scrollView.AssetPath = AssetPath.GetValueFromBag(bag, cc);
|
||||
#if UNITY_EDITOR
|
||||
VisualTreeAsset asset = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(scrollView.AssetPath);
|
||||
if (asset == null) { return; }
|
||||
scrollView.ClearContainer();
|
||||
int count = Count.GetValueFromBag(bag, cc);
|
||||
for (int i = 0; i < count; i++) { scrollView.AddContainer(asset); }
|
||||
#endif
|
||||
scrollView.scroller.MouseWheelScrollSize = scrollView.MouseWheelScrollSize;
|
||||
scrollView.ContainerRelease();
|
||||
}
|
||||
}
|
||||
public MUScrollerVertical scroller = new MUScrollerVertical();
|
||||
public VisualElement viewport = new VisualElement();
|
||||
public VisualElement container = new VisualElement();
|
||||
|
||||
public string AssetPath { get; set; }
|
||||
public float MouseWheelScrollSize { get; set; }
|
||||
public float SlidingValue { get; set; }
|
||||
|
||||
public void AddContainer(VisualTreeAsset asset) {
|
||||
AddContainer(asset.Instantiate());
|
||||
}
|
||||
public void AddContainer(VisualElement element) {
|
||||
container.Add(element);
|
||||
}
|
||||
public void ClearContainer() {
|
||||
container.Clear();
|
||||
}
|
||||
|
||||
internal bool isDragger;
|
||||
internal float mousePosition;
|
||||
|
||||
internal float ViewportHeight { get => viewport.resolvedStyle.height; }
|
||||
internal float ContainerHeight { get => container.resolvedStyle.height; }
|
||||
internal float MaxPosition { get => ComputeMaxPosition(); }
|
||||
internal float ComputeMaxPosition() {
|
||||
float value = ViewportHeight - ContainerHeight;
|
||||
return value <= 0 ? value : -1;
|
||||
}
|
||||
|
||||
internal void UpdateVisualElement(float value) {
|
||||
SlidingValue = value;
|
||||
container.transform.position = new Vector3(0, MaxPosition * SlidingValue);
|
||||
scroller.UpdateVisualElement(SlidingValue);
|
||||
}
|
||||
|
||||
public MUScrollViewVertical() {
|
||||
//设置名称
|
||||
viewport.name = "viewport";
|
||||
container.name = "container";
|
||||
//设置USS类名
|
||||
AddToClassList("vertical-scroll-view");
|
||||
viewport.AddToClassList("vertical-scroll-view-viewport");
|
||||
container.AddToClassList("vertical-scroll-view-container");
|
||||
|
||||
scroller.ClearClassList();
|
||||
scroller.AddToClassList("vertical-scroll-view-scroller");
|
||||
|
||||
VisualElement dragger = scroller.dragger;
|
||||
dragger.ClearClassList();
|
||||
dragger.AddToClassList("vertical-scroll-view-dragger");
|
||||
//设置层级结构
|
||||
hierarchy.Add(viewport);
|
||||
hierarchy.Add(scroller);
|
||||
viewport.Add(container);
|
||||
//设置事件
|
||||
scroller.SlidingValueChanged += UpdateVisualElement;
|
||||
container.RegisterCallback<PointerDownEvent>(ContainerDown);
|
||||
container.RegisterCallback<PointerMoveEvent>(ContainerDrag);
|
||||
container.RegisterCallback<PointerUpEvent>((evt) => ContainerRelease());
|
||||
container.RegisterCallback<PointerLeaveEvent>((evt) => ContainerRelease());
|
||||
container.RegisterCallback<WheelEvent>(ContainerWheel);
|
||||
container.generateVisualContent += ContainerGenerateVisualContent;
|
||||
}
|
||||
private void ContainerDown(PointerDownEvent evt) {
|
||||
isDragger = true;
|
||||
mousePosition = evt.position.y - SlidingValue * MaxPosition;
|
||||
}
|
||||
private void ContainerDrag(PointerMoveEvent evt) {
|
||||
if (!isDragger) { return; }
|
||||
float offset = evt.position.y - mousePosition;
|
||||
float value = offset / MaxPosition;
|
||||
UpdateVisualElement(value);
|
||||
mousePosition = evt.position.y - SlidingValue * MaxPosition;
|
||||
}
|
||||
private void ContainerRelease() {
|
||||
isDragger = false;
|
||||
float value = Mathf.Clamp01(SlidingValue);
|
||||
container.schedule.Execute(() => { UpdateVisualElement(value); }).StartingIn(1);
|
||||
}
|
||||
private void ContainerWheel(WheelEvent evt) {
|
||||
float wheel = Mathf.Clamp(evt.delta.y, -1, 1);
|
||||
float current = SlidingValue * MaxPosition;
|
||||
float offset = current - wheel * MouseWheelScrollSize;
|
||||
float value = offset / MaxPosition;
|
||||
UpdateVisualElement(value);
|
||||
ContainerRelease();
|
||||
}
|
||||
private void ContainerGenerateVisualContent(MeshGenerationContext mgc) {
|
||||
float ratio = Mathf.Clamp01(ViewportHeight / ContainerHeight);
|
||||
scroller.UpdateDragger(ratio);
|
||||
ContainerRelease();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e8d4926c5a75b394b843a5630d5005f7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,105 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace MuHua {
|
||||
public class MUScrollerHorizontal : VisualElement {
|
||||
public new class UxmlFactory : UxmlFactory<MUScrollerHorizontal, UxmlTraits> { }
|
||||
public new class UxmlTraits : VisualElement.UxmlTraits {
|
||||
private UxmlIntAttributeDescription MouseWheelScrollSize = new UxmlIntAttributeDescription {
|
||||
name = "mouse-wheel-scroll-size",
|
||||
defaultValue = 18
|
||||
};
|
||||
private UxmlFloatAttributeDescription SlidingValue = new UxmlFloatAttributeDescription {
|
||||
name = "sliding-value",
|
||||
defaultValue = 0
|
||||
};
|
||||
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc) {
|
||||
base.Init(ve, bag, cc);
|
||||
MUScrollerHorizontal scroller = (MUScrollerHorizontal)ve;
|
||||
scroller.MouseWheelScrollSize = MouseWheelScrollSize.GetValueFromBag(bag, cc);
|
||||
scroller.SlidingValue = SlidingValue.GetValueFromBag(bag, cc);
|
||||
scroller.ElasticRestoration();
|
||||
}
|
||||
}
|
||||
public event Action<float> SlidingValueChanged;
|
||||
public VisualElement dragger = new VisualElement();
|
||||
|
||||
public float MouseWheelScrollSize { get; set; }
|
||||
public float SlidingValue { get; set; }
|
||||
|
||||
internal bool isDragger;
|
||||
internal float mousePosition;
|
||||
|
||||
internal float ViewportWidth { get => resolvedStyle.width; }
|
||||
internal float ContainerWidth { get => dragger.resolvedStyle.width; }
|
||||
internal float MaxPosition { get => ComputeMaxPosition(); }
|
||||
internal float ComputeMaxPosition() {
|
||||
float value = ViewportWidth - ContainerWidth;
|
||||
return value >= 0 ? value : -1;
|
||||
}
|
||||
|
||||
internal void UpdateDragger(float ratio) {
|
||||
dragger.style.width = ratio * resolvedStyle.width;
|
||||
}
|
||||
internal void UpdateVisualElement(float value, bool callback = false) {
|
||||
SlidingValue = Mathf.Clamp(value, 0, 1);
|
||||
dragger.transform.position = new Vector3(MaxPosition * SlidingValue, 0);
|
||||
if (callback) { SlidingValueChanged?.Invoke(SlidingValue); }
|
||||
}
|
||||
internal void ElasticRestoration() {
|
||||
dragger.schedule.Execute(() => { UpdateVisualElement(SlidingValue); }).StartingIn(1);
|
||||
}
|
||||
|
||||
public MUScrollerHorizontal() {
|
||||
//设置名称
|
||||
dragger.name = "dragger";
|
||||
//设置USS类名
|
||||
AddToClassList("horizontal-scroller");
|
||||
dragger.AddToClassList("horizontal-scroller-dragger");
|
||||
//设置层级结构
|
||||
hierarchy.Add(dragger);
|
||||
//设置事件
|
||||
dragger.RegisterCallback<PointerDownEvent>(DraggerDown);
|
||||
dragger.RegisterCallback<PointerMoveEvent>(DraggerDrag);
|
||||
dragger.generateVisualContent += DraggerGenerateVisualContent;
|
||||
|
||||
RegisterCallback<PointerDownEvent>(ScrollerDown);
|
||||
RegisterCallback<WheelEvent>(ScrollerWheel);
|
||||
RegisterCallback<PointerUpEvent>((evt) => ScrollerRelease());
|
||||
RegisterCallback<PointerLeaveEvent>((evt) => ScrollerRelease());
|
||||
}
|
||||
private void DraggerDown(PointerDownEvent evt) {
|
||||
isDragger = true;
|
||||
mousePosition = evt.position.x - SlidingValue * MaxPosition;
|
||||
}
|
||||
private void DraggerDrag(PointerMoveEvent evt) {
|
||||
if (!isDragger) { return; }
|
||||
float offset = evt.position.x - mousePosition;
|
||||
float value = offset / MaxPosition;
|
||||
UpdateVisualElement(value, true);
|
||||
mousePosition = evt.position.x - SlidingValue * MaxPosition;
|
||||
}
|
||||
private void DraggerGenerateVisualContent(MeshGenerationContext mgc) {
|
||||
ElasticRestoration();
|
||||
}
|
||||
|
||||
private void ScrollerDown(PointerDownEvent evt) {
|
||||
float offset = evt.localPosition.x - dragger.resolvedStyle.width * 0.5f;
|
||||
float value = offset / MaxPosition;
|
||||
UpdateVisualElement(value, true);
|
||||
}
|
||||
private void ScrollerWheel(WheelEvent evt) {
|
||||
float wheel = Mathf.Clamp(evt.delta.y, -1, 1);
|
||||
float current = SlidingValue * MaxPosition;
|
||||
float offset = current + wheel * MouseWheelScrollSize;
|
||||
float value = offset / MaxPosition;
|
||||
UpdateVisualElement(value, true);
|
||||
}
|
||||
private void ScrollerRelease() {
|
||||
isDragger = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e9c31f78aa7665041a4858f2db3b5c07
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,105 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace MuHua {
|
||||
public class MUScrollerVertical : VisualElement {
|
||||
public new class UxmlFactory : UxmlFactory<MUScrollerVertical, UxmlTraits> { }
|
||||
public new class UxmlTraits : VisualElement.UxmlTraits {
|
||||
private UxmlIntAttributeDescription MouseWheelScrollSize = new UxmlIntAttributeDescription {
|
||||
name = "mouse-wheel-scroll-size",
|
||||
defaultValue = 18
|
||||
};
|
||||
private UxmlFloatAttributeDescription SlidingValue = new UxmlFloatAttributeDescription {
|
||||
name = "sliding-value",
|
||||
defaultValue = 0
|
||||
};
|
||||
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc) {
|
||||
base.Init(ve, bag, cc);
|
||||
MUScrollerVertical scroller = (MUScrollerVertical)ve;
|
||||
scroller.MouseWheelScrollSize = MouseWheelScrollSize.GetValueFromBag(bag, cc);
|
||||
scroller.SlidingValue = SlidingValue.GetValueFromBag(bag, cc);
|
||||
scroller.ElasticRestoration();
|
||||
}
|
||||
}
|
||||
public event Action<float> SlidingValueChanged;
|
||||
public VisualElement dragger = new VisualElement();
|
||||
|
||||
public float MouseWheelScrollSize { get; set; }
|
||||
public float SlidingValue { get; set; }
|
||||
|
||||
internal bool isDragger;
|
||||
internal float mousePosition;
|
||||
|
||||
internal float ViewportHeight { get => resolvedStyle.height; }
|
||||
internal float ContainerHeight { get => dragger.resolvedStyle.height; }
|
||||
internal float MaxPosition { get => ComputeMaxPosition(); }
|
||||
internal float ComputeMaxPosition() {
|
||||
float value = ViewportHeight - ContainerHeight;
|
||||
return value >= 0 ? value : -1;
|
||||
}
|
||||
|
||||
internal void UpdateDragger(float ratio) {
|
||||
dragger.style.height = ratio * resolvedStyle.height;
|
||||
}
|
||||
internal void UpdateVisualElement(float value, bool callback = false) {
|
||||
SlidingValue = Mathf.Clamp(value, 0, 1);
|
||||
dragger.transform.position = new Vector3(0, MaxPosition * SlidingValue);
|
||||
if (callback) { SlidingValueChanged?.Invoke(SlidingValue); }
|
||||
}
|
||||
internal void ElasticRestoration() {
|
||||
dragger.schedule.Execute(() => { UpdateVisualElement(SlidingValue); }).StartingIn(1);
|
||||
}
|
||||
|
||||
public MUScrollerVertical() {
|
||||
//设置名称
|
||||
dragger.name = "dragger";
|
||||
//设置USS类名
|
||||
AddToClassList("vertical-scroller");
|
||||
dragger.AddToClassList("vertical-scroller-dragger");
|
||||
//设置层级结构
|
||||
hierarchy.Add(dragger);
|
||||
//设置事件
|
||||
dragger.RegisterCallback<PointerDownEvent>(DraggerDown);
|
||||
dragger.RegisterCallback<PointerMoveEvent>(DraggerDrag);
|
||||
dragger.generateVisualContent += DraggerGenerateVisualContent;
|
||||
|
||||
RegisterCallback<PointerDownEvent>(ScrollerDown);
|
||||
RegisterCallback<WheelEvent>(ScrollerWheel);
|
||||
RegisterCallback<PointerUpEvent>((evt) => ScrollerRelease());
|
||||
RegisterCallback<PointerLeaveEvent>((evt) => ScrollerRelease());
|
||||
}
|
||||
private void DraggerDown(PointerDownEvent evt) {
|
||||
isDragger = true;
|
||||
mousePosition = evt.position.y - SlidingValue * MaxPosition;
|
||||
}
|
||||
private void DraggerDrag(PointerMoveEvent evt) {
|
||||
if (!isDragger) { return; }
|
||||
float offset = evt.position.y - mousePosition;
|
||||
float value = offset / MaxPosition;
|
||||
UpdateVisualElement(value, true);
|
||||
mousePosition = evt.position.y - SlidingValue * MaxPosition;
|
||||
}
|
||||
private void DraggerGenerateVisualContent(MeshGenerationContext mgc) {
|
||||
ElasticRestoration();
|
||||
}
|
||||
|
||||
private void ScrollerDown(PointerDownEvent evt) {
|
||||
float offset = evt.localPosition.y - dragger.resolvedStyle.height * 0.5f;
|
||||
float value = offset / MaxPosition;
|
||||
UpdateVisualElement(value, true);
|
||||
}
|
||||
private void ScrollerWheel(WheelEvent evt) {
|
||||
float wheel = Mathf.Clamp(evt.delta.y, -1, 1);
|
||||
float current = SlidingValue * MaxPosition;
|
||||
float offset = current + wheel * MouseWheelScrollSize;
|
||||
float value = offset / MaxPosition;
|
||||
UpdateVisualElement(value, true);
|
||||
}
|
||||
private void ScrollerRelease() {
|
||||
isDragger = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 170e690428c828a4bbfad60ded902e45
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: af81a61bb7f5221428d4d976cff44866
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,15 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace MuHua {
|
||||
public class UIViewExtend<ViewModel> : MonoBehaviour where ViewModel : UIViewModel {
|
||||
public ViewModel view;
|
||||
public virtual VisualElement element => view.Q<VisualElement>();
|
||||
|
||||
public T Q<T>(string name = null, string className = null) where T : VisualElement {
|
||||
return element.Q<T>(name, className);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c3f91d8edeffb1143b3eb547a09cc321
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,15 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace MuHua {
|
||||
public class UIViewModel : MonoBehaviour {
|
||||
public UIDocument document;
|
||||
public VisualElement element => document.rootVisualElement;
|
||||
|
||||
public T Q<T>(string name = null, string className = null) where T : VisualElement {
|
||||
return element.Q<T>(name, className);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f15a2fe11989e884d9294ac8577594a8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,15 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace MuHua {
|
||||
public class UIViewTemplate<Data> {
|
||||
public Data value;
|
||||
public VisualElement element;
|
||||
public virtual void SetValue(VisualTreeAsset asset, Data value) {
|
||||
this.value = value;
|
||||
this.element = asset.Instantiate();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1962450aff64953408148b16dd9ff69f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace MuHua {
|
||||
public static class UIViewTool {
|
||||
public static List<Template> Instantiate<Template, V>(this VisualElement parent, VisualTreeAsset template, V[] list) where Template : UIViewTemplate<V>, new() {
|
||||
List<Template> templates = new List<Template>();
|
||||
for (int i = 0; i < list.Length; i++) {
|
||||
templates.Add(parent.Instantiate<Template, V>(template, list[i]));
|
||||
}
|
||||
return templates;
|
||||
}
|
||||
public static List<Template> Instantiate<Template, V>(this VisualElement parent, VisualTreeAsset template, List<V> list) where Template : UIViewTemplate<V>, new() {
|
||||
List<Template> templates = new List<Template>();
|
||||
for (int i = 0; i < list.Count; i++) {
|
||||
templates.Add(parent.Instantiate<Template, V>(template, list[i]));
|
||||
}
|
||||
return templates;
|
||||
}
|
||||
public static Template Instantiate<Template, V>(this VisualElement parent, VisualTreeAsset template, V value) where Template : UIViewTemplate<V>, new() {
|
||||
Template temp = new Template();
|
||||
temp.SetValue(template, value);
|
||||
parent.Add(temp.element);
|
||||
return temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f47b238dc04f6f243b00dd9ab82419bd
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user