Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.