Guest User

CardInteractionCode

a guest
Oct 1st, 2025
9
0
13 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.36 KB | Software | 0 0
  1. using UnityEngine;
  2. using UnityEngine.EventSystems;
  3. using UnityEngine.InputSystem;
  4. using Dogfight;
  5.  
  6. public class CardInteraction : MonoBehaviour, IDragHandler, IPointerDownHandler, IPointerEnterHandler,IPointerExitHandler
  7. {
  8.     private RectTransform rectTransform;
  9.     private Canvas canvas;
  10.     private Vector2 originalLocalPointerPosition;
  11.     private Vector3 originalPanelLocalPosition;
  12.     private Vector3 originalScale;
  13.     private Vector3 originalPosition;
  14.     private Quaternion originalRotation;
  15.     private int currentState = 0;
  16.     [Range(0.1f, 2f)] public float movementSensitivity = 1.0f;
  17.  
  18.     [SerializeField] private float selectScale = 1.1f;
  19.     [SerializeField] private Vector2 cardPlay;
  20.     [SerializeField] private Vector3 playPosition;
  21.     [SerializeField] private GameObject highlight;
  22.     [SerializeField] private GameObject playArrow;
  23.  
  24.     void Awake()
  25.     {
  26.         rectTransform = GetComponent<RectTransform>();
  27.         canvas = GetComponentInParent<Canvas>();
  28.  
  29.         originalScale    = rectTransform.localScale;
  30.         originalPosition = rectTransform.localPosition;
  31.         originalRotation = rectTransform.localRotation;
  32.     }
  33.  
  34.     void Update()
  35.     {
  36.         switch (currentState)
  37.         {
  38.             case 1:
  39.                 HandleHoverState();
  40.                 break;
  41.             case 2:
  42.                 HandleDragState();
  43.                 if (!Mouse.current.leftButton.isPressed) ResetState();
  44.                 break;
  45.             case 3:
  46.                 HandlePlayState();
  47.                 if (!Mouse.current.leftButton.isPressed) ResetState();
  48.                 break;
  49.         }
  50.     }
  51.  
  52.     private void ResetState()
  53.     {
  54.         currentState = 0;
  55.         rectTransform.localPosition = originalPosition;//Vector3.Lerp(rectTransform.localPosition, originalPosition, 1f);
  56.         rectTransform.localRotation = originalRotation;
  57.         rectTransform.localScale    = originalScale;
  58.         highlight.SetActive(false);
  59.         playArrow.SetActive(false);
  60.     }
  61.  
  62.     public void OnPointerEnter(PointerEventData eventData)
  63.     {
  64.         if (currentState == 0)
  65.         {
  66.             originalPosition = rectTransform.localPosition;
  67.             originalRotation = rectTransform.localRotation;
  68.             originalScale    = rectTransform.localScale;
  69.  
  70.             currentState = 1;
  71.         }
  72.     }
  73.  
  74.     public void OnPointerExit(PointerEventData eventData)
  75.     {
  76.         if (currentState == 1)
  77.         {
  78.             ResetState();
  79.             // MORE TO COME
  80.         }
  81.     }
  82.  
  83.     public void OnPointerDown(PointerEventData eventData)
  84.     {
  85.         if (currentState == 1)
  86.         {
  87.             currentState = 2;
  88.             RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.GetComponent<RectTransform>(), eventData.position, eventData.pressEventCamera, out originalLocalPointerPosition);
  89.             originalPanelLocalPosition = rectTransform.localPosition;
  90.             // MORE TO COME
  91.         }
  92.     }
  93.  
  94.     public void OnDrag(PointerEventData eventData)
  95.     {
  96.         if (currentState == 2)
  97.         {
  98.             Vector2 localPointerPosition;
  99.             if (RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.GetComponent<RectTransform>(), eventData.position, eventData.pressEventCamera, out localPointerPosition))
  100.             {
  101.                 rectTransform.localPosition = localPointerPosition;
  102.  
  103.                 if (localPointerPosition.y > cardPlay.y)
  104.                 {
  105.                     currentState = 3;
  106.                     playArrow.SetActive(true);
  107.                     rectTransform.localPosition = playPosition;
  108.                 }
  109.             }
  110.         }
  111.     }
  112.  
  113.     public void HandleHoverState()
  114.     {
  115.         highlight.SetActive(true);
  116.         rectTransform.localScale = originalScale * selectScale;
  117.         rectTransform.localPosition = new Vector3 (rectTransform.localPosition.x, rectTransform.localPosition.y+100, rectTransform.localPosition.z);
  118.     }
  119.  
  120.     private void HandleDragState()
  121.     {
  122.         rectTransform.localRotation = Quaternion.identity;
  123.  
  124.     }
  125.  
  126.     private void HandlePlayState()
  127.     {
  128.         rectTransform.localPosition = playPosition;
  129.         rectTransform.localRotation = Quaternion.identity;
  130.         if (Mouse.current.position.ReadValue().y < cardPlay.y)
  131.         {
  132.             currentState = 2;
  133.             playArrow.SetActive(false);
  134.         }
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment