using DG.Tweening; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; using XRTool.Util; namespace XRTool.WorldUI { /// /// 空间按钮 /// public class XRButton : Button { [Range(0, 200f)] public float thickness = 50; /// /// 压缩比例,按下按钮时的压缩比例 /// [Range(0.05f, 1)] public float pressDis = 0.5f; [Range(1, 10)] public float pressTime = 3f; public bool isShowBack = false; public bool isShowBox = true; private XRIcon3D box; private Transform back; private RectTransform body; public bool isTweenOnClick = true; public XRIcon3D Box { get { if (!box) { box = UnityUtil.GetBreadthChild(transform, "BoxIcon3D"); } return box; } set => box = value; } public Transform Back { get { if (!back) { back = UnityUtil.GetBreadthChild(transform, "Back"); } return back; } set => back = value; } public RectTransform Body { get { if (!body) { body = GetComponent(); } return body; } set => body = value; } public override void OnPointerDown(PointerEventData eventData) { base.OnPointerDown(eventData); if (!isTweenOnClick) return; if (Box) { //Box.DOKill(); //Box.DOScaleZ(thickness * pressDis, (1 - pressDis) / pressTime / 3 * 2); Box.DoThickness(pressDis, (1 - pressDis) / pressTime / 3 * 2); } if (targetGraphic) { targetGraphic.transform.DOKill(); targetGraphic.transform.DOLocalMoveZ(-thickness * pressDis / 2, (1 - pressDis) / pressTime); } } public override void OnPointerUp(PointerEventData eventData) { base.OnPointerUp(eventData); if (!isTweenOnClick) return; if (Box) { //Box.DOKill(); //Box.DOScaleZ(thickness, (1 - pressDis) / pressTime / 3 * 2); Box.DoThickness(1, (1 - pressDis) / pressTime / 3 * 2); } if (targetGraphic) { targetGraphic.transform.DOKill(); targetGraphic.transform.DOLocalMoveZ(-thickness / 2, (1 - pressDis) / pressTime); } } /// /// 更新按钮的厚度 /// public void UpdateThickness() { Vector3 tmp = targetGraphic.rectTransform.anchoredPosition3D; tmp.z = -thickness / 2; targetGraphic.rectTransform.anchoredPosition3D = tmp; if (Box) { Box.SetThickness(thickness); } } protected override void OnRectTransformDimensionsChange() { base.OnRectTransformDimensionsChange(); AutoSetSize(); } public void AutoSetSize() { for (int i = 0; i < Body.childCount; i++) { var child = Body.GetChild(i); //as RectTransform; if (child is RectTransform) { (child as RectTransform).sizeDelta = Body.rect.size; } else { Vector3 tmp = child.localScale; tmp.x = Body.rect.size.x; tmp.y = Body.rect.size.y; child.localScale = tmp; } } } } }