当前位置:网站首页>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 = "";
}
}
四、全部实现

边栏推荐
- JS variable types and common type conversions
- JS正则表达式基础知识学习
- Servlet
- @Autowired 和 @Resource 的区别
- (课设第一套)1-4 消息传递接口 (100 分)(模拟:线程)
- (1) Introduction Guide to R language - the first step of data analysis
- Naive Bayesian theory derivation
- Gateway fails to route according to the service name, and reports an error service unavailable, status=503
- Générateur d'identification distribué basé sur redis
- NRF24L01故障排查
猜你喜欢
![[Nodejs] 20. Koa2 onion ring model ----- code demonstration](/img/a8/a4390238685903b63bb036206f8dcb.jpg)
[Nodejs] 20. Koa2 onion ring model ----- code demonstration

Latex learning

JS正则表达式基础知识学习

Vulnhub target: hacknos_ PLAYER V1.1

Redis based distributed ID generator

js 变量作用域和函数的学习笔记

Derivation of logistic regression theory

(3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis

Gravure sans fil Bluetooth sur micro - ordinateur à puce unique

(一)R语言入门指南——数据分析的第一步
随机推荐
[leetcode15] sum of three numbers
(五)R语言入门生物信息学——ORF和序列分析
Unity场景跳转及退出
ESP learning problem record
Esp8266 connects to onenet cloud platform (mqtt) through Arduino IDE
[Clickhouse kernel principle graphic explanation] about the collaborative work of partitioning, indexing, marking and compressed data
Remember an experience of ECS being blown up by passwords - closing a small black house, changing passwords, and changing ports
2022.2.12 resumption
Unity3D制作注册登录界面,并实现场景跳转
There is no red exclamation mark after SVN update
@The difference between Autowired and @resource
如何给Arduino项目添加音乐播放功能
Important methods of array and string
(四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图
[leetcode622]设计循环队列
[offer78]合并多个有序链表
js题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
记一次云服务器被密码爆破的经历——关小黑屋、改密码、改端口
Unity3d camera, the keyboard controls the front and rear left and right up and down movement, and the mouse controls the rotation, zoom in and out
Imgcat usage experience