Header
Unity 2D [E3] - Improve touch in the smallest device
in: Gaming CSharp  So/Code: Generico Ubuntu 10.04 Data: 12/05/2016
Ora: 10.49:12
Articolo visualizzato: 975 volte

The creation of a graphical interface has always been daunting task especially if you want to create something that uses different devices with an infinite number of resolutions, screens etc ...

I thought it was useful to implement something to make the most efficient touch especially in the smaller device which is, without a doubt, an alternative to creating an interface dedicated graphics. An efficient solution is the creation of a collider which manages all of the events that we wish, in order to locate thenearest object compatible with the event and send it. A sort of touch manager.

The collider takes up the whole room and is above all, for convenience I call SmartTouchCollider. This intercepts all events, we want to manage, and through his script locates the nearest object.

How to search for items? 
There are several ways to search:

  1. associate a client script objects and search them with GetComponentsInChildren: 

    SmartTouchClientScript [] = smartTouchClientsMainCameraCanvas MainCameraCanvas.GetComponentsInChildren (); 
     
  2.  Search for collider 

    MainCameraCanvas.GetComponentsInChildren ();


So here's an example of operation:

Through the SmartTouchScript associated with SmartTouchCollider you catch the OnPointerDown event (for example, but you can manage them all), you find the object (Object 1) closer, and you send the event to Object 1 through SendMessage.

 

public void OnPointerDown (PointerEventData eventData)
{
	try {
		GameObject obj = getNearestSmartTouchClient (eventData.position, "OnPointerDown");
		if (obj != null) {
			obj.SendMessage ("OnPointerDown", eventData);
		}

	} catch (Exception e) {
		Debug.Log ("Ext" + e.ToString ());
	}
}

This generally is the basic idea to handle the touch, adding a tolerance, that is, a value indicating the maximum distance in which to search for an object, it becomes really efficient.

In SmartTouchScriptClient, in case you have chosen the search through the script, simply aggiunere events to which the object is responsible.

There the SmartTouchScript and Client example. Obviously for clarification, advice, suggestions leave a comment.

using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
using System.Collections.Generic;
using System;

public class SmartTouchScript : MonoBehaviour, IPointerClickHandler, IPointerUpHandler, IPointerDownHandler
{
	private GameObject MainCameraCanvas;
	private GameObject MainCamera;

	float tollerance = 2;

	void Awake ()
	{
		base.Init ();
		MainCameraCanvas = GameObject.Find ("MainCameraCanvas");

	}

	// Use this for initialization
	void Start ()
	{

	}

	public void OnPointerUp (PointerEventData eventData)
	{
		try {
			GameObject obj = getNearestSmartTouchClient (eventData.position, "OnPointerUp");
			if (obj != null) {
				obj.SendMessage ("OnPointerUp", eventData);
			}

		} catch (Exception e) {
			Debug.Log ("Ext" + e.ToString ());
		}
	}

	public void OnPointerDown (PointerEventData eventData)
	{
		try {
			GameObject obj = getNearestSmartTouchClient (eventData.position, "OnPointerDown");
			if (obj != null) {
				obj.SendMessage ("OnPointerDown", eventData);
			}

		} catch (Exception e) {
			Debug.Log ("Ext" + e.ToString ());
		}
	}

	
	public GameObject getNearestSmartTouchClient (Vector2 position, String eventToSearch)
	{
		GameObject nearestObject = null;
		float nearestDistance = 0.0f;

		//position to world point
		position = Camera.main.ScreenToWorldPoint (position);
			
		// search in canvas
		SmartTouchClientScript[] smartTouchClientsMainCameraCanvas = MainCameraCanvas.GetComponentsInChildren ();
		List smartTouchClients = new List (smartTouchClientsMainCameraCanvas);

		foreach (SmartTouchClientScript c in smartTouchClients) {
			
			// if events it's in list
			if (Array.IndexOf (c.responseEvents, eventToSearch) > -1) {
				Vector3 objectPosition = new Vector3 (c.gameObject.transform.position.x, c.gameObject.transform.position.y);
				float actualDistance = Vector2.Distance (position, new Vector2 (objectPosition.x, objectPosition.y));
				if (actualDistance < tollerance) {
					if (nearestObject == null) {
						nearestDistance = actualDistance;
						nearestObject = c.gameObject;
					} else {
						if (actualDistance < nearestDistance) {
							nearestDistance = actualDistance;
							nearestObject = c.gameObject;
						}
					}
				}
			}
		}
		return nearestObject;

	}

}
using UnityEngine;
using System.Collections;

public class SmartTouchClientScript : MonoBehaviour {

	public string[] responseEvents;

	// Use this for initialization
	void Start () {
	
	}	

}

Commenti Commenti (0) | User Autore: Guido Camerlingo (Guiz)
Tags: unity 2D videogame touch improve migliorare small devices piccoli dispositivi implementare





Articoli Correlati
Unity 2D [E3] - Improve touch in the smallest device
Privacy - Carrier IQ lo spione
Greenpois0n : la guida all'altro JailBreak per iOS 4.1
Anche Alcatel presenterà un Tablet Android
Ubuntu 10.10 Maverick Meerkat RC (Release Candidate)

Commenti

Scrivi Commento
Codice Verifica

Commento massimo 5000 caratteri.(Tutti i campi contrassegnati da * sono obbligatori).

   

Ricerca

Glossario Naviga nel nostro glossario!
Scopri il gergo dei Geek!


 ULTIMI ARTICOLI


 Giochi in uscita oggi

Nessuna uscita prevista


 [EN] Last Articles

 Siti Amici

Visita il Blog Roll
Contattaci! Diventa nostro amico!



 Amazon


 Hot Downloads

3550

 Categorie

79
163
57
87
13
2
20
36
1
2
5
5
128
7
24
14
57
45

 Code

11
11
3
5
23
1
7

 Games

19
45
13

 GUIDE


 Novità Downloads

61
35
3550

 Tags Cloud

smartphone aggiornamento per 2g Wildfire sostituzione foto di copy a inclusione un scoperto copy tipi scoperto avviso rimosse file



 CopyRight

Valid XHTML 1.0 Transitional CSS Valido! [Valid RSS] Creative Commons License


Geek-Blog by Flavio Mandato, Giuseppe Vaccaro, Guido Camerlingo, Stefano Natale, Domenico Cavallo is licensed under a Creative Commons Attribuzione-Non opere derivate 2.5 Italia License.
Based on a work at www.geek-blog.it.
Permissions beyond the scope of this license may be available at http://www.geek-blog.it/

Disclaimer - Responsabilità - Pagina generata in 0.117 secondi. Geek-Blog.it