当前位置:网站首页>Fairygui joystick
Fairygui joystick
2022-07-06 12:39:00 【SQ Liu】
FairyGUI rocker
Material resources :
One 、 preparation
1、 establish FairyGUI The new project
2、 Import material
3、 The new button
4、 Edit button
5、 Set the scope
6、 Set correlation
7、 Packaging releases
Two 、Unity It shows that
1、 establish Unity The new project
2、 Import FairyGUI Of unity Bao He DOTween package
3、Unity It shows that
3、 ... and 、 Script control
1、 newly build 2 Script
One is RockingBarMain, Need to be mounted on UIPanel On ; The other is the script of the rocker itself RockingBar.
2、 coded
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FairyGUI;
using DG.Tweening;
public class RockingBar : EventDispatcher // Event sending and receiving class
{
// The monitor of the event
public EventListener onMove {
get; private set; } // Set a security permission
public EventListener onEnd {
get; private set; }
//mainUI The object in
private GButton rockingbarButton;
private GObject thumb;
private GObject touchArea;
private GObject center;
// Properties of the rocker
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);
}
// Start touching
private void OnTouchBegin(EventContext context)
{
if (touchID == -1) // First touch
{
InputEvent inputEvent = (InputEvent)context.data;
touchID = inputEvent.touchId;
if (tweener != null)
{
tweener.Kill(); // Kill the last animation
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; // The angle of arc
thumb.rotation = degrees + 90;
context.CaptureTouch();
}
}
// Mobile touch
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;
// set range
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);
}
}
// End touch
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 = "";
}
}
Four 、 All implementation
边栏推荐
- Redis cache update strategy, cache penetration, avalanche, breakdown problems
- Acwing-116 pilot brother
- Problèmes avec MySQL time, fuseau horaire, remplissage automatique 0
- (3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis
- SSD technical features
- Esp8266 connects to bafayun (TCP maker cloud) through Arduino IED
- (1) Introduction Guide to R language - the first step of data analysis
- C programming exercise
- (5) Introduction to R language bioinformatics -- ORF and sequence analysis
- Stm32f1+bc20+mqtt+freertos system is connected to Alibaba cloud to transmit temperature and humidity and control LED lights
猜你喜欢
Easy to use shortcut keys in idea
Vulnhub target: hacknos_ PLAYER V1.1
[Nodejs] 20. Koa2 onion ring model ----- code demonstration
单片机蓝牙无线烧录
Postman 中级使用教程【环境变量、测试脚本、断言、接口文档等】
FairyGUI复选框与进度条的组合使用
Pat 1097 duplication on a linked list (25 points)
Types de variables JS et transformations de type communes
Redis 缓存更新策略,缓存穿透、雪崩、击穿问题
JS Title: input array, exchange the largest with the first element, exchange the smallest with the last element, and output array.
随机推荐
Theoretical derivation of support vector machine
idea问题记录
基于Redis的分布式锁 以及 超详细的改进思路
Basic operations of databases and tables ----- view data tables
Symbolic representation of functions in deep learning papers
JS正则表达式基础知识学习
JS變量類型以及常用類型轉換
Redis cache update strategy, cache penetration, avalanche, breakdown problems
JUC forkjoin and completable future
Intermediate use tutorial of postman [environment variables, test scripts, assertions, interface documents, etc.]
About using @controller in gateway
(五)R语言入门生物信息学——ORF和序列分析
Stm32f1+bc20+mqtt+freertos system is connected to Alibaba cloud to transmit temperature and humidity and control LED lights
Pytorch: tensor operation (I) contiguous
Classification, understanding and application of common methods of JS array
Arduino get random number
(3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis
Whistle+switchyomega configure web proxy
C programming exercise
[Leetcode15]三数之和