Draggable_Card.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7. using Coffee.UIEffects;
  8. public class Draggable_Card : Draggable
  9. {
  10. public CEntity_Base cEntity_Base { get; set; }
  11. public Transform DefaultParent;
  12. public Image CardImage;
  13. public UnityAction<List<DropArea>> OnDragAction;
  14. public UnityAction<List<DropArea>> OnDropAction;
  15. public bool IsDragging { get; set; }
  16. public void OffDraggable_Card()
  17. {
  18. this.gameObject.SetActive(false);
  19. }
  20. public async void SetUpDraggable_Card(CEntity_Base _cEntity_Base, Vector3 StartPosition)
  21. {
  22. this.transform.localScale = new Vector3(1, 1, 1);
  23. this.cEntity_Base = _cEntity_Base;
  24. //CardImage.color = new Color(1, 1, 1, 0.8f);
  25. CardImage.sprite = await _cEntity_Base.GetCardSprite(); ;
  26. this.gameObject.SetActive(true);
  27. this.transform.SetParent(DefaultParent);
  28. this.transform.position = StartPosition;
  29. IsDragging = true;
  30. OnBeginDrag(null);
  31. }
  32. public override void OnDrag(BaseEventData eventData)
  33. {
  34. if (GetRaycastArea() != null)
  35. {
  36. OnDragAction?.Invoke(GetRaycastArea());
  37. }
  38. base.OnDrag(eventData);
  39. }
  40. public override void OnEndDrag(BaseEventData eventData)
  41. {
  42. if (!IsDragging)
  43. {
  44. return;
  45. }
  46. IsDragging = false;
  47. this.transform.SetParent(DefaultParent);
  48. OnDropAction?.Invoke(GetRaycastArea());
  49. base.OnEndDrag(eventData);
  50. }
  51. private void Update()
  52. {
  53. if (IsDragging)
  54. {
  55. OnDrag(null);
  56. if (Input.GetMouseButtonUp(0))
  57. {
  58. if (this == null)
  59. {
  60. return;
  61. }
  62. OnEndDrag(null);
  63. }
  64. }
  65. }
  66. }