当前位置:网站首页>FairyGUI摇杆
FairyGUI摇杆
2022-07-06 09:18:00 【SQ刘】
FairyGUI摇杆
素材资源:
一、准备工作
1、创建FairyGUI新项目
2、导入素材
3、新建按钮
4、编辑按钮
5、设置作用区域
6、设置关联
7、打包发布
二、Unity中显示
1、创建Unity新项目
2、导入FairyGUI的unity包和DOTween包
3、Unity中显示
三、脚本控制
1、新建2个脚本
一个是RockingBarMain,需要挂载在UIPanel上;另一个是摇杆本身脚本RockingBar。
2、编码实现
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FairyGUI;
using DG.Tweening;
public class RockingBar : EventDispatcher //事件收发类
{
//事件的监听者
public EventListener onMove {
get; private set; } //设置了一个安全权限
public EventListener onEnd {
get; private set; }
//mainUI里的对象
private GButton rockingbarButton;
private GObject thumb;
private GObject touchArea;
private GObject center;
//摇杆的属性
private float initX;
private float initY;
private float startStageX;
private float startStageY;
private float lastStageX;
private float lastStageY;
private int touchID;
private int radius {
get; set; }
private Tweener tweener;
public RockingBar(GComponent mainUI)
{
onMove = new EventListener(this,"onMove");
onEnd = new EventListener(this, "onEnd");
rockingbarButton = mainUI.GetChild("RockingBar").asButton;
rockingbarButton.changeStateOnClick = false;
thumb = rockingbarButton.GetChild("thumb");
touchArea = mainUI.GetChild("RockingBarTouchArea");
center = mainUI.GetChild("RockingBarCenter");
initX = center.x + center.width / 2;
initY = center.y + center.height / 2;
touchID = -1;
radius = 150;
touchArea.onTouchBegin.Add(OnTouchBegin);
touchArea.onTouchMove.Add(OnTouchMove);
touchArea.onTouchEnd.Add(OnTouchEnd);
}
//开始触摸
private void OnTouchBegin(EventContext context)
{
if (touchID == -1) //第一次触摸
{
InputEvent inputEvent = (InputEvent)context.data;
touchID = inputEvent.touchId;
if (tweener != null)
{
tweener.Kill(); //杀死上一个动画
tweener = null;
}
Vector2 localPos = GRoot.inst.GlobalToLocal(new Vector2(inputEvent.x, inputEvent.y));
float posX = localPos.x;
float posY = localPos.y;
rockingbarButton.selected = true;
lastStageX = posX;
lastStageY = posY;
startStageX = posX;
startStageY = posY;
center.visible = true;
center.SetXY(posX - center.width / 2, posY - center.height / 2);
rockingbarButton.SetXY(posX - rockingbarButton.width / 2, posY - rockingbarButton.height / 2);
float deltaX = posX - initX;
float deltaY = posY - initY;
float degrees = Mathf.Atan2(deltaY, deltaX) * 180 / Mathf.PI; //弧度转角度
thumb.rotation = degrees + 90;
context.CaptureTouch();
}
}
//移动触摸
private void OnTouchMove(EventContext context)
{
InputEvent inputEvent = (InputEvent)context.data;
if (touchID != -1 && inputEvent.touchId == touchID)
{
Vector2 localPos = GRoot.inst.GlobalToLocal(new Vector2(inputEvent.x, inputEvent.y));
float posX = localPos.x;
float posY = localPos.y;
float moveX = posX - lastStageX;
float moveY = posY - lastStageY;
lastStageX = posX;
lastStageY = posY;
float buttonX = rockingbarButton.x + moveX;
float buttonY = rockingbarButton.y + moveY;
float deltaX = buttonX + rockingbarButton.width / 2 - startStageX;
float deltaY = buttonY + rockingbarButton.height / 2 - startStageY;
float rad = Mathf.Atan2(deltaY, deltaX);
float degree = rad * 180 / Mathf.PI;
thumb.rotation = degree + 90;
//设置范围
float maxX = radius * Mathf.Cos(rad);
float maxY = radius * Mathf.Sin(rad);
if (Mathf.Abs(deltaX) > Mathf.Abs(maxX))
{
deltaX = maxX;
}
if (Mathf.Abs(deltaY) > Mathf.Abs(maxY))
{
deltaY = maxY;
}
buttonX = startStageX + deltaX;
buttonY = startStageY + deltaY;
rockingbarButton.SetXY(buttonX - rockingbarButton.width / 2, buttonY - rockingbarButton.height / 2);
onMove.Call(degree);
}
}
//结束触摸
private void OnTouchEnd(EventContext context)
{
InputEvent inputEvent = (InputEvent)context.data;
if (touchID != -1 && inputEvent.touchId == touchID)
{
touchID = -1;
thumb.rotation = thumb.rotation + 180;
center.visible = false;
tweener = rockingbarButton.TweenMove(new Vector2(initX - rockingbarButton.width / 2, initY - rockingbarButton.height / 2), 0.3f).OnComplete(() =>
{
tweener = null;
rockingbarButton.selected = false;
thumb.rotation = 0;
center.visible = true;
center.SetXY(initX - center.width / 2, initY - center.height / 2);
;
}
);
}
onEnd.Call();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FairyGUI;
public class RockingBarMain : MonoBehaviour
{
private GComponent mainUI;
private GTextField gTextField;
private RockingBar rockingBar;
void Start()
{
mainUI = GetComponent<UIPanel>().ui;
gTextField = mainUI.GetChild("n4").asTextField;
rockingBar = new RockingBar(mainUI);
rockingBar.onMove.Add(RockingBarMove);
rockingBar.onEnd.Add(RockingBarEnd);
}
// Update is called once per frame
void Update()
{
}
private void RockingBarMove(EventContext context)
{
float degree = (float)context.data;
gTextField.text = degree.ToString();
}
private void RockingBarEnd()
{
gTextField.text = "";
}
}
四、全部实现
边栏推荐
- [leetcode19]删除链表中倒数第n个结点
- Single chip Bluetooth wireless burning
- Learning notes of JS variable scope and function
- Kconfig Kbuild
- 基于Redis的分布式ID生成器
- JS Title: input array, exchange the largest with the first element, exchange the smallest with the last element, and output array.
- Navigator object (determine browser type)
- Imgcat usage experience
- Programmers can make mistakes. Basic pointers and arrays of C language
- Use of lists
猜你喜欢
CUDA C programming authoritative guide Grossman Chapter 4 global memory
Database course design: college educational administration management system (including code)
SVN更新后不出现红色感叹号
记一次云服务器被密码爆破的经历——关小黑屋、改密码、改端口
Redis 缓存更新策略,缓存穿透、雪崩、击穿问题
[Red Treasure Book Notes simplified version] Chapter 12 BOM
Unity3D,阿里云服务器,平台配置
ES6 grammar summary -- Part I (basic)
Postman 中级使用教程【环境变量、测试脚本、断言、接口文档等】
Custom view puzzle getcolor r.color The color obtained by colorprimary is incorrect
随机推荐
[offer9] implement queues with two stacks
@Autowired 和 @Resource 的区别
Minio文件下载问题——inputstream:closed
Basic operations of databases and tables ----- modifying data tables
Symbolic representation of functions in deep learning papers
[leetcode19] delete the penultimate node in the linked list
Walk into WPF's drawing Bing Dwen Dwen
(4) Data visualization of R language -- matrix chart, histogram, pie chart, scatter chart, linear regression and strip chart
How to add music playback function to Arduino project
First use of dosbox
[offer29] sorted circular linked list
Mp3mini playback module Arduino < dfrobotdfplayermini H> function explanation
Important methods of array and string
程序设计大作业:教务管理系统(C语言)
Pat 1097 duplication on a linked list (25 points)
(一)R语言入门指南——数据分析的第一步
Solution to the problem of automatic login in Yanshan University Campus Network
单片机蓝牙无线烧录
记一次云服务器被密码爆破的经历——关小黑屋、改密码、改端口
Fashion Gen: the general fashion dataset and challenge paper interpretation & dataset introduction