- Home /
Android touch gui help!!!
I really want to make it so when you touch an npc it displays some gui, like a text box. Then next to it a few buttons and when you click the buttons its shows another text box and so on its for an android RPG I'm making and I'm really stuck with getting it so when you touch a npc it shows gui. im a massive newbie to scripting so please explain in detail. i know its a lot to ask but PLEASE HELP, thanks
#pragma strict
var show : boolean;
var show2 : boolean;
var target : Transform;
var screenPosX : Vector3 = camera.WorldToScreenPoint (target.position);
print ("target is " + screenPosX.x + " pixels from the left");
var screenPosY : Vector3 = camera.WorldToScreenPoint (target.position);
print ("target is " + screenPosY.y + " pixels from the top");
// screen position of gameobject
var oSPX = Mathf.Round(screenPosX.x / 100);
var oSPY = Mathf.Round(screenPosY.y / 100);
if(show2 === true);
{
}
function Update () {
for (var touch : Touch in Input.touches) {
if (touch.phase == TouchPhase.Began) {
show = true;
Debug.Log("niceballsTrue");
Debug.Log(show);
var touchPositionXRounded = Mathf.Round(touch.position.x / 100);
var touchPositionYRounded = Mathf.Round(touch.position.y / 100);
}
else
{
show = false;
Debug.Log("False");
Debug.Log(show);
}
if(show === true)
{
Debug.Log("OMG IT WORKSish");
}
else if (touchPositionXRounded == oSPX)
{
if (touchPositionYRounded == oSPY) {
show2 = true;
}
Debug.Log("ITS ALIVE":)
}
else
{
Debug.Log("printed false :(");
}
}
}
so far ive done this
function OnGUI () {
var position = Vector2(0,0); for (var touch : Touch in Input.touches) { if (touch.phase == TouchPhase.Stationary) { if (touch.position == touch) { (GUI.Box(Rect(0,0,100,20),"Example")); } } } }
Answer by Michael CMS · Nov 03, 2012 at 02:31 PM
What you want to do is to test if the touch is on top of your npc :
RaycastHit hit;
Collider m_Collider
if (m_Collider.Raycast(
Camera.main.ScreenPointToRay(
new Vector3(
Input.Touches[0].position.x,
Input.Touches[0].position.y,
0),
out hit,
Mathf.Infinity))
{
//show gui
}
This is C# code, but you get the point :). Also this wasn't compiled , so you might need to test for some errors.
In the code above m_Collider is a collider component placed on your npc. This script should go on the NPC as well, and the code snippet above should go on the Update function.
thx i have fixed it now but the gui dissapeers when i let go :(
Add a boolean, and have the touch switch the boolean on when you touch it. When the boolean is true, GUI shows up. When it's false, it doesn't. Once you've gone through all the buttons you need, have the last one set the GUI boolean to false.
Thanks @Itinerant im strugging with that though :( i will post where i am at the moment as an edit :)
Answer by shaystibelman · Nov 13, 2012 at 04:58 PM
This is my TOUCH script. I have attached it to the player and get all my touch effects from there (that way I run the ray test only once). In your case you could attach it to a GUI that will check if it has been touched (maybe with a variable) in order to display the GUI.
function Update(){
#if UNITY_ANDROID
if (Input.touchCount > 0)
{
var theTouch : Touch = Input.GetTouch (0);
touchPos = theTouch.position;
touchPos.z = 1;
var ray : Ray = cam.ScreenPointToRay(touchPos);
var hit : RaycastHit;
if(Physics.Raycast(ray,hit,1000.0))
{
if (Input.touchCount == 1 && theTouch.phase == TouchPhase.Began/* && Target.collider.gameObject.tag=="Product"*/)
{
Target = hit.collider.gameObject;
TargetGrabFunction = Target.GetComponent(GrabNRotate);
TargetGrabFunction.bringOverInt = 1;
}
}
}
#endif
}