Created
March 1, 2012 04:52
-
-
Save asus4/1947358 to your computer and use it in GitHub Desktop.
Popup script for Unity3D (using EzGUI, iTween)
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> | |
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"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
1. Make the Prefab
2. Set Properties
Example