Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411427 Posts in 69363 Topics- by 58416 Members - Latest Member: JamesAGreen

April 19, 2024, 11:30:28 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsCommunityTownhallForum IssuesArchived subforums (read only)TutorialsBeginner Unity UI Button with Sprites, TextMesh, Collider2D, and Mouse Functions
Pages: [1]
Print
Author Topic: Beginner Unity UI Button with Sprites, TextMesh, Collider2D, and Mouse Functions  (Read 3116 times)
Uncle Scotty
Level 0
***


View Profile WWW
« on: December 31, 2014, 01:56:07 AM »

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.zip

There 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:

Code:
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!  Toast Right
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic