Unity GameObject 拖拉原始碼

LiKaiY1982 2018-09-24 08:18:44
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

[RequireComponent(typeof(Image))]
public class LightingOnList : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
public bool dragOnSurfaces = true;

private GameObject m_DraggingIcon;
private RectTransform m_DraggingPlane;
public GameObject prefab;

public void OnBeginDrag(PointerEventData eventData)
{
Debug.LogError("OnBeginDrag");
var canvas = FindInParents<Canvas>(gameObject);
if (canvas == null)
return;

// We have clicked something that can be dragged.
// What we want to do is create an icon for this.
m_DraggingIcon = new GameObject("icon");

m_DraggingIcon.transform.SetParent(canvas.transform, false);
m_DraggingIcon.transform.SetAsLastSibling();

var image = m_DraggingIcon.AddComponent<Image>();
// The icon will be under the cursor.
// We want it to be ignored by the event system.
CanvasGroup group = m_DraggingIcon.AddComponent<CanvasGroup>();
group.blocksRaycasts = false;

image.sprite = GetComponent<Image>().sprite;
image.SetNativeSize();

if (dragOnSurfaces)
m_DraggingPlane = transform as RectTransform;
else
m_DraggingPlane = canvas.transform as RectTransform;

SetDraggedPosition(eventData);
}

public void OnDrag(PointerEventData data)
{
Debug.LogError("OnDrag");
if (m_DraggingIcon != null)
SetDraggedPosition(data);
}

private void SetDraggedPosition(PointerEventData data)
{
Debug.LogError("SetDraggedPosition");
if (dragOnSurfaces && data.pointerEnter != null && data.pointerEnter.transform as RectTransform != null)
m_DraggingPlane = data.pointerEnter.transform as RectTransform;

var rt = m_DraggingIcon.GetComponent<RectTransform>();
Vector3 globalMousePos;
if (RectTransformUtility.ScreenPointToWorldPointInRectangle(m_DraggingPlane, data.position, data.pressEventCamera, out globalMousePos))
{
rt.position = globalMousePos;
rt.rotation = m_DraggingPlane.rotation;
}
}

public void OnEndDrag(PointerEventData eventData)
{
Debug.LogError("OnEndDrag - "+ eventData);

GameObject pointerEnter = eventData.pointerEnter;
GameObject lastPress = Instantiate(prefab, new Vector3(0, 0, 0), Quaternion.identity);

lastPress.transform.parent = pointerEnter.transform;
lastPress.transform.position = eventData.position;
if (m_DraggingIcon != null)
Destroy(m_DraggingIcon);
}

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;
}
}
...全文
223 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
SunryOne 2018-10-22
  • 打赏
  • 举报
回复
有没有移动端的拖拉源码呢

2,537

社区成员

发帖
与我相关
我的任务
社区描述
Unity3D相关内容讨论专区
游戏unity 技术论坛(原bbs)
社区管理员
  • Unity3D
  • 芝麻粒儿
  • 「已注销」
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

Unity3D社区公告:

  1. 社区致力于解决各种Unity3D相关的“疑难杂症”。
  2. 社区不允许发布与Unity3D或相关技术无关内容。
  3. 社区版主邀请各位一道为打造优秀社区不懈努力。

试试用AI创作助手写篇文章吧