Unity 3D模型展示之背包系统

这篇博客详细介绍了在Unity3D中如何实现配件UI的拖拽交互。通过RightPanel脚本,当鼠标拖入配件区时,会创建配件预制体并隐藏拖拽元件。RightItem脚本负责模拟选择和安装配件的过程,实现配件的拖动。同时,调整了SwitchDrag脚本,增加了元件状态信息,优化了拖拽行为,使其跟随鼠标轨迹移动。这些交互对于提升游戏用户体验至关重要。
摘要由CSDN通过智能技术生成
1. 效果展示

在这里插入图片描述

2. 配件区脚本
	RightPanel脚本:当鼠标拖入到配件区时,创建配件预制体,隐藏拖拽的元件。
using UnityEngine;
using UnityEngine.EventSystems;

public class RightPanel : MonoBehaviour, IPointerEnterHandler
{

    public GameObject contentPrefbs;
    public Transform parentTransform;

    public void OnPointerEnter(PointerEventData eventData)
    {
        var gameobj = SwitchOperation.Instance.GetCurrentSwitchObj();

        if (gameobj != null)
        {
            gameobj = SwitchOperation.Instance.GetCurrentSwitchObj().gameObject;
            var dd = gameobj.GetComponent<SwitchDrag>();
            if (dd.isdrag && gameobj.activeSelf && dd.status == 1)
            {
                //修改透明度
                TransparentSet.Instance.RecoveryTransparent();
                gameobj.SetActive(false);
                SwitchOperation.Instance.hideInfo(false);
                //获取配件区命名UI的信息
                string objName = SwitchOperation.Instance.GetCurrentSwitchObj().gameObject.name + "_UI";
                //判断是否存在,不存在则创建配件区RightItem
                var tempObj = parentTransform.Find(objName) != null ? parentTransform.Find(objName).gameObject : Instantiate(contentPrefbs, parentTransform);
                //并命名作为重复判断的依据
                tempObj.name = objName;
            }
        }
    }
}

脚本挂载
在这里插入图片描述

3. 配件区UI拖拽
	配件区UI的拖拽,主要是模拟选择配件,安装配件。
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class RightItem : MonoBehaviour, IBeginDragHandler, IDragHandler
{
    // Start is called before the first frame update
    private Button button;
    private string objName;
    private Image img;

    private GameObject moveObj;

    Vector3 offPos;//存储按下鼠标时的图片-鼠标位置差
    Vector3 arragedPos; //保存经过整理后的向量,用于图片移动
    public void OnBeginDrag(PointerEventData eventData)
    {
        if (RectTransformUtility.ScreenPointToWorldPointInRectangle(transform.GetComponent<RectTransform>(), Input.mousePosition
   , eventData.enterEventCamera, out arragedPos))
        {
            Debug.Log("移动位置1" + arragedPos);
            offPos = transform.position - arragedPos;
        }
    }
    private void Destroy()
    {
        Destroy(this.gameObject);
    }
    public void OnDrag(PointerEventData eventData)
    {
        transform.position = offPos + Input.mousePosition;

        var gameObj = GameObject.Find("Switch");

        moveObj = UHelper.FindTheChild(gameObj.gameObject, objName).gameObject;
        if (!moveObj.activeSelf)
        {
            moveObj.SetActive(true);
            moveObj.GetComponent<SwitchDrag>().isdrag = true;
            moveObj.GetComponent<SwitchDrag>().status = 0;
            Invoke("Destroy", 0.1f);
        }

    }


    // Start is called before the first frame update
    void Start()
    {
        button = GetComponent<Button>();
        objName = this.name.Replace("_UI", "");
        button.AddOnClick(onClick);
    }
    void onClick()
    {
        Debug.Log("compentPart:" + objName);
    }
    // Update is called once per frame
    void Update()
    {

    }
}

脚本挂载
在这里插入图片描述

4. 调整脚本
  • 调整SwitchDrag,增加元件状态信息,删除不需要的变量,修改元件拖拽方式为跟踪鼠标轨迹移动

using UnityEngine;

public class SwitchDrag : MonoBehaviour
{
    public bool isdrag = false;//用来判断物体是否被拖拽
    Vector3 targetPos;
    Vector3 lastPos;
    Vector3 originalPos;
    //private Outline outline;
    private SwitchPart switchPart;
  
    //状态0为默认 1为卸下,作为配件区UI创建的依据
    public int status = 0;
    void Start()
    {
        switchPart = GetComponent<SwitchPart>();
        originalPos = this.transform.position;
    }
    //当鼠标按下物体
    void OnMouseDown()
    {
        if (!isdrag)
        {
            lastPos = Camera.main.ScreenToViewportPoint(Input.mousePosition) - Camera.main.WorldToViewportPoint(transform.position);
        }
        isdrag = true;
        status = 1;

    }

    private void OnMouseOver()
    {
        if (Input.GetMouseButtonDown(1))
        {
            isdrag = false;
            this.transform.position = originalPos;
        }
    }

    void Update()
    {
        moveObj();
    }
    void moveObj()
    {
        //moveObj1 = UHelper.FindTheChild(gameObj.gameObject, objName).gameObject;
        if (this.gameObject.activeSelf && isdrag)
        {
            Vector3 screenPos = Camera.main.WorldToScreenPoint(this.transform.position);
            //获取鼠标位置
            Vector3 mousePos = Input.mousePosition;
            //因为鼠标只有X,Y轴,所以要赋予给鼠标Z轴
            mousePos.z = screenPos.z;
            //把鼠标的屏幕坐标转换成世界坐标
            Vector3 worldPos = Camera.main.ScreenToWorldPoint(mousePos);
            //控制物体移动
            //this.transform.position = worldPos;
            this.transform.position = Vector3.Lerp(this.transform.position, worldPos, Time.deltaTime * 15);
        }

    }
    //当鼠标拖拽物体
    void OnMouseDrag()
    {
        targetPos = Camera.main.ViewportToWorldPoint(Camera.main.ScreenToViewportPoint(Input.mousePosition) - lastPos);
        transform.position = new Vector3(targetPos.x, targetPos.y, targetPos.z);
        isdrag = true;
        SwitchOperation.Instance.currentIndex = SwitchOperation.Instance.GetSwitchParts().FindIndex(p => p.ObjName == switchPart.mSwitchParts.ObjName);
    }
}

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yxlalm

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值