123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- 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
- {
- /// <summary>
- /// 空间按钮
- /// </summary>
- public class XRButton : Button
- {
- [Range(0, 200f)]
- public float thickness = 50;
- /// <summary>
- /// 压缩比例,按下按钮时的压缩比例
- /// </summary>
- [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<XRIcon3D>(transform, "BoxIcon3D");
- }
- return box;
- }
- set => box = value;
- }
- public Transform Back
- {
- get
- {
- if (!back)
- {
- back = UnityUtil.GetBreadthChild<Transform>(transform, "Back");
- }
- return back;
- }
- set => back = value;
- }
- public RectTransform Body
- {
- get
- {
- if (!body)
- {
- body = GetComponent<RectTransform>();
- }
- 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);
- }
- }
- /// <summary>
- /// 更新按钮的厚度
- /// </summary>
- 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;
- }
- }
- }
- }
- }
|