unity拼图

文章介绍了在Unity中使用C#实现的游戏对象(如笔画)随鼠标移动的逻辑,包括BeginDrag、Drag和EndDrag事件处理,以及与Destination组件的距离判断以决定笔画的显示和隐藏。
摘要由CSDN通过智能技术生成
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;


public class Move : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    public Camera m_cam;//相机
    public Image that;//笔画
    public Transform traget;//笔画目的地
    private bool ismove;//当ismove为true时,笔画跟随鼠标移动,该变量为了防止没找到笔画,但是通过长按屏幕使笔画移动
    public GameObject that_copy;//背景笔画
    void Start()
    {

    }

    void Update()
    {
        //组件拼图完成,消失
       if (Vector2.Distance(transform.position, traget.position) < destination.instance.range)
        {
            Destroy(this.gameObject);
            that_copy.SetActive(true);
        }
    }
    public Vector2 GetWorldPos(Vector2 screenPos)
    {
        return m_cam.ScreenToWorldPoint(new Vector2(screenPos.x, screenPos.y));
        // return m_cam.ScreenToWorldPoint(screenPos);这里可以尝试这样写
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        if(ismove==true)
         transform.position = GetWorldPos(eventData.position);
    }

    public void OnDrag(PointerEventData eventData)
    {
        if (ismove == true)
        transform.position = GetWorldPos(eventData.position);;
    }

    public void OnEndDrag(PointerEventData eventData)
    {
 
    }

    public void click()
    {
        that.color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
        ismove = true;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class destination : MonoBehaviour
{
    // Start is called before the first frame update
    public static destination instance;
    [Tooltip("range,当笔画与春字距离小于range时笔画消失")]
    public float range;
    void Start()
    {
        instance = this;
    }


    private void OnDrawGizmos()
    {
        Gizmos.color = Color.green;
        Gizmos.DrawWireSphere(transform.position, range);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值