This commit is contained in:
MuHua-123
2025-02-28 15:32:19 +08:00
parent 34d02c4015
commit 5bc834bfcd
10 changed files with 153 additions and 253 deletions
+2 -2
View File
@@ -1,8 +1,8 @@
{
"name": "muhua-finite-state-machine",
"name": "muhua-fsm",
"version": "1.0.0",
"displayName": "MuHua FSM",
"description": "\u6709\u9650\u72b6\u6001\u673a\u6846\u67b6",
"description": "FiniteStateMachine\u6709\u9650\u72b6\u6001\u673a\u6846\u67b6",
"author": {
"name": "MuHua",
"email": "2960208585@qq.com"
+10 -14
View File
@@ -6,28 +6,24 @@ namespace MuHua
{
public static LabelController Instance { get; private set; }
public Canvas canvas; // 包含标签的Canvas
public Transform parent; // 标签父物体
public GameObject labelPrefab; // 标签预制体
void Awake()
{
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(gameObject);
}
if (Instance == null) { Instance = this; }
else { Destroy(gameObject); }
}
public static GameObject CreateLabel(Transform target, Vector3 offset)
{
return CreateLabel(target, Instance.labelPrefab, offset);
}
// 启用标签
public static void Enable(bool enable) => Instance.parent.gameObject.SetActive(enable);
// 创建标签
public static GameObject CreateLabel(Transform target) => CreateLabel(target, Vector3.zero);
public static GameObject CreateLabel(Transform target, Vector3 offset) => CreateLabel(target, Instance.labelPrefab, offset);
public static GameObject CreateLabel(Transform target, GameObject labelPrefab, Vector3 offset)
{
GameObject labelObject = Instantiate(labelPrefab, Instance.canvas.transform);
GameObject labelObject = Instantiate(labelPrefab, Instance.parent);
LabelFollower followObjectLabel = labelObject.GetComponent<LabelFollower>();
followObjectLabel.target = target;
followObjectLabel.offset = offset;
@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: d9d7dc56eb286ef44869e5c4a543a365
guid: e94d558a7fcc0af4a83b561abcdb6e67
folderAsset: yes
DefaultImporter:
externalObjects: {}
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 004a039923c4e3845b77cdbaae4fae09
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
+1 -1
View File
@@ -12,7 +12,7 @@
{
"displayName": "Label Follow Example",
"description": "An example showing how to use the Label Follow system.",
"path": "Samples~"
"path": "Samples"
}
]
}
@@ -4,34 +4,45 @@ using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
namespace MuHua {
public class MUSliderHorizontal : VisualElement {
public enum RoundDataType {
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 {
public new class UxmlTraits : VisualElement.UxmlTraits
{
private UxmlStringAttributeDescription Text = new UxmlStringAttributeDescription
{
name = "text"
};
private UxmlFloatAttributeDescription MinValue = new UxmlFloatAttributeDescription {
private UxmlFloatAttributeDescription MinValue = new UxmlFloatAttributeDescription
{
name = "min-value"
};
private UxmlFloatAttributeDescription MaxValue = new UxmlFloatAttributeDescription {
private UxmlFloatAttributeDescription MaxValue = new UxmlFloatAttributeDescription
{
name = "max-value"
};
private UxmlFloatAttributeDescription SlidingValue = new UxmlFloatAttributeDescription {
private UxmlFloatAttributeDescription SlidingValue = new UxmlFloatAttributeDescription
{
name = "sliding-value"
};
private UxmlBoolAttributeDescription DisplayInput = new UxmlBoolAttributeDescription {
private UxmlBoolAttributeDescription DisplayInput = new UxmlBoolAttributeDescription
{
name = "display-input"
};
private UxmlEnumAttributeDescription<RoundDataType> DataType = new UxmlEnumAttributeDescription<RoundDataType> {
private UxmlEnumAttributeDescription<RoundDataType> DataType = new UxmlEnumAttributeDescription<RoundDataType>
{
name = "data-type"
};
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc) {
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);
@@ -50,31 +61,38 @@ namespace MuHua {
public VisualElement tracker = new VisualElement();
public VisualElement dragger = new VisualElement();
public string Text {
public string Text
{
get => labelElement.text;
set => UpdateLabelElement(value);
}
public float MinValue {
public float MinValue
{
get => minValue;
set { minValue = value; UpdateFloatField(); }
}
public float MaxValue {
public float MaxValue
{
get => maxValue;
set { maxValue = value; UpdateFloatField(); }
}
public float SlidingValue {
public float SlidingValue
{
get => slidingValue;
set => UpdateSlidingValue(value);
}
public bool DisplayInput {
public bool DisplayInput
{
get => isDisplayInput;
set => UpdateFloatField(value);
}
public RoundDataType DataType {
public RoundDataType DataType
{
get => dataType;
set { dataType = value; UpdateFloatField(); }
}
public float Value {
public float Value
{
get => UpdateValue();
set => UpdateValue(value);
}
@@ -91,42 +109,56 @@ namespace MuHua {
internal float MaxPosition { get => container.resolvedStyle.width; }
internal float CurrentPosition { get => slidingValue * container.resolvedStyle.width; }
internal void UpdateLabelElement(string value) {
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) {
internal void UpdateSlidingValue(float value)
{
UpdateDragger(value);
SlidingValueChanged?.Invoke(Value);
}
internal void UpdateDragger(float value) {
internal void UpdateDragger(float value)
{
slidingValue = value;
slidingValue = Mathf.Clamp(slidingValue, 0, 1);
tracker.style.width = CurrentPosition;
tracker.style.width = Length.Percent(slidingValue * 100);
UpdateFloatField();
}
internal void UpdateFloatField(bool value) {
internal void UpdateFloatField(ChangeEvent<float> evt)
{
float value = Mathf.Clamp(evt.newValue, MinValue, MaxValue);
slidingValue = Mathf.InverseLerp(MinValue, MaxValue, value);
tracker.style.width = Length.Percent(slidingValue * 100);
SlidingValueChanged?.Invoke(Value);
}
internal void UpdateFloatField(bool value)
{
isDisplayInput = value;
floatField.style.display = isDisplayInput ? DisplayStyle.Flex : DisplayStyle.None;
}
internal void UpdateFloatField() {
internal void UpdateFloatField()
{
floatField.value = Value;
}
internal float UpdateValue() {
float value = (MaxValue - MinValue) * SlidingValue;
internal float UpdateValue()
{
float value = Mathf.Lerp(MinValue, MaxValue, 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;
internal void UpdateValue(float value)
{
slidingValue = Mathf.InverseLerp(MinValue, MaxValue, value);
tracker.style.width = Length.Percent(slidingValue * 100);
UpdateFloatField();
}
public MUSliderHorizontal() {
public MUSliderHorizontal()
{
//设置名称
labelElement.name = "Label";
floatField.name = "FloatField";
@@ -165,20 +197,25 @@ namespace MuHua {
dragger.RegisterCallback<PointerUpEvent>((evt) => isDragger = false);
dragger.RegisterCallback<PointerLeaveEvent>((evt) => isDragger = false);
floatField.RegisterCallback<ChangeEvent<float>>(UpdateFloatField);
container.RegisterCallback<PointerDownEvent>(ContainerDown);
}
private void DraggerDown(PointerDownEvent evt) {
private void DraggerDown(PointerDownEvent evt)
{
isDragger = true;
mousePosition = evt.position.x;
originalPosition = CurrentPosition;
}
private void DraggerDrag(PointerMoveEvent evt) {
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) {
private void ContainerDown(PointerDownEvent evt)
{
float value = evt.localPosition.x / MaxPosition;
UpdateSlidingValue(value);
}
+1 -1
View File
@@ -215,7 +215,7 @@
},
"url": "https://packages.unity.cn"
},
"muhua-finite-state-machine": {
"muhua-fsm": {
"version": "file:FSM",
"depth": 0,
"source": "embedded",