| 123456789101112131415161718 |
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
- namespace DiamondArrow
- {
- // Move the face within its stable hit area, so a press never moves the touch target.
- public sealed class ButtonDepth : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler
- {
- RectTransform[] faces;Vector2[] origins;float offset;bool held;
- void Start(){int n=transform.childCount;faces=new RectTransform[n];origins=new Vector2[n];for(int i=0;i<n;i++){faces[i]=transform.GetChild(i) as RectTransform;if(faces[i])origins[i]=faces[i].anchoredPosition;}}
- void Update(){if(faces==null)return;offset=Mathf.Lerp(offset,held?2.5f:0,1-Mathf.Exp(-Time.unscaledDeltaTime*30));for(int i=0;i<faces.Length;i++)if(faces[i])faces[i].anchoredPosition=origins[i]+Vector2.down*offset;}
- public void OnPointerDown(PointerEventData e){held=GetComponent<Button>().IsInteractable();}
- public void OnPointerUp(PointerEventData e){held=false;}
- public void OnPointerExit(PointerEventData e){held=false;}
- void OnDisable(){held=false;offset=0;if(faces!=null)for(int i=0;i<faces.Length;i++)if(faces[i])faces[i].anchoredPosition=origins[i];}
- }
- }
|