Skip to content

Instantly share code, notes, and snippets.

@asus4
Created March 1, 2012 04:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asus4/1947358 to your computer and use it in GitHub Desktop.
Save asus4/1947358 to your computer and use it in GitHub Desktop.
Popup script for Unity3D (using EzGUI, iTween)
using UnityEngine;
using System.Collections;
// delegates
public enum UIPopupState {
SUBMIT,
CANCEL
}
public delegate void ClickDelegate(UIPopupState state);
/// <summary>
/// Common Popup Framework
/// by koki ibukuro
/// </summary>
public class UIPopup : MonoBehaviour {
public bool enableCoverCancel = true;
public UIButton submitButton;
public UIButton cancelButton;
public UIButton coverButton;
private ClickDelegate _popupDelegate = null;
void Start () {
if(submitButton) {
submitButton.SetValueChangedDelegate(onSubmitClicked);
}
if(cancelButton) {
cancelButton.SetValueChangedDelegate(onCancelClicked);
}
if(coverButton) {
coverButton.SetValueChangedDelegate(onCoverClicked);
}
}
void OnDestroy() {
submitButton = null;
cancelButton = null;
coverButton = null;
}
/// <summary>
/// Set Click Delegate
/// </summary>
public void SetClickDelegate(ClickDelegate del) {
_popupDelegate = del;
}
void onSubmitClicked(IUIObject obj) {
animateDestroy();
callDelegte(UIPopupState.SUBMIT);
}
void onCancelClicked(IUIObject obj) {
animateDestroy();
callDelegte(UIPopupState.CANCEL);
}
void onCoverClicked(IUIObject obj) {
if(enableCoverCancel) {
animateDestroy();
if(cancelButton != null) {
callDelegte(UIPopupState.CANCEL);
}
else {
callDelegte(UIPopupState.SUBMIT);
}
}
}
void animateDestroy() {
float outScale = 0.5f;
// do fade out
iTween.FadeTo(gameObject, iTween.Hash(
"alpha", 0f,
"time",0.3f,
"easetype","easeInQuad",
"oncompletetarget", gameObject,
"oncomplete", "OnAnimateDestroyFinished"
));
}
void OnAnimateDestroyFinished() {
Destroy(gameObject);
}
void callDelegte(UIPopupState state) {
if(_popupDelegate != null) {
_popupDelegate(state);
}
else {
Debug.LogWarning("you need set Delegate");
}
}
}
@asus4
Copy link
Author

asus4 commented Mar 1, 2012

1. Make the Prefab

  • Popup:UIPopup
    • Window:Sprite
      • SubmitButton:UIButton
      • CancelButton (optional) : UIButton
    • CoverButton:UIButton

2. Set Properties

  • SubmitButton to UIPopup.submitButton
  • CancelButton to UIPopup.cancelButton
  • CoverButton to UIPopup.coverButton

Example

var popupPrefab;

function Start() {
    var popup:UIPopup = Instantiate(popupPrefab, Vector3.zero, Quaternion.identity) as UIPopup;
    popup.SetClickDelegate(onPopupClick);
}

function onPopupClick(UIPopupState state) {
    if(state.SUBMIT) {
        // DO 
    }
    else if(state.CANCEL) {
        // DO
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment