- Home /
Multiple GUI textures one script
I am making the control panel for my game. At the bottom of the screen, there will be various textures to do various tasks: put you into combat mode, various attack types, camp outside a city, open your quest journal, etc. I got one item working, but I'm unsure to make it work with multiple onMouseDown events. I would assume, you add a listener, but I thought you added it to onMouseEnter and Exit to add and remove.
Do you have a separate bool for each item and see if it was clicked? Any help would be appreciated. Thanks!
Answer by OperationDogBird · Aug 05, 2012 at 09:07 PM
Are you using OnGUI or GUITexture?
If you are using OnGUI, use the builtin button with a style for the image(you can have separate images for hover, active, normal) see GUI Style.
if(GUI.Button(rect,"Quest Journal",journalStyle))
{
Debug.Log("Clicked quest journal");
}
If you are using GUITextures there is a built in function called HitTest() that make it act like a button. Check out GUIElement.HitTest.
if(guiTexture1.HitTest(Input.mousePosition,Camera.main))
{
Debug.Log("Clicked gui texture 1");
}
I suggest not using OnMouseEnter/Exit functions as they are intensive(those dont work on Phones either, for that reason).
Thanks! What does guiTexture1 need to be defined as Texture2d or Texture or GuiTexture? What should the graphic element be set to? GUI? Texture? Clamp?
thats your guitexture, so like
public var journalTexture:GUITexture;
function Update()
{
if(Input.Get$$anonymous$$ouseButtonUp(0))
{
if(journalTexture.HitTest(Input.mousePosition,Camera.main))
{
Debug.Log("Journal Opened");
}
}
}
$$anonymous$$y graphic is set to Texture, not Clamped.
Thanks! I had heard that setting it to GUI was the way to go.
Isn't Hit test part of the physics engine for 3d objects?
Not that i know of. You may be thinking of RaycastHit which uses raycast and collider, tho there is no physics involved. There is a link to the proper Hit Test in my original answer if you need the ref.
Answer by TurboHermit · Aug 05, 2012 at 10:30 PM
I'd use GUI.Buttons if I were you. You can easily assign a texture to the button and what it does when you click it.
var AwesomeTexture : Texture;
var AnotherOne : Texture;
function OnGUI(){
if (GUI.Button(Rect(10,10,50,50),AwesomeTexture)){
//Do whatever
}
if (GUI.Button(Rect(20,500,100,100),AnotherOne)){
//Do something else
}
}
Your answer
Follow this Question
Related Questions
OnMouseDown 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
OnMouseOver Problem 3 Answers