首页 > 其他分享 >微信小游戏中拖拽场景位置的限制代码

微信小游戏中拖拽场景位置的限制代码

时间:2023-12-19 13:56:16浏览次数:29  
标签:RayName 微信 list private 小游戏 cam Input newP 拖拽

using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Camera))]
public class CameraControl : MonoBehaviour
{
    public static CameraControl instance;
    public List<string> list_RayName = new List<string>();
    public Camera m_cam;
    private Transform m_selfTrans;
    private bool m_fingerDown = false;
    public RaycastHit hit;
    //当场景显示之前出现UI界面就关闭拖拽
    public bool Boxcoller_bol = true;//判断碰撞框是否激活

    /// <summary>
    /// 单指滑动的手指位置
    /// </summary>
    private Vector3 m_oneFingerDragStartPos;
    /// <summary>
    /// 双指缩放的上一帧的双指距离
    /// </summary>
    private float m_twoFingerLastDistance = -1;


    private void Awake()
    {
        instance = this;
        m_cam = GetComponent<Camera>();
        m_selfTrans = transform;
        list_RayName.Add("Hospital_panel");
        list_RayName.Add("Hospital_panel_1");
        list_RayName.Add("Hoof_repair_rooms");
        list_RayName.Add("Hoof_repair_room_2");
        list_RayName.Add("Pancake_tudio_2");
        list_RayName.Add("Wall");
        list_RayName.Add("Caltha");
        list_RayName.Add("Bar");
        list_RayName.Add("Gaming");
        list_RayName.Add("Guidance_table");
    }

    private void Update()
    {
#if UNITY_EDITOR || UNITY_STANDALONE
        if (Boxcoller_bol)
        {
            if (Input.GetMouseButtonDown(0))
            {
                Ray ray = m_cam.ScreenPointToRay(Input.mousePosition);
                RaycastHit hitInfo;  //射线对象(结构体),存储了相关信息
                                     //发出射线检测碰撞,返回一个布尔值(是否发生碰撞),这里的out下面会详解
                                     //碰到了标签是123的碰撞盒物体
                if (Physics.Raycast(ray, out hitInfo))
                {
                    hit = hitInfo;
                    for (int i = 0; i < list_RayName.Count; i++)
                    {
                        //Debug.Log(hitInfo.transform.gameObject.layer);
                        if (list_RayName.Contains(hitInfo.transform.name))
                        {
                            m_fingerDown = true;
                            m_oneFingerDragStartPos = GetWorldPos(Input.mousePosition);
                        }
                    }
                }
            }

            if (Input.GetMouseButtonUp(0))
            {

                m_fingerDown = false;
                m_twoFingerLastDistance = -1;
            }

            if (m_fingerDown)
            {
                HandleFingerDragMove(Input.mousePosition);
            }
            var distance = Input.GetAxis("Mouse ScrollWheel");
            HandleMouseScrollWheel(distance * 10);
        }
#else
        if(Boxcoller_bol)
        {
            if (2 == Input.touchCount)
            {
                // 双指缩放
                HandleTwoFingerScale();
            }
            else if (1 == Input.touchCount)
            {
                if (TouchPhase.Began == Input.touches[0].phase)
                {   
                    Ray ray = m_cam.ScreenPointToRay(Input.mousePosition);
                    RaycastHit hitInfo;  
                     if (Physics.Raycast(ray, out hitInfo))
                    {
                        if (list_RayName.Contains(hitInfo.transform.name))
                        {
                            m_fingerDown = true;
                            m_oneFingerDragStartPos = GetWorldPos(Input.mousePosition);
                        }
                    }
                }
                else if (TouchPhase.Moved == Input.touches[0].phase)
                {
                    // 单指滑动
                    HandleFingerDragMove(Input.touches[0].position);
                }
                m_twoFingerLastDistance = -1;
            }
            else
            {
                m_fingerDown = false;
                m_twoFingerLastDistance = -1;
            }
        }
#endif
    }


    /// <summary>
    /// 单指滑动
    /// </summary>
    private void HandleFingerDragMove(Vector2 fingerPos)
    {
        //滑动差
        Vector3 cha = m_oneFingerDragStartPos - GetWorldPos(fingerPos);
        Vector3 newP = m_cam.transform.position;
        newP.x = newP.x + cha.x;
        if (newP.x > 16f) { newP.x = 16f; }
        if (newP.x < -16f) { newP.x = -16f; }
        newP.y = newP.y + cha.y;
        if (newP.y > 23.4f) { newP.y = 23.4f; }
        if (newP.y < -23.4f) { newP.y = -23.4f; }

        m_selfTrans.position = newP;
    }

    /// <summary>
    /// 双指缩放
    /// </summary>
    private void HandleTwoFingerScale()
    {
        float distance = Vector2.Distance(Input.touches[0].position, Input.touches[1].position);
        if (-1 == m_twoFingerLastDistance) m_twoFingerLastDistance = distance;
        // 与上一帧比较变化
        float scale = 0.1f * (distance - m_twoFingerLastDistance);
        ScaleCamere(scale);
        m_twoFingerLastDistance = distance;
    }

