UI 多点触控架构?
UI Multitouch Architecture?
我正在开发一款带有多点触控控制器的游戏。但是我有问题 UI Image multi touching
我想创建这个架构。
绿色循环将用作按钮。 (触摸结束)
红色循环将作为 button.I 需要触摸移动或触摸停留事件进行连拍。 (触摸移动 | 触摸停留)
黄框将用作触摸板(UI 图像元素)。当手指在方框上移动时,会触发Rotate方法。
我尝试了一些方法,但都失败了。
我创建了一个名为 MultiTouchElement 的 class。
var touches = Input.touches;
for (int i = 0; i < touches.Count; i++)
{
Touch touch = Input.GetTouch(i);
TouchPhase phase = touch.phase;
PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
eventDataCurrentPosition.position = touch.position;
List<RaycastResult> results = new List<RaycastResult>();
EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
var touchOnThisObject = results.Any(x => x.gameObject.name.ToLower() == this.gameObject.name.ToLower());
if (!touchOnThisObject)
return;
if (phase == TouchPhase.Began)
{
this.GetComponent<Image>().color = Color.red;
}
if (phase == TouchPhase.Ended)
{
this.GetComponent<Image>().color = Color.blue;
}
}
有没有关于我的架构的教程?
我找到了解决方案:)
谢谢兄弟@IsGreen
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;
public class UImageRayCast : MonoBehaviour
{
Image image;
Color colorOn, colorOff;
void Start()
{
this.image = this.GetComponent<Image>();
this.colorOff = this.image.color;
this.colorOn = new Color(this.colorOff.r, this.colorOff.g, this.colorOff.b, this.colorOff.a * 0.5f);
}
void Update()
{
this.image.color = this.colorOff;
PointerEventData pointer = new PointerEventData(EventSystem.current);
List<RaycastResult> raycastResult = new List<RaycastResult>();
foreach (Touch touch in Input.touches)
{
pointer.position = touch.position;
EventSystem.current.RaycastAll(pointer, raycastResult);
foreach (RaycastResult result in raycastResult)
{
if (result.gameObject == this.gameObject)
{
if(touch.phase == TouchPhase.Began)
{
Debug.Log("Began " + result.gameObject.name );
}
else if(touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary)
{
this.image.color = this.colorOn;
}
else if (touch.phase == TouchPhase.Ended)
{
Debug.Log("End " + result.gameObject.name);
}
//this.gameObject.transform.position = touch.position;
//Do Stuff
}
}
raycastResult.Clear();
}
}
我正在开发一款带有多点触控控制器的游戏。但是我有问题 UI Image multi touching
我想创建这个架构。
绿色循环将用作按钮。 (触摸结束)
红色循环将作为 button.I 需要触摸移动或触摸停留事件进行连拍。 (触摸移动 | 触摸停留)
黄框将用作触摸板(UI 图像元素)。当手指在方框上移动时,会触发Rotate方法。
我尝试了一些方法,但都失败了。
我创建了一个名为 MultiTouchElement 的 class。
var touches = Input.touches;
for (int i = 0; i < touches.Count; i++)
{
Touch touch = Input.GetTouch(i);
TouchPhase phase = touch.phase;
PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
eventDataCurrentPosition.position = touch.position;
List<RaycastResult> results = new List<RaycastResult>();
EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
var touchOnThisObject = results.Any(x => x.gameObject.name.ToLower() == this.gameObject.name.ToLower());
if (!touchOnThisObject)
return;
if (phase == TouchPhase.Began)
{
this.GetComponent<Image>().color = Color.red;
}
if (phase == TouchPhase.Ended)
{
this.GetComponent<Image>().color = Color.blue;
}
}
有没有关于我的架构的教程?
我找到了解决方案:)
谢谢兄弟@IsGreen
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;
public class UImageRayCast : MonoBehaviour
{
Image image;
Color colorOn, colorOff;
void Start()
{
this.image = this.GetComponent<Image>();
this.colorOff = this.image.color;
this.colorOn = new Color(this.colorOff.r, this.colorOff.g, this.colorOff.b, this.colorOff.a * 0.5f);
}
void Update()
{
this.image.color = this.colorOff;
PointerEventData pointer = new PointerEventData(EventSystem.current);
List<RaycastResult> raycastResult = new List<RaycastResult>();
foreach (Touch touch in Input.touches)
{
pointer.position = touch.position;
EventSystem.current.RaycastAll(pointer, raycastResult);
foreach (RaycastResult result in raycastResult)
{
if (result.gameObject == this.gameObject)
{
if(touch.phase == TouchPhase.Began)
{
Debug.Log("Began " + result.gameObject.name );
}
else if(touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary)
{
this.image.color = this.colorOn;
}
else if (touch.phase == TouchPhase.Ended)
{
Debug.Log("End " + result.gameObject.name);
}
//this.gameObject.transform.position = touch.position;
//Do Stuff
}
}
raycastResult.Clear();
}
}