其他分享
首页 > 其他分享> > unity 实现接口,3d物体拖拽

unity 实现接口,3d物体拖拽

作者:互联网

using UnityEngine.EventSystems;
using System;
using MessageBoxNS;
using UnityEngine;
using TMPro;
using GunAssembly.Facade;

namespace GunAssembly.View
{
    public class GunUIPartItem : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler,
IInitializePotentialDragHandler, IBeginDragHandler, IDragHandler, IEndDragHandler
    {
        /// <summary>
        /// 是否正在拖拽中
        /// </summary>
        public static bool IsDrag = false;

        /// <summary>
        /// Q的名字
        /// </summary>
        public string GunName;

        /// <summary>
        /// 零件名称
        /// </summary>
        public TextMeshProUGUI TextMesh;

        /// <summary>
        /// 零件的说明
        /// </summary>
        public string PartExplain;

        /// <summary>
        /// 零件名称
        /// </summary>
        public string PartName
        {
            get
            {
                return TextMesh.text;
            }
            set
            {
                TextMesh.text = value;
            }
        }

        /// <summary>
        /// 设置该零件是否已经完成
        /// </summary>
        public bool IsCurrentPartFinish;

        /// <summary>
        /// 当前零件所属的步骤
        /// </summary>
        public int CurrentPartStep;

        public GunUIView View;

        /// <summary>
        /// 当前零件的目标对象
        /// </summary>
        private GameObject TargetObj;

        /// <summary>
        /// 拖拽对象
        /// </summary>
        private GameObject ResouceObj;


        public void OnPointerEnter(PointerEventData eventData)
        {
            if (!IsDrag)
            {
                View.SetGunPartInfo(PartExplain);
            }
        }

        public void OnPointerExit(PointerEventData eventData)
        {
            if (!IsDrag)
            {
                View.SetGunPartInfo("");
            }
        }

        public void OnInitializePotentialDrag(PointerEventData eventData)
        {
            View.SetGunPartInfo(PartExplain);
        }

        public void OnBeginDrag(PointerEventData eventData)
        {
            IsDrag = true;
            CameraObserve.Instance.CanMove = false;
            ResouceObj = CreatDragObj(PartName);
        }

        public void OnDrag(PointerEventData eventData)
        {
            if (IsDrag)
            {
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit hit;
                if (Physics.Raycast(ray, out hit))
                {
                    GameObject gameObj = hit.collider.gameObject;
                    if (gameObj.name.Equals(PartName) && !IsCurrentPartFinish)
                    {
                        IsCurrentPartFinish = true;
                        OnEndDrag(null);
                        AppFacade.Instance.SendNotification(Events.Events.UpdateView, this);
                    }
                }
            }
        }

        public void OnEndDrag(PointerEventData eventData)
        {
            IsDrag = false;
            CameraObserve.Instance.CanMove = true;
            if (ResouceObj != null) Destroy(ResouceObj);
            View.SetGunPartInfo("");
        }



        /// <summary>
        /// 生成预制
        /// </summary>
        /// <param name="partName"></param>
        private GameObject CreatDragObj(string partName)
        {
            GunModelViewMediator gunModelViewMediator = Facade.AppFacade.Instance.RetrieveMediator(GunModelViewMediator.NAME) as GunModelViewMediator;
            TargetObj = gunModelViewMediator.GetGunPart(PartName);
            return Instantiate(Resources.Load("Model/Part/" + GunName + "/" + PartName) as GameObject);
        }

        private void Update()
        {
            if (IsDrag && ResouceObj != null)
            {
                float distance = Vector3.Distance(Camera.main.transform.position, TargetObj.transform.position);
                Vector3 Pos = Camera.main.ScreenToWorldPoint(Input.mousePosition + new Vector3(0, 0, distance));
                ResouceObj.transform.position = Pos;
            }
        }
    }
}

主要逻辑还是在OnDrag里边

标签:void,拖拽,PointerEventData,unity,IsDrag,using,ResouceObj,public,3d
来源: https://blog.csdn.net/kvkZzzz/article/details/120342927