I have put together a basic Unity project that has a simple button to switch between scenes. This project should help Unity beginners understand the basics of making 2D User Interface (UI) buttons from scratch, using sprites, a 2D collider, text meshes, and mouse functions. The same basic concepts can be applied to create custom 3D buttons.
You can download a zip of the project files from:
https://dl.dropboxusercontent.com/u/52637256/AnachronicDesigns/Tutorials/Projects/BasicMenu.zipThere are many different ways to create UI elements in Unity, including using the new built-in UI tools in Version 4.6, or Asset Store products like NGUI, Daikon Forge, and InControl.
Thisproject, however, is a simple way to get started with making a custom UI from scratch, and can help people learn some of the basic Unity components. There is only one script in the project, which is as follows:
using UnityEngine;
using System.Collections;
public class Button : MonoBehaviour {
public GameObject highlightSprite;
public enum buttonType {Scene1, Scene2};
public buttonType typeOfButton;
// Use this for initialization
void Start () {
highlightSprite.renderer.enabled = false;
}
// Update is called once per frame
void Update () {
}
void OnMouseEnter() {
Debug.Log ("Mouse entered collider");
highlightSprite.renderer.enabled = true;
}
void OnMouseExit() {
Debug.Log ("Mouse exited collider");
highlightSprite.renderer.enabled = false;
}
void OnMouseDown() {
Debug.Log ("Mouse clicked on collider");
switch (typeOfButton) {
case buttonType.Scene1:
Application.LoadLevel ("Scene2");
break;
case buttonType.Scene2:
Application.LoadLevel ("Scene1");
break;
default:
break;
}
}
}
Good luck and happy developing!