    /// <summary>
    /// 鼠标滚轮缩放
    /// </summary>
    /// <param name="distance"></param>
    private void HandleMouseScrollWheel(float distance)
    {
        if (0 == distance) return;
        ScaleCamere(distance);
    }

    /// <summary>
    /// 缩放摄像机的视口
    /// </summary>
    /// <param name="scale"></param>
    private void ScaleCamere(float scale)
    {
        m_cam.orthographicSize -= scale * 0.5f;//缩小放大速度
        if (m_cam.orthographicSize < 10)//最小可视距离
        {
            m_cam.orthographicSize = 10;
        }
        if (m_cam.orthographicSize > 20)//最大可视距离
        {
            m_cam.orthographicSize = 20;
        }
    }

    /// <summary>
    /// 屏幕坐标换算成3D坐标
    /// </summary>
    /// <param name="screenPos">屏幕坐标</param>
    /// <returns></returns>
    Vector3 GetWorldPos(Vector2 screenPos)
    {
        return m_cam.ScreenToWorldPoint(new Vector3(screenPos.x, screenPos.y, 0));
    }
}

 

标签:RayName,微信,list,private,小游戏,cam,Input,newP,拖拽
From: https://www.cnblogs.com/cxh123/p/17913538.html

相关文章

  • 做一个小游戏,跳跃的小球
    以下为代码:#-*-coding:utf-8-*-importsys#导入sys模块importpygame#导入pygame模块pygame.init()#初始化pygamesize=width,height=800,700#设置窗口screen=pygame.display.set_mode(size)#显示窗口color=(0,0,0)#设置颜......
  • Unity3D 拖拽赋值组件与通过Find赋值组件的优点与缺点详解
    前言Unity3D是一款流行的游戏开发引擎,提供了丰富的功能和工具,使开发人员能够轻松创建高质量的游戏。在Unity3D中,我们经常需要通过拖拽赋值组件或通过Find赋值组件来实现不同对象之间的交互。本文将详细介绍这两种方法的优点和缺点,并给出相应的技术详解和代码实现。对啦!这里有个......
  • 小游戏
    importpygameimportsysclassBird(object):"""定义一个鸟类"""def__init__(self):"""定义初始化方法"""self.birdRect=pygame.Rect(65,50,50,50)#鸟的矩形#定义鸟的3种状态列表......
  • 微信小程序校园跑腿系统怎么做,如何做,要做多久
    ​ 在这个互联网快速发展、信息爆炸的时代,人人都离不开手机,每个人都忙于各种各样的事情,大学生也一样,有忙于学习,忙于考研,忙着赚学分,忙于参加社团,当然也有忙于打游戏的(还很多),但生活中的一些琐事无形当中会占用你的一些时间,例如排队打饭、排队取快递、寄快递、拿外卖,打印资料等等等等......
  • 微信小程序顶部自定义标题对齐胶囊按钮
    微信小程序顶部自定义标题样式对其胶囊按钮css中使用了uniapp的var(--status-bar-height))获取系统栏高度,js中使用了uni.getMenuButtonBoundingClientRect();该api获取小程序菜单按钮的位置信息,返回的有胶囊按钮的宽、高、上、右、下、左,本例中使用了胶囊按钮的top信息示例图......
  • ASP.NET WEBAPI 接入微信公众平台总结,Token验证失败解决办法
    首先,请允许我说一句:shit!因为这个问题不难,但是网上有关ASP.NETWEBAPI的资料太少。都是PHP等等的。我也是在看了某位大神的博客后有启发,一点点研究出来的。来看正题!1.微信公众平台的接入方法,无非4个参数(signature,timestamp,nonce,echostr)加1个Token(两边对应)2.Token,timestamp,......
  • 一个能用的微信小程序抓包方式
    [一个能用的微信小程序抓包方式(亲测)-『精品软件区』-吾爱破解-LCG-LSG|安卓破解|病毒分析|www.52pojie.cn](https://www.52pojie.cn/thread-1849166-1-2.html)今天接到复测微信小程序的任务,需要对微信小程序进行抓包,从上午到现在试了很多方式,分别为Burp+Proxifier、Burp......
  • 微信小程序 文字超过行后隐藏显示省略号
    1、只需要显示一行,超过的省略号处理text{overflow:hidden;//超出一行文字自动隐藏text-overflow:ellipsis;//文字隐藏后添加省略号white-space:nowrap;//强制不换行}2、如果在多行的情况下注解一定要加注解一定要加注解一定要加重要的事情......
  • 小游戏
    flybird游戏importpygameimportrandompygame.init()WIDTH=288HEIGHT=512screen=pygame.display.set_mode((WIDTH,HEIGHT))pygame.display.set_caption('FlappyBird')background_img=pygame.image.load('background.png').convert()bird_im......
  • 小游戏
    制作一个跳跃的小球游戏(Pygame基本使用)importsysimportpygamepygame.init()size=width,height=640,480screen=pygame.display.set_mode(size)color=(0,0,0)ball=pygame.image.load("ball123.png")ballrect=ball.get_rect()s......