UGUI学习:抓取和放入替换图片
程序员文章站
2022-05-30 17:37:59
...
效果:
抓取的图片
注:
PointerEventData.pointerEnter
当前触发OnPointerEnter的对象,一般鼠标移动到ui上的时候会触发这个事件,对应的是OnPointerExit,鼠标移出的时候
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
[RequireComponent(typeof(Image))]
public class DragMe : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
private GameObject m_DraggingIcon; // 抓取的图片
private RectTransform m_DraggingPlane; //图片位置信息
public void OnBeginDrag(PointerEventData eventData)
{
var canvas = FindInParents<Canvas>(gameObject);
if (canvas == null)
return;
m_DraggingIcon = new GameObject("icon");
m_DraggingIcon.transform.SetParent(canvas.transform, false);
m_DraggingIcon.transform.SetAsLastSibling();
var image = m_DraggingIcon.AddComponent<Image>();
image.sprite = GetComponent<Image>().sprite;
image.SetNativeSize();
CanvasGroup group = m_DraggingIcon.AddComponent<CanvasGroup>();
group.blocksRaycasts = false; //不交互
m_DraggingPlane = transform as RectTransform;
}
public void OnDrag(PointerEventData eventData)
{
if (m_DraggingIcon != null)
{
SetDraggedPosition(eventData);
}
}
public void OnEndDrag(PointerEventData eventData)
{
if (m_DraggingIcon != null)
{
Destroy(m_DraggingIcon);
}
}
public void SetDraggedPosition(PointerEventData data)
{
if (data.pointerEnter != null && data.pointerEnter.transform as RectTransform != null)
m_DraggingPlane = data.pointerEnter.transform as RectTransform;
var rt = m_DraggingIcon.transform as RectTransform;
Vector3 pos;
if (RectTransformUtility.ScreenPointToWorldPointInRectangle(FindInParents<Canvas>(gameObject).transform as RectTransform, data.position, data.pressEventCamera, out pos))
{
rt.position = pos;
rt.rotation = m_DraggingPlane.rotation;
}
}
static public T FindInParents<T>(GameObject go) where T : Component
{
if (go == null) return null;
var comp = go.GetComponent<T>();
if (comp != null)
return comp;
Transform t = go.transform.parent;
while (t != null && comp == null)
{
comp = t.gameObject.GetComponent<T>();
t = t.parent;
}
return comp;
}
}
放入的图片
注:
1.PointerEventData .pointerDrag
当前触发 OnDrag事件的物体,设置为空的时候不再触发OnDrag和 OnEndDrag .
2.Unity Image.sprite 与 Image.overrideSprite 的区别
overrideSprite 用于修改图片,但是不会把原来的图片给消除掉,直接的效果是如果 overrideSprite 是 null 的话,会变回原本的图片。可以理解为 overrdieSprite 是覆盖在 sprite 上的一层图片。所以,若使用 overrideSprite 更换图片,接下来要更换也只能用overrideSprite再覆盖掉,除非把它置为 null,不然用 sprite 更换是显示不出来的。
3.选择Image组件中-type-simple的Preserve Aspect 确保图像保留其现有尺寸
using System.Collections;
using System.Collections.Generic;
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;
private void OnEnable()
{
if (containerImage != null)
_normalColor = containerImage.color;
}
public void OnDrop(PointerEventData eventData)
{
containerImage.color = _normalColor;
if (eventData.pointerDrag != null && receivingImage != null)
{
Image image = eventData.pointerDrag.GetComponent<Image>();
receivingImage.sprite = image.sprite;
}
}
public void OnPointerEnter(PointerEventData eventData)
{
if(eventData.pointerDrag != null && containerImage != null)
containerImage.color = highlightColor;
}
public void OnPointerExit(PointerEventData eventData)
{
if (containerImage != null)
containerImage.color = _normalColor;
}
}
推荐阅读