当前位置:网站首页>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 = "";
}
}
四、全部实现
边栏推荐
- Programmers can make mistakes. Basic pointers and arrays of C language
- . elf . map . list . Hex file
- 程序设计大作业:教务管理系统(C语言)
- dosbox第一次使用
- Knowledge summary of request
- Basic operations of databases and tables ----- classification of data
- ES6 grammar summary -- Part I (basic)
- MySQL时间、时区、自动填充0的问题
- [esp32 learning-2] esp32 address mapping
- Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)
猜你喜欢
Stm32f1+bc20+mqtt+freertos system is connected to Alibaba cloud to transmit temperature and humidity and control LED lights
Redis 缓存更新策略,缓存穿透、雪崩、击穿问题
MySQL time, time zone, auto fill 0
VSCode基础配置
JS数组常用方法的分类、理解和运用
编译原理:源程序的预处理及词法分析程序的设计与实现(含代码)
Vulnhub target: hacknos_ PLAYER V1.1
Unity3d makes the registration login interface and realizes the scene jump
Database course design: college educational administration management system (including code)
CUDA C programming authoritative guide Grossman Chapter 4 global memory
随机推荐
Unity3D,阿里云服务器,平台配置
level16
1081 rational sum (20 points) points add up to total points
[Offer18]删除链表的节点
Mp3mini playback module Arduino < dfrobotdfplayermini H> function explanation
HCIP Day 12
The dolphin scheduler remotely executes shell scripts through the expect command
JS function promotion and declaration promotion of VaR variable
Who says that PT online schema change does not lock the table, or deadlock
JS变量类型以及常用类型转换
Working principle of genius telephone watch Z3
Mysqldump error1066 error solution
关于Gateway中使用@Controller的问题
Unity场景跳转及退出
PT OSC deadlock analysis
ESP8266连接onenet(旧版MQTT方式)
open-mmlab labelImg mmdetection
Talking about the startup of Oracle Database
[Red Treasure Book Notes simplified version] Chapter 12 BOM
1041 Be Unique (20 point(s))(哈希:找第一个出现一次的数)