From 115f77bda0246a00f6e17469685c67746bdbd29d Mon Sep 17 00:00:00 2001 From: nirav Date: Sat, 12 Dec 2020 07:18:36 +0000 Subject: Initial commit --- Assets/SampleUI/Scripts/DropMe.cs | 65 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100755 Assets/SampleUI/Scripts/DropMe.cs (limited to 'Assets/SampleUI/Scripts/DropMe.cs') diff --git a/Assets/SampleUI/Scripts/DropMe.cs b/Assets/SampleUI/Scripts/DropMe.cs new file mode 100755 index 0000000..cb4bca5 --- /dev/null +++ b/Assets/SampleUI/Scripts/DropMe.cs @@ -0,0 +1,65 @@ +using System.Reflection; +using UnityEngine; +using UnityEngine.EventSystems; +using UnityEngine.UI; + +public class DropMe : MonoBehaviour, IDropHandler, IPointerEnterHandler, IPointerExitHandler +{ + public Image containerImage; + public Image receivingImage; + private Color normalColor; + public Color highlightColor = Color.yellow; + + public void OnEnable () + { + if (containerImage != null) + normalColor = containerImage.color; + } + + public void OnDrop(PointerEventData data) + { + containerImage.color = normalColor; + + if (receivingImage == null) + return; + + Sprite dropSprite = GetDropSprite (data); + if (dropSprite != null) + receivingImage.overrideSprite = dropSprite; + } + + public void OnPointerEnter(PointerEventData data) + { + if (containerImage == null) + return; + + Sprite dropSprite = GetDropSprite (data); + if (dropSprite != null) + containerImage.color = highlightColor; + } + + public void OnPointerExit(PointerEventData data) + { + if (containerImage == null) + return; + + containerImage.color = normalColor; + } + + private Sprite GetDropSprite(PointerEventData data) + { + var originalObj = data.pointerDrag; + if (originalObj == null) + return null; + + var dragMe = originalObj.GetComponent(); + if (dragMe == null) + return null; + + var srcImage = originalObj.GetComponent(); + if (srcImage == null) + return null; + + return srcImage.sprite; + } +} -- cgit v1.2.3