Csharp/C#教程:unity实现手游虚拟摇杆分享

本文实例为大家分享了unity实现手游虚拟摇杆的具体代码,供大家参考,具体内容如下

usingSystem.Collections; usingSystem.Collections.Generic; usingUnityEngine; usingUnityEngine.UI; ///<summary> ///绑定到摇杆上的摇杆类,参考半径50 ///</summary> publicclassRocker:MonoBehaviour{ Vector2m_offet;//偏移向量 Vector2m_originalPos;//摇杆原始屏幕坐标 Touch[]touches;//屏幕上触控点数组 inttouch_Id=-1;//触控点数组下标 boolisMove=false;//是否移动 floatm_ScreenScale; ///<summary> ///给外部调用的偏移向量,告知摇杆参数 ///</summary> publicVector3Offet { get { returnm_offet; } } //Usethisforinitialization voidStart(){ m_originalPos=transform.position;//摇杆中心的屏幕坐标位置 m_ScreenScale=Screen.width/800f; } //Updateiscalledonceperframe voidUpdate(){ //得到屏幕触控数组 touches=Input.touches; if(touches.Length>0)//如果触点开启 { //得到离摇杆中心最近的触点下标touch_Id; if(touches.Length==1)//只有一个触点时 { touch_Id=0; } elseif(touches.Length>1)//触点大于1个时 { touch_Id=0;//先假设下标为0 for(inti=1;i<touches.Length;i++)//遍历触点数组 { if(Vector2.SqrMagnitude(touches[i].position-m_originalPos)<Vector2.SqrMagnitude(touches[touch_Id].position-m_originalPos))//第i个点比假设的点近 { touch_Id=i;//假设的点改为第i个点 } } } //如果得到的触点不是取消或抬起 if(Input.GetTouch(touch_Id).phase!=TouchPhase.Canceled&&Input.GetTouch(touch_Id).phase!=TouchPhase.Ended) { //触点在摇杆范围内 if(Vector2.SqrMagnitude(touches[touch_Id].position-m_originalPos)<=50*50*m_ScreenScale*m_ScreenScale)//50为背景半径 { isMove=true;//开启遥控 //摇杆开始控制,计算偏移量 SetOffetIn(); } elseif(isMove)//触点在摇杆范围外,但是遥控已经开启 { SetOffetOut(); } } else//手指抬起,摇杆回归原始位置 { transform.position=m_originalPos; m_offet=Vector2.zero; isMove=false; touch_Id=-1; } } } ///<summary> ///触点在操作盘内时 ///摇杆控制方法 ///</summary> voidSetOffetIn() { //距离过小视为不偏移摇杆位置不变 if(Vector2.SqrMagnitude(touches[touch_Id].position-m_originalPos)<5*m_ScreenScale) { GetComponent<Image>().rectTransform.position=m_originalPos;//摇杆定位在原始位置 m_offet=Vector3.zero; } else { //摇杆位置追踪 GetComponent<Image>().rectTransform.position=touches[touch_Id].position; m_offet=touches[touch_Id].position-m_originalPos;//赋值偏移值 m_offet=m_offet.normalized;//归一化 } } ///<summary> ///触点在操作盘外时 ///摇杆控制方法 ///</summary> voidSetOffetOut() { Vector2tempDir;//临时偏移向量 tempDir=touches[touch_Id].position-m_originalPos; //更新摇杆位置:距离原始位置127各单位 GetComponent<Image>().rectTransform.position=m_originalPos+(tempDir.normalized)*25*m_ScreenScale; //偏移量 m_offet=tempDir.normalized;//归一化 } privatevoidOnGUI() { GUIStylestyle=newGUIStyle();//实例化一个新的GUIStyle,名称为style,后期使用 style.fontSize=50;//字体的大小设置数值越大,字越大,默认颜色为黑色 style.normal.textColor=newColor(1,1,1);//设置文本的颜色为新的颜色(0,0,0)修改值-代表不同的颜色,值为整数我个人觉得有点像RGB的感觉 GUI.Label(newRect(20,30,300,60),"原始位置:"+m_originalPos.ToString(),style); GUI.Label(newRect(20,100,300,60),"摇杆位置:"+GetComponent<Image>().rectTransform.position.ToString(),style); GUI.Label(newRect(20,170,300,60),"触点位置:"+touches[touch_Id].position.ToString(),style); GUI.Label(newRect(20,240,300,60),"屏幕分辨率:"+Screen.currentResolution,style); } } 您可能感兴趣的文章:Unity虚拟摇杆的实现方法unity实现虚拟摇杆控制VirtualJoystickUnity3D使用UGUI开发原生虚拟摇杆Unity实现简单虚拟摇杆unity实现手机端摇杆控制人物移动Unity实现虚拟摇杆效果Unity实现简单的虚拟摇杆Unity3D基于UGUI实现虚拟摇杆Unity实现简单虚拟摇杆Unity实现虚拟摇杆UnityUGUI通过摇杆控制角色移动Unity使用ScrollRect制作摇杆

标签: unity 手游 ni 摇杆

解决docker容器无法ping外网的问题

docker容器调用yum报错的解决办法

上述就是C#学习教程:unity实现手游虚拟摇杆分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/903948.html

(0)
上一篇 2021年10月21日
下一篇 2021年10月21日

精彩推荐