Skip to content

Instantly share code, notes, and snippets.

@nicolasmbatista
Created May 9, 2014 01:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicolasmbatista/46199f91819e3751da4e to your computer and use it in GitHub Desktop.
Save nicolasmbatista/46199f91819e3751da4e to your computer and use it in GitHub Desktop.
SmartButton for Unity3D
using UnityEngine;
using System.Collections;
public class SmartButton : MonoBehaviour {
public enum Usage {
Default,
URL,
SceneLoader,
PanelSwitcher,
IAPPurchase,
Exit,
CoroutineTrigger
}
public Usage UseAs;
public string Value;
public MonoBehaviour ComponentToUse;
public GameObject GoToPanel;
public GameObject OurParentPanel;
public static System.Action<SmartButton> OnSmartButtonClickEvent;
void Start () {
if(gameObject.collider == null && gameObject.collider2D == null)
{
Debug.LogWarning("Warning! Smart button might not work without a collider or collider2D attached to the GameObject");
}
}
void OnMouseDown()
{
//For those NGUI Users ;)
OnClick();
}
public virtual void OnClick()
{
switch (UseAs)
{
case Usage.Default:
if(OnSmartButtonClickEvent != null)
OnSmartButtonClickEvent(this);
break;
case Usage.URL:
Application.OpenURL(Value);
break;
case Usage.SceneLoader:
Application.LoadLevel(Value);
break;
case Usage.PanelSwitcher:
OurParentPanel.SetActive(false);
if(GoToPanel != null)
GoToPanel.SetActive(true);
break;
case Usage.IAPPurchase:
IAPManager.Buy(value);
break;
case Usage.Exit:
Application.Quit();
break;
case Usage.CoroutineTrigger:
ComponentToUse.StartCoroutine(Value);
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment