2015-11-18 2 views
1

Я разрабатываю игру с мультитач-контроллерами. Но у меня проблемы с UI Image multi touchingUI Multitouch Architecture?

Я хочу создать эту архитектуру.

I want to create this architecture

  • Зеленые циклы будут работать как кнопки. (Touch End)

  • Red Cycle будет работать как кнопка. Мне нужно сенсорное перемещение или прикосновение к событиям остановки для серийного выстрела. (Touch Move | Touch Stay)

  • Желтая коробка будет работать как тачпад (элемент изображения UI). Когда палец перемещается над полем, он вызывает метод Rotate.

Я пробовал некоторые методы, но все не удалось.

Я создал класс с именем MultiTouchElement.

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; 
    } 
} 

Есть ли у меня учебник для моей архитектуры?

ответ

0

Я нашел решение :)

Спасибо Bro @IsGreen

Источник: http://forum.unity3d.com/threads/solved-raycast-multitouch-input-convert-local-recttransform-rect-to-screen-position.318115/

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(); 

    } 

}