c# - Create a simple button in Unity3D 2D mode -
i'm working on 2d game in unity 3d, v4.5. have added sprites hierarchy. how add button hierarchy? have designated button.png should display button. should turn sprite button?
what tried far:
someone noted gui.button, but
1: has done in code - assume can add buttons game ui inside unity gui (??)
2: using gui.button script, class adds border around specified texture. haven't figured out how use existing image , size (width/height) of sprite texture sent gui.button.
you can useongui button , disappear rectangle have make new guistyle
private guistyle teststyle = new guistyle(); public texture2d texture1; void ongui(){ if( gui.button( new rect (0, 0, 100, 100) , texture1 ,teststyle) ) { //dosomething if clicked on } }
if dont want can raycasting selfand give buttons tags below
void update () { if (input.getmousebuttondown (0)) { ray ray = camera.main.screenpointtoray(input.mouseposition); raycasthit hit; if (physics.raycast(ray, out hit)) { if(hit.collider.tag=="play") //dosomething } } }
in unity 4.6 ui can add listeners buttons in script below
private button mybutton = null; // assign in editor void start() { mybutton.onclick.addlistener(() => { somefunction(); }); }
Comments
Post a Comment