2026-03-21 19:57:49 +08:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using TMPro;
|
|
|
|
|
|
2026-03-23 14:45:12 +08:00
|
|
|
namespace UnityEngine.UI
|
2026-03-21 19:57:49 +08:00
|
|
|
{
|
|
|
|
|
public sealed class UXBindingPropertyMetadata
|
|
|
|
|
{
|
|
|
|
|
public UXBindingPropertyMetadata(
|
|
|
|
|
UXBindingProperty property,
|
|
|
|
|
string displayName,
|
|
|
|
|
UXBindingValueKind valueKind,
|
|
|
|
|
Type objectReferenceType)
|
|
|
|
|
{
|
|
|
|
|
Property = property;
|
|
|
|
|
DisplayName = displayName;
|
|
|
|
|
ValueKind = valueKind;
|
|
|
|
|
ObjectReferenceType = objectReferenceType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UXBindingProperty Property { get; }
|
|
|
|
|
public string DisplayName { get; }
|
|
|
|
|
public UXBindingValueKind ValueKind { get; }
|
|
|
|
|
public Type ObjectReferenceType { get; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class UXBindingPropertyUtility
|
|
|
|
|
{
|
|
|
|
|
private static readonly UXBindingPropertyMetadata[] Metadata =
|
|
|
|
|
{
|
|
|
|
|
new UXBindingPropertyMetadata(UXBindingProperty.GameObjectActive, "GameObject/Active", UXBindingValueKind.Boolean, null),
|
|
|
|
|
new UXBindingPropertyMetadata(UXBindingProperty.CanvasGroupAlpha, "CanvasGroup/Alpha", UXBindingValueKind.Float, null),
|
|
|
|
|
new UXBindingPropertyMetadata(UXBindingProperty.CanvasGroupInteractable, "CanvasGroup/Interactable", UXBindingValueKind.Boolean, null),
|
|
|
|
|
new UXBindingPropertyMetadata(UXBindingProperty.CanvasGroupBlocksRaycasts, "CanvasGroup/Blocks Raycasts", UXBindingValueKind.Boolean, null),
|
|
|
|
|
new UXBindingPropertyMetadata(UXBindingProperty.GraphicColor, "Graphic/Color", UXBindingValueKind.Color, null),
|
|
|
|
|
new UXBindingPropertyMetadata(UXBindingProperty.GraphicMaterial, "Graphic/Material", UXBindingValueKind.ObjectReference, typeof(Material)),
|
|
|
|
|
new UXBindingPropertyMetadata(UXBindingProperty.ImageSprite, "Image/Sprite", UXBindingValueKind.ObjectReference, typeof(Sprite)),
|
|
|
|
|
new UXBindingPropertyMetadata(UXBindingProperty.TextContent, "Text/Content", UXBindingValueKind.String, null),
|
|
|
|
|
new UXBindingPropertyMetadata(UXBindingProperty.TextColor, "Text/Color", UXBindingValueKind.Color, null),
|
|
|
|
|
new UXBindingPropertyMetadata(UXBindingProperty.RectTransformAnchoredPosition, "RectTransform/Anchored Position", UXBindingValueKind.Vector2, null),
|
|
|
|
|
new UXBindingPropertyMetadata(UXBindingProperty.TransformLocalScale, "Transform/Local Scale", UXBindingValueKind.Vector3, null),
|
|
|
|
|
new UXBindingPropertyMetadata(UXBindingProperty.TransformLocalEulerAngles, "Transform/Local Rotation", UXBindingValueKind.Vector3, null)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public static IReadOnlyList<UXBindingPropertyMetadata> AllMetadata => Metadata;
|
|
|
|
|
|
|
|
|
|
public static UXBindingPropertyMetadata GetMetadata(UXBindingProperty property)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < Metadata.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
if (Metadata[i].Property == property)
|
|
|
|
|
{
|
|
|
|
|
return Metadata[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Metadata[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool IsSupported(GameObject target, UXBindingProperty property)
|
|
|
|
|
{
|
|
|
|
|
if (target == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (property)
|
|
|
|
|
{
|
|
|
|
|
case UXBindingProperty.GameObjectActive:
|
|
|
|
|
case UXBindingProperty.TransformLocalScale:
|
|
|
|
|
case UXBindingProperty.TransformLocalEulerAngles:
|
|
|
|
|
return true;
|
|
|
|
|
case UXBindingProperty.CanvasGroupAlpha:
|
|
|
|
|
case UXBindingProperty.CanvasGroupInteractable:
|
|
|
|
|
case UXBindingProperty.CanvasGroupBlocksRaycasts:
|
|
|
|
|
return target.GetComponent<CanvasGroup>() != null;
|
|
|
|
|
case UXBindingProperty.GraphicColor:
|
|
|
|
|
case UXBindingProperty.GraphicMaterial:
|
|
|
|
|
return target.GetComponent<Graphic>() != null;
|
|
|
|
|
case UXBindingProperty.ImageSprite:
|
|
|
|
|
return target.GetComponent<Image>() != null;
|
|
|
|
|
case UXBindingProperty.TextContent:
|
|
|
|
|
case UXBindingProperty.TextColor:
|
|
|
|
|
return target.GetComponent<Text>() != null || target.GetComponent<TextMeshProUGUI>() != null;
|
|
|
|
|
case UXBindingProperty.RectTransformAnchoredPosition:
|
|
|
|
|
return target.GetComponent<RectTransform>() != null;
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void GetSupportedProperties(GameObject target, List<UXBindingProperty> output)
|
|
|
|
|
{
|
|
|
|
|
output.Clear();
|
|
|
|
|
if (target == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < Metadata.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
UXBindingProperty property = Metadata[i].Property;
|
|
|
|
|
if (IsSupported(target, property))
|
|
|
|
|
{
|
|
|
|
|
output.Add(property);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void CaptureValue(GameObject target, UXBindingProperty property, UXBindingValue destination)
|
|
|
|
|
{
|
|
|
|
|
if (target == null || destination == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (property)
|
|
|
|
|
{
|
|
|
|
|
case UXBindingProperty.GameObjectActive:
|
|
|
|
|
destination.BoolValue = target.activeSelf;
|
|
|
|
|
return;
|
|
|
|
|
case UXBindingProperty.CanvasGroupAlpha:
|
|
|
|
|
destination.FloatValue = target.GetComponent<CanvasGroup>().alpha;
|
|
|
|
|
return;
|
|
|
|
|
case UXBindingProperty.CanvasGroupInteractable:
|
|
|
|
|
destination.BoolValue = target.GetComponent<CanvasGroup>().interactable;
|
|
|
|
|
return;
|
|
|
|
|
case UXBindingProperty.CanvasGroupBlocksRaycasts:
|
|
|
|
|
destination.BoolValue = target.GetComponent<CanvasGroup>().blocksRaycasts;
|
|
|
|
|
return;
|
|
|
|
|
case UXBindingProperty.GraphicColor:
|
|
|
|
|
destination.ColorValue = target.GetComponent<Graphic>().color;
|
|
|
|
|
return;
|
|
|
|
|
case UXBindingProperty.GraphicMaterial:
|
|
|
|
|
destination.ObjectValue = target.GetComponent<Graphic>().material;
|
|
|
|
|
return;
|
|
|
|
|
case UXBindingProperty.ImageSprite:
|
|
|
|
|
destination.ObjectValue = target.GetComponent<Image>().sprite;
|
|
|
|
|
return;
|
|
|
|
|
case UXBindingProperty.TextContent:
|
|
|
|
|
if (target.TryGetComponent<Text>(out Text text))
|
|
|
|
|
{
|
|
|
|
|
destination.StringValue = text.text;
|
|
|
|
|
}
|
|
|
|
|
else if (target.TryGetComponent<TextMeshProUGUI>(out TextMeshProUGUI tmp))
|
|
|
|
|
{
|
|
|
|
|
destination.StringValue = tmp.text;
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
case UXBindingProperty.TextColor:
|
|
|
|
|
if (target.TryGetComponent<Text>(out Text legacyText))
|
|
|
|
|
{
|
|
|
|
|
destination.ColorValue = legacyText.color;
|
|
|
|
|
}
|
|
|
|
|
else if (target.TryGetComponent<TextMeshProUGUI>(out TextMeshProUGUI tmpText))
|
|
|
|
|
{
|
|
|
|
|
destination.ColorValue = tmpText.color;
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
case UXBindingProperty.RectTransformAnchoredPosition:
|
|
|
|
|
destination.Vector2Value = target.GetComponent<RectTransform>().anchoredPosition;
|
|
|
|
|
return;
|
|
|
|
|
case UXBindingProperty.TransformLocalScale:
|
|
|
|
|
destination.Vector3Value = target.transform.localScale;
|
|
|
|
|
return;
|
|
|
|
|
case UXBindingProperty.TransformLocalEulerAngles:
|
|
|
|
|
destination.Vector3Value = target.transform.localEulerAngles;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void ApplyValue(GameObject target, UXBindingProperty property, UXBindingValue value)
|
|
|
|
|
{
|
|
|
|
|
if (target == null || value == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (property)
|
|
|
|
|
{
|
|
|
|
|
case UXBindingProperty.GameObjectActive:
|
|
|
|
|
target.SetActive(value.BoolValue);
|
|
|
|
|
return;
|
|
|
|
|
case UXBindingProperty.CanvasGroupAlpha:
|
|
|
|
|
target.GetComponent<CanvasGroup>().alpha = value.FloatValue;
|
|
|
|
|
return;
|
|
|
|
|
case UXBindingProperty.CanvasGroupInteractable:
|
|
|
|
|
target.GetComponent<CanvasGroup>().interactable = value.BoolValue;
|
|
|
|
|
return;
|
|
|
|
|
case UXBindingProperty.CanvasGroupBlocksRaycasts:
|
|
|
|
|
target.GetComponent<CanvasGroup>().blocksRaycasts = value.BoolValue;
|
|
|
|
|
return;
|
|
|
|
|
case UXBindingProperty.GraphicColor:
|
|
|
|
|
target.GetComponent<Graphic>().color = value.ColorValue;
|
|
|
|
|
return;
|
|
|
|
|
case UXBindingProperty.GraphicMaterial:
|
|
|
|
|
target.GetComponent<Graphic>().material = value.ObjectValue as Material;
|
|
|
|
|
return;
|
|
|
|
|
case UXBindingProperty.ImageSprite:
|
|
|
|
|
target.GetComponent<Image>().sprite = value.ObjectValue as Sprite;
|
|
|
|
|
return;
|
|
|
|
|
case UXBindingProperty.TextContent:
|
|
|
|
|
if (target.TryGetComponent<Text>(out Text text))
|
|
|
|
|
{
|
|
|
|
|
text.text = value.StringValue;
|
|
|
|
|
}
|
|
|
|
|
else if (target.TryGetComponent<TextMeshProUGUI>(out TextMeshProUGUI tmp))
|
|
|
|
|
{
|
|
|
|
|
tmp.text = value.StringValue;
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
case UXBindingProperty.TextColor:
|
|
|
|
|
if (target.TryGetComponent<Text>(out Text legacyText))
|
|
|
|
|
{
|
|
|
|
|
legacyText.color = value.ColorValue;
|
|
|
|
|
}
|
|
|
|
|
else if (target.TryGetComponent<TextMeshProUGUI>(out TextMeshProUGUI tmpText))
|
|
|
|
|
{
|
|
|
|
|
tmpText.color = value.ColorValue;
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
case UXBindingProperty.RectTransformAnchoredPosition:
|
|
|
|
|
target.GetComponent<RectTransform>().anchoredPosition = value.Vector2Value;
|
|
|
|
|
return;
|
|
|
|
|
case UXBindingProperty.TransformLocalScale:
|
|
|
|
|
target.transform.localScale = value.Vector3Value;
|
|
|
|
|
return;
|
|
|
|
|
case UXBindingProperty.TransformLocalEulerAngles:
|
|
|
|
|
target.transform.localEulerAngles = value.Vector3Value;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|