arrays - How to make my buttons do different things with C# in Unity? -
i have code puts buttons on screen via array , assign textures in inspector. problem don't know how make example button 1 quit game, button 2 go next level , button 3 load image. explain me need make individual buttons different things when clicked? (the second array buttons on right side of screen)
this code looks right now:
using unityengine; using system.collections; public class ui : monobehaviour { public texture2d[] parts; public texture2d[] extra; int buttonheight, buttonwidth; int x, y; int width, height; void ongui () { (int = 0; < parts.length; ++i) { if (gui.button (new rect (x - 35, * (height + 97), width + 300, height + 90), parts [i])) debug.log ("clicked button " + i); //if (gui.button (new rect (0, * (buttonheight + 20), buttonwidth + 100, buttonheight + 100), textures [i])) } (int x = 0; x < extra.length; ++x) { if (gui.button (new rect (x + 800, x * (height + 140), width + 300, height + 120), [x])) debug.log ("clicked button " + x); if (extra [0] && input.getbuttondown ("jump")) { application.capturescreenshot ("screenshot.png"); debug.log ("screen captured"); } } } }
something work:
using unityengine; using system.collections; public class ui : monobehaviour { public texture2d[] parts; public texture2d[] extra; public string[] actions; int buttonheight, buttonwidth; int x, y; int width, height; void ongui () { (int = 0; < parts.length; ++i) { if (gui.button (new rect (x - 35, * (height + 97), width + 300, height + 90), parts [i])) execute(actions[i]); //if (gui.button (new rect (0, * (buttonheight + 20), buttonwidth + 100, buttonheight + 100), textures [i])) } (int x = 0; x < extra.length; ++x) { if (gui.button (new rect (x + 800, x * (height + 140), width + 300, height + 120), [x])) debug.log ("clicked button " + x); if (extra [0] && input.getbuttondown ("jump")) { application.capturescreenshot ("screenshot.png"); debug.log ("screen captured"); } } } void execute(string action) { switch (action) { case "exit": application.quit(); break; case "next": // load next level.. debug.log("next"); break; } }
}
Comments
Post a Comment