Skip to content

Instantly share code, notes, and snippets.

@mahm
Created February 17, 2014 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mahm/9059619 to your computer and use it in GitHub Desktop.
Save mahm/9059619 to your computer and use it in GitHub Desktop.
SpriteButton (Unity)
using UnityEngine;
using System.Collections;
public class SpriteButton : MonoBehaviour
{
public Sprite buttonDownSprite = null;
public GameObject targetObject = null;
public string messageName = "";
private Sprite normalSprite;
private SpriteRenderer ren;
private AudioManager am;
// Use this for initialization
void Start() {
GameObject go = GameObject.FindGameObjectWithTag("AudioManager");
if (go == null) {
go = GameObject.Instantiate(Resources.Load("Prefab/AudioManager", typeof(GameObject))) as GameObject;
}
am = go.GetComponent<AudioManager>();
ren = GetComponent<SpriteRenderer>();
normalSprite = ren.sprite;
if (collider2D == null) {
gameObject.AddComponent<BoxCollider2D>();
}
}
void OnMouseDown() {
if (buttonDownSprite) {
ren.sprite = buttonDownSprite;
}
am.PlayButton ();
}
void OnMouseUp() {
ren.sprite = normalSprite;
if (targetObject) {
targetObject.SendMessage(messageName);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment