Created
April 6, 2012 04:46
-
-
Save asus4/2316951 to your computer and use it in GitHub Desktop.
Popup for nGUI,Unity3D
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
[AddComponentMenu("NGUI/Interaction/Ex/Popup")] | |
public class UIPopup : MonoBehaviour { | |
public bool enableCoverCancel = true; | |
public UIEventListener submitButton; | |
public UIEventListener cancelButton; | |
public UIEventListener coverButton; | |
public UILabel mainLabel; | |
public string mainText; | |
public UILabel submitLabel; | |
public string submitText = "ok"; | |
public UILabel cancelLabel; | |
public string cancelText = "cancel"; | |
private ClickDelegate _popupDelegate = null; | |
void Start () { | |
if(submitButton) { | |
submitButton.onClick = onSubmitClicked; | |
} | |
if(cancelButton) { | |
cancelButton.onClick = onCancelClicked; | |
} | |
if(coverButton) { | |
coverButton.onClick = onCoverClicked; | |
} | |
if(mainLabel) { | |
mainLabel.text = mainText; | |
} | |
if(submitLabel) { | |
submitLabel.text = submitText; | |
} | |
if(cancelLabel) { | |
cancelLabel.text = cancelText; | |
} | |
} | |
void OnDestroy() { | |
submitButton.onClick = null; | |
cancelButton.onClick = null; | |
coverButton.onClick = null; | |
submitButton = null; | |
cancelButton = null; | |
coverButton = null; | |
mainLabel = null; | |
submitLabel = null; | |
cancelLabel = null; | |
} | |
/// <summary> | |
/// Set Click Delegate | |
/// </summary> | |
public void SetClickDelegate(ClickDelegate del) { | |
_popupDelegate = del; | |
} | |
void onSubmitClicked(GameObject go) { | |
animateDestroy(); | |
callDelegte(UIPopupState.SUBMIT); | |
} | |
void onCancelClicked(GameObject go) { | |
animateDestroy(); | |
callDelegte(UIPopupState.CANCEL); | |
} | |
void onCoverClicked(GameObject go) { | |
if(enableCoverCancel) { | |
animateDestroy(); | |
if(cancelButton != null) { | |
callDelegte(UIPopupState.CANCEL); | |
} | |
else { | |
callDelegte(UIPopupState.SUBMIT); | |
} | |
} | |
} | |
void animateDestroy() { | |
UIWidget[] wigets = GetComponentsInChildren<UIWidget>(); | |
Color toColor = new Color(1f,1f,1f,0f); | |
float tweenTime = 0.5f; | |
foreach(UIWidget wiget in wigets) { | |
TweenColor tween = TweenColor.Begin(wiget.gameObject, tweenTime, toColor); | |
tween.method = UITweener.Method.EaseOut; | |
tween.Play(true); | |
} | |
StartCoroutine(OnAnimateDestroyFinished(tweenTime)); | |
} | |
IEnumerator OnAnimateDestroyFinished(float time) { | |
yield return new WaitForSeconds(time); | |
Destroy(gameObject); | |
} | |
void callDelegte(UIPopupState state) { | |
if(_popupDelegate != null) { | |
_popupDelegate(state); | |
} | |
else { | |
Debug.LogWarning("you need set Delegate"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment