Hey guys, I’m been struggling with a problem the last week and now I’m tired of it and hopping to get an easy explanation and solution on how to just add something to any text on a scene so that touch work on it on a mobile platform as Android or IOS…
I just want to be able to click on it as an button an a scene will load, nothing more.
You really should title your question as a question rather than a "subtitle". Something like "How to make text interact with touch on any mobile platform?"
function OnGUI()
{
var hit = GUI.Button(Rect(x,y,width,height), "Some text");
if(hit) Application.LoadLevel(name);
}
Explanation: GUI.Button creates a button at the desired x,y coordinates having the size of width and height, and is labeled with “Some text”. This function returns true if the user clicked the button. So if the button has been clicked, Application.LoadLevel is called, which loads the scene by name (where name is just a string). Note that this code only works within an OnGUI function definition.
function Update()
{
if(Input.GetMouseButtonUp(0))
{
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit))
{
Debug.Log(hit.transform.gameObject.name + " has been clicked.");
}
}
}
Explanation: In each frame we test if the user has used its left mouse button (thus clicked). If yes, we take the current mouse position and create a ray from it pointing down the viewing direction of the current camera. Physics.Raycast returns true, if the ray has hit a GameObject with a collider attached to it.
From @japanitrat: "Explanation: GUI.Button creates a button at the desired x,y coordinates having the size of width and height, and is labeled with "Some text"". This means that YOU have to define those parameters in according to the desired button and not simply copy and paste them.
You really should title your question as a question rather than a "subtitle". Something like "How to make text interact with touch on any mobile platform?"
– cregox