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

边栏推荐
- Flink late data processing (3)
- ORA-02030: can only select from fixed tables/views
- Vulnhub target: hacknos_ PLAYER V1.1
- Detailed explanation of truncate usage
- [offer18] delete the node of the linked list
- Basic operations of databases and tables ----- view data tables
- Esp8266 connects to bafayun (TCP maker cloud) through Arduino IED
- By v$rman_ backup_ job_ Oracle "bug" caused by details
- open-mmlab labelImg mmdetection
- Arduino JSON data information parsing
猜你喜欢

The dolphin scheduler remotely executes shell scripts through the expect command

Programming homework: educational administration management system (C language)

记一次云服务器被密码爆破的经历——关小黑屋、改密码、改端口

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

Fashion Gen: the general fashion dataset and challenge paper interpretation & dataset introduction

Redis cache update strategy, cache penetration, avalanche, breakdown problems

Navigator object (determine browser type)

基于Redis的分布式ID生成器

Office提示您的许可证不是正版弹框解决

Expected value (EV)
随机推荐
Embedded startup process
如何给Arduino项目添加音乐播放功能
Postman 中级使用教程【环境变量、测试脚本、断言、接口文档等】
2021.11.10 compilation examination
Expected value (EV)
Easy to use shortcut keys in idea
[Leetcode15]三数之和
Esp8266 connects to onenet cloud platform (mqtt) through Arduino IDE
ESP learning problem record
Derivation of logistic regression theory
[899] ordered queue
Unity场景跳转及退出
How to add music playback function to Arduino project
HCIP Day 12
记一次云服务器被密码爆破的经历——关小黑屋、改密码、改端口
[leetcode622] design circular queue
(五)R语言入门生物信息学——ORF和序列分析
Générateur d'identification distribué basé sur redis
SSD technical features
ES6 grammar summary -- Part 2 (advanced part es6~es11)