Unity拖拽UI生成物体的实现

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class MYTEST : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    public bool IsDrag;
    public Transform OtherBox;
    public Transform SelfBox;
    public Vector2 OriginPos;
    public GameObject ResCube;

    public int TerrainLayMask;

    private GameObject _createGo;


    void Awake()
    {
        OriginPos = transform.GetComponent<RectTransform>().anchoredPosition;
        TerrainLayMask = LayerMask.GetMask("Terrain");
    }
    private void Update()
    {
        if (!IsDrag) return;
        InOrOutUI(Input.mousePosition.x > 100);
        if (_createGo == null) return;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);//摄像机到屏幕的射线
        RaycastHit hit;
        Vector3 newPos;
        if (Physics.Raycast(ray, out hit,1000, 1 << LayerMask.NameToLayer("Terrain")))//如果碰到Collider,就把物体的位置变成这个位置
        {
            newPos = hit.point;
            Debug.Log("碰到Collider>>>"+ newPos);
        }
        else//如果没有碰到,就把屏幕坐标转换为世界坐标
        {
            var pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 300f));
            newPos = pos;
            Debug.LogError("NO碰到>>>>>"+ newPos);
        }
//        _createGo.transform.position = newPos;
        _createGo.transform.position = new Vector3(newPos.x, newPos.y+25, newPos.z);
    }

    void InOrOutUI(bool b)
    {
        if (b)
        {
            if (transform.parent == OtherBox) return;
            transform.SetParent(OtherBox);
            if (_createGo == null)
            {
                _createGo = Instantiate<GameObject>(ResCube);
            }
            else
            {
                _createGo.SetActive(true);
            }
        }
        else
        {
            if (transform.parent == SelfBox) return;
            transform.SetParent(SelfBox);
            transform.localScale = Vector3.one;
            if (_createGo != null)
            {
                _createGo.SetActive(false);
            }
        }
    }

    // begin dragging
    public void OnBeginDrag(PointerEventData eventData)
    {
        IsDrag = true;
        Debug.Log("OnBeginDrag");
        SetDraggedPosition(eventData);
    }

    // during dragging
    public void OnDrag(PointerEventData eventData)
    {
        SetDraggedPosition(eventData);
    }

    // end dragging
    public void OnEndDrag(PointerEventData eventData)
    {
        IsDrag = false;
        Debug.Log("OnEndDrag");
        SetDraggedPosition(eventData);
        transform.GetComponent<RectTransform>().anchoredPosition = OriginPos;
    }

    /// <summary>
    /// set position of the dragged game object
    /// </summary>
    /// <param name="eventData"></param>
    private void SetDraggedPosition(PointerEventData eventData)
    {
        var rt = gameObject.GetComponent<RectTransform>();

        // transform the screen point to world point int rectangle
        Vector3 globalMousePos;
        if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, eventData.position, eventData.pressEventCamera,
            out globalMousePos))
        {
            rt.position = globalMousePos;
        }

    }
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值