Hi, I’m creating an iPhone game and I’ve heard from many people/topics that using Unity GUI costs a lot of performance…and that to use GUI Textures instead. Now, I’ve grown accustomed to using the GUI.Buttons and such and just need some help to get started to re-do my GUI in this way. The biggest hurdle I’ve been having is that unity Iphone doesnt support onMouseDown/onmouseover functions (like flash), So the only guesses I have in detecting if you’ve pressed a button is checking the Input.mousePosition with the textures position, but even then it’s way too awkward the way Im doing it. I searched high and low (google/forums) for a tutorial, sample code, ANYTHING to get me started…but no such luck.
Also, I downloaded the GUI Manager and SpriteUI…but jumping straight into those was a little too advanced for my lack of knowledge of GUI Textures in general.
SpriteUI doesn’t use GUITextures at all actually.
It creates a single surface UI basing on SpriteManagers functionality.
The sample scene that comes with it should give you a very painless start (it comes with a few buttons with down visual behavior as well as a directional gadget that could easily be used for radial spinners)
Thanks, my knowledge of C is quite limited…so although the spriteUI seems like my solution, I really dont think I have the time to learn it (is the sample one you are talking about with the falling cans and soccerballs?)…especially considering all I am needing is to make a button. And it’s not like I have a lot of them either, just gets a little crazy with my level select screen. So I’ve got 2 questions:
-
How do I make a button with a GUI Texture (in javascript)?
-
(forgot to mention this one before) How would I make a “flick scrolling” type of screen, like seen in many iphone apps (again in javascript). So you can go through a lot of buttons in a list quickly.
Any help would be greatly appreciated.
Hope you don’t mind me joining in here, but I too am trying to make a gui with textures rather than the built in buttons.
I have the same questions…how to make the page flick-scroll (which I actually have a good idea how to do by using variables for the Y coordinates on all my buttons etc). But with scrolling, how do we determine whether or not the mouse clicks a button? the fact that GUI uses 3D coordinates (upside down) makes things even worse. I could figure it out mathematically, but I’m hoping for an easier solution.
Can a texture read a mouse-click. Sorry, I’m a bit of a newb too and just starting the menu system for my game.
Anyways, I hope I’m not hijacking this thread Madman, in fact I’m looking for the same answers as you 
Below is a simple example. Create a game object containing a GUITexture component that has the tag “BackButton” and then attach this script to it. After importing a texture (i.e. image) into Unity, you can assign that texture to the Texture property of the GUITexture component.
The script also assumes your scene has a camera in it tagged “MainCamera”. If you want to simulate iPhone input with the keyboard, then you’ll need to define a “Back” button on the keyboard as well.
The back button GUI Texture component will only register a touch when it is actually touched in terms of its rendered screen location. You can stitch together multiple GUITexture and GUIText objects to create your UI but keep in mind that each object will result in a draw call. Given this, you should construct the static parts of your UI as a single texture image and then only use separate GUITexture objects for actual buttons that are pressed. Another trick is that rather than construct a HUD of game stats information with multiple GUIText objects, you can use one big string and format with tabs and line feeds characters, etc. to create the display. That way its all one draw call. Search the forum for examples of that one.
public var simIPhone: boolean;
private var backButtonObj: GameObject;
private var backButton: GUITexture;
private var backButtonDown: boolean;
private var mainCameraObj: GameObject;
private var mainCamera: Camera;
function Start() {
this.backButtonObj = GameObject.FindWithTag("BackButton");
this.backButton = this.backButtonObj.GetComponent(GUITexture);
this.mainCameraObj = GameObject.FindWithTag("MainCamera");
this.mainCamera = this.mainCameraObj.GetComponent(Camera);
return;
}
function Update() {
if (this.simIPhone) {
if (Input.GetButtonDown("Back")) {
this.backButtonDown = true;
}
}
else {
var count = iPhoneInput.touchCount;
for (var i : int = 0; i < count; i++) {
var touch : iPhoneTouch = iPhoneInput.GetTouch(i);
if (touch.phase == iPhoneTouchPhase.Began) {
if (!this.backButtonDown) {
this.backButtonDown = this.backButton.HitTest(touch.position, this.mainCamera);
if (this.backButtonDown) {
continue;
}
}
(process begin phases of other touches)
}
(process cancel and end phases of touches if desired)
} // end loop over touches.
if (this.backButtonDown) {
this.ProcessBackAction();
}
(do other update stuff if necessary)
return;
}
private function ProcessBackAction() {
(do back button stuff)
return;
}
Awesome, thanks HanulTech…that’s exactly what I was looking for. A couple small questions:
-
Is there any difference on using iPhoneInput.getTouch() and Input.mousePosition? Besides the obvious (iPhoneInput supports more than one position).
-
Would you put guitextures inside onGUI() functions, or is it ok to do it inside update like the example above?
And schplurg, its cool to hijack this topic (really glad this might help more than just me)…still gotta find a way to do the flick scrolling.
Not sure I follow your first question. iPhoneInput.getTouch() returns all touches that have been recognized by the phone. The code then goes through and processes them by phase and then check for touches on the GUITexture based buttons. Rather than worrying about the specific location of the touches, the code simply detects whether one of the control has been touched. The code only looks for button presses, but could be extended to process any touch including gestures.
You do not need to use OnGUI events. Your UI can be simply constructed from GUITexture and GUIText components contained inside game objects with one controller script processing all phone input.
One obvious advantage to using Input.mousePosition is that it works in the editor (without having to use Unity Remote).