How do I put controls on screen?

安稳与你 提交于 2019-11-28 00:30:56
Xerar

The best option for you is probably using the UI system of Unity. You can add a lot of different things in the UI, like buttons, scrollbars, sliders, pictures or text. Using this you might be able to build your own controls for your game. You can either add the UI element through the Scene editor or through code.

To add buttons for example is ridiculously easy:

1, click to add a Canvas

Note - always select "Screen Space Overlay" and "Scale with screen size"

(Unity accidentally made the defaults the wrong ones, in Unity5. The only settings you use are those two.)

2, click to add a Button

You're done - just drag in any function you want run by the button.

In the example, you'd have a script like THIS:

public void SomeScript()
  {
  public void ClickedMainMenu()
    {
    a.PlayOneShot(clickSound, 3f);
    SceneManager.LoadScene("main_menu");
    }
  public void ClickedLevel()
    {
    a.PlayOneShot(clickSound, 3f);
    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }
  public void ClickedQuit()
    {
    a.PlayOneShot(clickSound, 3f);
    Application.Quit();
    }

simply drag and select the relevant function, in the UI.Button Inspector panel. (You press "+" first, and then just drag.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!