You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.6 KiB
64 lines
1.6 KiB
using UnityEngine; |
|
using UnityEngine.EventSystems; |
|
using UnityEngine.UI; |
|
|
|
public class DropImage : 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<DragImage>(); |
|
if (dragMe == null) |
|
return null; |
|
|
|
var srcImage = originalObj.GetComponent<Image>(); |
|
if (srcImage == null) |
|
return null; |
|
|
|
return srcImage.sprite; |
|
} |
|
}
|
|
|