system
1
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.
system
2
You have two options. First, a 2D Button:
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.
Second option is to make a 3D object clickable:
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.
This is as easy as it gets to be.