Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System;
- using System.Collections;
- public class ButtonController : MonoBehaviour {
- public event Action<ButtonController> OnPressed;
- public event Action<ButtonController> OnReleased;
- public event Action<ButtonController> MouseEnter;
- public event Action<ButtonController> MouseExit;
- public static int buttonsHovering { get; protected set; }
- public GameObject[] highlight;
- public Color normalColor = Color.white;
- public Color pressedColor = Color.blue;
- public Color hightlightColor = Color.gray;
- public Color disabledColor = Color.black;
- public FunctionInfo[] functions;
- protected bool buttonEnabled = true;
- protected bool mouseOver { get; private set; }
- void OnMouseEnter() {
- buttonsHovering++;
- mouseOver = true;
- Highlight();
- if(MouseEnter != null) MouseEnter(this);
- }
- void OnMouseExit() {
- buttonsHovering--;
- mouseOver = false;
- Unhighlight();
- if(MouseExit != null) MouseExit(this);
- }
- void OnMouseDown() {
- PressHighlight();
- if(buttonEnabled && OnPressed != null)
- OnPressed(this);
- }
- void OnMouseUp() {
- Highlight();
- if(buttonEnabled && OnReleased != null)
- OnReleased(this);
- }
- void OnMouseUpAsButton() {
- ButtonEvent();
- }
- protected virtual void OnEnable() {
- Unhighlight();
- }
- protected virtual void OnDisable() {
- if(mouseOver) {
- mouseOver = false;
- buttonsHovering--;
- Unhighlight();
- if(MouseExit != null) MouseExit(this);
- }
- }
- [ContextMenu("Highlight")]
- public virtual void Highlight() {
- if(buttonEnabled) SetColors(hightlightColor);
- }
- [ContextMenu("Unhighlight")]
- public virtual void Unhighlight() {
- if(buttonEnabled) SetColors(normalColor);
- }
- public virtual void PressHighlight() {
- if(buttonEnabled) SetColors(pressedColor);
- }
- public virtual void DisabledHighlight() {
- if(!buttonEnabled) SetColors(disabledColor);
- }
- void SetColors(Color color) {
- foreach(GameObject a in highlight)
- SetColor(a, color);
- }
- static void SetColor(GameObject gameObject, Color color) {
- if(gameObject & gameObject.renderer) {
- SpriteRenderer spriteRenderer = gameObject.renderer as SpriteRenderer;
- if(spriteRenderer)
- spriteRenderer.color = color;
- else
- gameObject.renderer.material.color = color;
- }
- }
- public virtual void ButtonEvent() {
- foreach(FunctionInfo a in functions)
- a.SendMessage(this);
- }
- public void SetEnabled(bool enabled) {
- buttonEnabled = enabled;
- if(!buttonEnabled)
- DisabledHighlight();
- else if(mouseOver)
- Highlight();
- else
- Unhighlight();
- }
- [ContextMenu("Enable")]
- public void EnableButton() {
- SetEnabled(true);
- }
- [ContextMenu("Disable")]
- public void DisableButton() {
- SetEnabled(false);
- }
- [System.Serializable]
- public class FunctionInfo {
- public string function = "";
- public GameObject functionObject;
- public string findObjectOfType = "";
- public string findObjectWithTag = "";
- public FunctionInfo(GameObject functionObject, string function) {
- this.functionObject = functionObject;
- this.function = function;
- }
- public void SendMessage(ButtonController button) {
- if(function != "") {
- GameObject sendTo = functionObject;
- if(!sendTo && findObjectOfType != "") {
- Type type = Type.GetType(findObjectOfType);
- UnityEngine.Object obj = FindObjectOfType(type) as UnityEngine.Object;
- if(obj as MonoBehaviour)
- sendTo = (obj as MonoBehaviour).gameObject;
- else if(obj as Behaviour)
- sendTo = (obj as Behaviour).gameObject;
- }
- if(!sendTo && findObjectWithTag != "")
- sendTo = GameObject.FindGameObjectWithTag(findObjectWithTag);
- if(!sendTo) return;
- sendTo.SendMessage(function, button);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment