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

边栏推荐
- [esp32 learning-1] construction of Arduino esp32 development environment
- 基于Redis的分布式ID生成器
- [Leetcode15]三数之和
- MySQL time, time zone, auto fill 0
- idea中导包方法
- MySQL占用内存过大解决方案
- Pytorch: tensor operation (I) contiguous
- js题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
- 基于Redis的分布式锁 以及 超详细的改进思路
- (the first set of course design) 1-4 message passing interface (100 points) (simulation: thread)
猜你喜欢

基于Redis的分布式ID生成器

ESP learning problem record

History object

Design and implementation of general interface open platform - (39) simple and crude implementation of API services

Programmers can make mistakes. Basic pointers and arrays of C language

There is no red exclamation mark after SVN update

Redis 缓存更新策略,缓存穿透、雪崩、击穿问题

Esp8266 uses Arduino to connect Alibaba cloud Internet of things

Custom view puzzle getcolor r.color The color obtained by colorprimary is incorrect

Expected value (EV)
随机推荐
There is no red exclamation mark after SVN update
[leetcode15] sum of three numbers
Use of lists
[golang] leetcode intermediate - fill in the next right node pointer of each node & the k-smallest element in the binary search tree
Unity场景跳转及退出
JS variable types and common type conversions
ORA-02030: can only select from fixed tables/views
SSD technical features
如何给Arduino项目添加音乐播放功能
(4) Data visualization of R language -- matrix chart, histogram, pie chart, scatter chart, linear regression and strip chart
JS Title: input array, exchange the largest with the first element, exchange the smallest with the last element, and output array.
Gateway fails to route according to the service name, and reports an error service unavailable, status=503
Important methods of array and string
Arduino get random number
Servlet
基於Redis的分布式ID生成器
[esp32 learning-1] construction of Arduino esp32 development environment
js 变量作用域和函数的学习笔记
Basic operations of databases and tables ----- classification of data
Understanding of AMBA, AHB, APB and Axi