- Home /
The question is answered, right answer was accepted
Blocking user interaction when a prompt is displayed
Hello guys,
I'm new to Unity and please don't be too harsh with me on this if it's something simple to achieve.
I've created a "social" game-like map, with a "map-drag" functionality, "igm", and currently leveling up function on zooming in. I've also created prompts for level up and no energy.
I am currently trying to make anything unrelated to the prompts inaccessible. I've already tried with Time.timeScale = 0.0 and then 1.0 but that does not help.
Any ideas?
Thanks in advance, Eugen
Thanks for all your help Fattie, I managed just great. I'm sure that your tutorial below will prove itself useful to many Unity newbies like me ^_^. Thanks for everything. I would close the question but I'm not sure how :).
You have closed it ! Everything is perfect and I want you to have a great xmas. I'm sure everyone on the list is looking forwards to your next excellent question !!!
Okey then, thank you. I'm going to continue asking as much as I can even if I don't get answers for them I'll post whatever I come up with. $$anonymous$$erry Christmas and a Happy New Year to you too ^_^!
Answer by Fattie · Dec 15, 2012 at 05:38 PM
regarding buttons: a good possibility is,
don't use the "GUI" system at all. use IMAGE buttons that you sit there (perhaps using 2DToolkit for example).
then just write your own "buttons" (a collider with 2 lines of code). in this way you can very easily completely control everything.
bear in mind that many people feel that Unity's "GUI" system is rubbish, and just don't use it. (for me it's just a placeholder system, for development etc.)
also - there are various alternate GUI systems you can buy for €2 on the asset store. but (IMO) they are very inelegant (anything that takes more than 20 seconds to learn is not for me) - but that could be a solution for you. (I am sure they would do everything you describe, and much more)
Now, here's some totally typical - not elegant at all !!!!!! -- code for buttons.
make a scene, say with some background images or whatever
simply make the buttons floating in space so they look correct for the user (They could be animated, exploding, anything - they could be 2DToolkit flat sprites, or, actually just 3D spaceships, neon lights or whatever you want)
now, make an empty game object called "my happy buttons". inside that game object (folder if you will), make a number of simply "cubes"... sit them sort of between your camera and the button images
on the cubes, REMOVE the renderer so it's nothing more than a box collider. make these colliders fairly flat, and the correct shape of the buttons (and probably a bit bigger on iPad). those are in fact the buttons. you must carefully name those objects (the buttons) correctly (eg, from the example below, "about" etc)
To be clear, this all "shapes" better if your project is using an orthographic camera, but it will be fine in 3D camera, too. you may prefer, or have to, make another camera (ortho) to handle just the buttons - but that's just a click. it's no problem having two cameras in a scene and one should learn about this anyway. So, your "buttons" (nothing more than trivial colliders) will look like this ..
in the example code below those three colliders would be named "about" "home" etc. Note that the actual "buttons" (the colliders) do absolutely nothing - they have no code attached whatsoever. the only quantum feature they possess is their name string.
(TBC, it's perfectly OK to use spheres, planes or anything you want as the colliders there. Also, recall that people's fingers are enormous compared to the glass, accuracy is not important.)
Note as a general comment in unity you can and should learn about the physics Layers system - you can use that so that your Ninjas, etc do not collide with the "about" button! heh. /Documentation/Components/LayerBasedCollision.html
Note that the code example below includes full working code for the unity editor too. In practice, you have to have that or it's impossible to work with - often the stupid editor code takes more fooling around with than the real code for the glass on the ipad.
Note that it uses the extremely simple touch metaphor that the user must ONLY be using one finger. In practice (particularly for children's apps) you have to allow for all sorts of screwing around by users - think about how people grab the edges of screens, how chidren touch screens and so on. In practice that software takes BILLIONS of lines of code and is often more complex than the whole other software project! heh. But here you simply must touch cleanly with one finger
{To investigate the intricacies of touch programming, consider posts such as
http://answers.unity3d.com/questions/326253/strange-touch-behavior.html
http://answers.unity3d.com/questions/292333/how-to-calculate-swipe-speed-on-ios.html }
Recall that writing glass code is "haiku coding" or "koan coding" - the end result can be very short indeed but it is very critical and tiny changes make a huge difference.
to impress clients
you can write code all winter
but not for the glass
this is not as good as "i see an old pond / in jumps a silly froggy / i can hear the sound" but you get the idea :)
// an impossibly trivial instructions page. it has three buttons.
//
#pragma strict
private var clicks:ClicksAudio; // just some audio for clicking, beeps, etc
function Awake()
{
theCam = GameObject.Find("_screenscam").GetComponent(Camera);
// it's important to get the correct camera
clicks = GameObject.Find("screensAudio").GetComponent(ClicksAudio);
}
function Start()
{
}
function buttonNamedPressed(nn:String)
{
if ( nn == "about" )
{
clicks.select();
Application.LoadLevelAsync( "ScreenAbout" );
return;
}
if ( nn == "credits" )
{
clicks.select();
Application.LoadLevelAsync( "ScreenCredits" );
return;
}
if ( nn == "home" )
{
clicks.back();
Application.LoadLevelAsync( "ScreenHome" );
return;
}
}
function OnGUI ()
{
_basicButtons();
}
/////////////////////////////////////////////////////////////////////////////
private var touchDown:boolean;
private var theHit : RaycastHit;
private var theCam:Camera;
function _basicButtons()
{
#if UNITY_EDITOR
if ( Input.GetMouseButtonUp(0) )
{
if ( Physics.Raycast ( theCam.ScreenPointToRay( Input.mousePosition ), theHit) )
{
if ( ! touchDown ) return;
touchDown = false;
buttonNamedPressed(theHit.collider.name );
}
}
if ( Input.GetMouseButtonDown(0) )
{
touchDown = true;
}
#else
if( Input.touches.Length > 0 )
{
if ( Input.touches[0].phase == TouchPhase.Ended )
{
if ( ! touchDown ) return;
if ( Physics.Raycast ( theCam.ScreenPointToRay( Input.touches[0].position ), theHit, 10) )
{
touchDown = false;
buttonNamedPressed( theHit.collider.name );
}
}
if ( Input.touches[0].phase == TouchPhase.Began )
{
touchDown = true;
}
}
#endif
}
/////////////////////////////////////////////////////////////////////////////
Great tutorial! I think this is actually what I was searching for. Thanks for all your help.
@Fattie I managed to trick the Unity GUI system somehow so I must ask a question before I rewrite 500 lines of code :)). Why do you consider Unity's GUI system as rubbish? I'm a newbie and that's why I'm asking so that I could maybe completely revise my code and not use GUI at all.
The tutorial you provided is more than great, I've already tried it and it works wonders :D, but calling GUI's is a bit easier in this case - considering that I cannot use the loadlevel function, because my app will not be structured on levels that need to be loaded from another scene. :)
O$$anonymous$$, i would say it is confusing to use - it's that simple. I would urge you to ask a new question "Please explain to a new user why the GUI is held in low opinion" and you will get more intelligent answers than from me! :)
that would be a good question on thsi site
PS normally I guess you'd use "Comment" for a comment - I might move your post since the other moderators would get angry (not me of course, I'm not anal at all :) )
Cheers!
Answer by Bunny83 · Dec 15, 2012 at 01:41 AM
The GUI system is like a state machine, if you set something it's valid for everything that follows this change until you change it again or the end of the current frane.
What you need is GUI.enabled. But keep in mind you have to disable the other GUIs.
Thanks for the reply. The thing is, I would like the GUI to still be displayed but not working. Easily put, I would like to make whatever exists beside the prompt, to inactive without making them disappear. An invisible wall or anything if something like that is possible?
[EDIT]: Sorry about that, I read through the whole wiki and it might be something i could use. I'm going to come back with a reply afterwards and tell you if it worked.
If I were to move it off screen, the user wouldn't be able to see the prompt anymore. Thanks for the answer but that doesn't work in this situation.
And by static do you mean static variables? Could that be the key?
Sorry, my bad. Through prompt, I am referring to a pop-up type of window which lets the user know he has leveled up or that he has no energy (or any other information).
I am considering what you said about writing down my own button. I am probably going to stick to that for now. Thank you! @Fattie could you transform the comment to an answer so that I can mark it as the correct one?
Answer by RobbingDaHood · Dec 15, 2012 at 01:53 PM
Cant you just use GUI object like GUI.Box to show the data in when you dont want it to be active and then use GUI.Button when you want it to be active again?
Thanks for the answer but I can't do that. As I said in the previous comment, I would like for the GUI to be visible just for the user to not be able to interact with it. Thanks anyway ^_^.
Follow this Question
Related Questions
Pause the video in unity after playing it but it will done automatically after some time of period. 0 Answers
How to Pause game 1 Answer
Unity iOS - Increase Value Based On Time Inactive 1 Answer
How to find inactive objects? 0 Answers
Why does Unity freeze for long periods when highlighting some objects in the hierarchy? 0 Answers