DropMe.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System.Reflection;
  2. using UnityEngine;
  3. using UnityEngine.EventSystems;
  4. using UnityEngine.UI;
  5. namespace Rokid.UXR.Demo
  6. {
  7. public class DropMe : MonoBehaviour, IDropHandler, IPointerEnterHandler, IPointerExitHandler
  8. {
  9. public Image containerImage;
  10. public Image receivingImage;
  11. private Color normalColor = Color.white;
  12. public Color highlightColor = Color.yellow;
  13. public void OnDrop(PointerEventData data)
  14. {
  15. containerImage.color = normalColor;
  16. if (receivingImage == null)
  17. return;
  18. Image dropImage = GetDropSprite(data);
  19. if (dropImage != null)
  20. {
  21. receivingImage.overrideSprite = dropImage.sprite;
  22. receivingImage.color = dropImage.color;
  23. }
  24. }
  25. public void OnPointerEnter(PointerEventData data)
  26. {
  27. if (containerImage == null)
  28. return;
  29. Sprite dropSprite = GetDropSprite(data)?.sprite;
  30. if (dropSprite != null)
  31. containerImage.color = highlightColor;
  32. }
  33. public void OnPointerExit(PointerEventData data)
  34. {
  35. if (containerImage == null)
  36. return;
  37. containerImage.color = normalColor;
  38. }
  39. private Image GetDropSprite(PointerEventData data)
  40. {
  41. var originalObj = data.pointerDrag;
  42. if (originalObj == null)
  43. return null;
  44. var dragMe = originalObj.GetComponent<DragMe>();
  45. if (dragMe == null)
  46. return null;
  47. var srcImage = originalObj.GetComponent<Image>();
  48. srcImage.color = originalObj.GetComponent<Image>().color;
  49. if (srcImage == null)
  50. return null;
  51. return srcImage;
  52. }
  53. }
  54. }