博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UGUI实现的虚拟摇杆,可改变摇杆位置
阅读量:6939 次
发布时间:2019-06-27

本文共 3680 字,大约阅读时间需要 12 分钟。

实现方式主要参考这篇文章:http://www.cnblogs.com/plateFace/p/4687896.html。

主要代码如下:

1 using UnityEngine;  2 using System.Collections;  3 using UnityEngine.UI;  4 using UnityEngine.EventSystems;  5   6   7 public delegate void JoystickMoveDelegate(JoystickData data);  8   9 public class Joystick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler 10 { 11  12  13     public GameObject joystickUI;                   //摇杆整体UI,方便Active 14     public RectTransform joystickCenter;            //摇杆重心 15     public RectTransform joystickBackground;        //摇杆背景 16  17     public RectTransform joystickRect; 18     private float radius; 19     bool isClick; 20     Vector2 clickPosition; 21  22  23     public static event JoystickMoveDelegate JoystickMoveEvent; 24  25  26  27     // Use this for initialization 28     void Start() 29     { 30         radius = 71; 31     } 32  33     // Update is called once per frame 34     void Update() 35     { 36         JoystickController(); 37     } 38  39     public void JoystickController() 40     { 41         if (isClick) 42         { 43             clickPosition = GetClickPosition(); 44             float distance = Vector2.Distance(new Vector2(clickPosition.x, clickPosition.y) / Screen.width * 960, joystickRect.anchoredPosition); 45  46             if (distance < radius) 47             { 48                 //当距离小于半径就开始移动 摇杆重心 49                 joystickCenter.anchoredPosition = new Vector2(clickPosition.x / Screen.width * 960 - joystickRect.anchoredPosition.x, clickPosition.y / Screen.width * 960 - joystickRect.anchoredPosition.y); 50             } 51             else 52             { 53                 //求圆上的一点:(目标点-原点) * 半径/原点到目标点的距离 54                 Vector2 endPosition = (new Vector2(clickPosition.x, clickPosition.y) / Screen.width * 960 - joystickRect.anchoredPosition) * radius / distance; 55                 joystickCenter.anchoredPosition = endPosition; 56             } 57  58             if (JoystickMoveEvent != null) 59             { 60                 JoystickMoveEvent(new JoystickData() { x = joystickCenter.anchoredPosition.x - joystickBackground.anchoredPosition.x, y = joystickCenter.anchoredPosition.y - joystickBackground.anchoredPosition.y }); 61             } 62  63         } 64     } 65  66     public void OnPointerDown(PointerEventData eventData) 67     { 68         ChangeAlpha(1); 69         clickPosition = GetClickPosition(); 70         joystickRect.anchoredPosition = clickPosition / Screen.width * 960; 71         isClick = true; 72     } 73  74     public void OnPointerUp(PointerEventData eventData) 75     { 76         ChangeAlpha(0.3f); 77         joystickCenter.anchoredPosition = Vector2.zero; 78         isClick = false; 79     } 80  81     ///  82     /// 根据平台不同获取点击位置坐标 83     ///  84     /// 
85 Vector2 GetClickPosition() 86 { 87 if (Application.platform == RuntimePlatform.Android) 88 { 89 return Input.GetTouch(0).position; 90 91 } 92 else if (Application.platform == RuntimePlatform.WindowsEditor) 93 { 94 return Input.mousePosition; 95 } 96 return Vector2.zero; 97 } 98 99 /// 100 /// 改变图片alpha值101 /// 102 /// 103 void ChangeAlpha(float alphaValue)104 {105 joystickBackground.GetComponent
().color = new Color(1,1,1,alphaValue);106 joystickCenter.GetComponent
().color = new Color(1, 1, 1, alphaValue);107 }108 }109 110 public class JoystickData111 {112 public float x;113 public float y;114 115 }

主要实现了两个接口:IPointerDownHandler, IPointerUpHandler,监测按下和抬起事件。

转载于:https://www.cnblogs.com/bzyzhang/p/5411822.html

你可能感兴趣的文章
Ajax与Comet
查看>>
NSStream文件流
查看>>
react组件生命周期
查看>>
MyEclipse6.5安装SVN插件的三种方法
查看>>
Azure Active Directory Connect密码同步问题
查看>>
问题 F: 背包问题
查看>>
又爱又恨的eval
查看>>
LeetCode Next Permutation
查看>>
Treap 实现名次树
查看>>
SSD 单发多框检测
查看>>
Linux下jdk、Tomcat、MySQL的安装
查看>>
06-jQuery的文档操作***
查看>>
HDU 2364 (记忆化BFS搜索)
查看>>
CodeForces 803E Roma and Poker
查看>>
2018 MUltiU 9 dp / 8 upper_bound ; 构造?/
查看>>
初识Redis(一)
查看>>
收藏一下,虽然很多东西还没接触到
查看>>
Python之tkinter中的askyescancel窗口返回值
查看>>
asp.net core系列 55 IS4使用Identity密码保护API
查看>>
ajax发送请求(关于搜索引擎)
查看>>