Instantly share code, notes, and snippets.
Created
March 8, 2015 21:23
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save kankikuchi/e20cc90b39214b8f9eb6 to your computer and use it in GitHub Desktop.
UIButtonの補助クラス【Unity】【NGUI】
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; | |
using System.Collections.Generic; | |
public class UIButtonAssistant : MonoBehaviour { | |
//このボタンが有効がどうか | |
protected bool _isEnabled = true; | |
//ボタンの色 | |
protected Color _normalColor = new Color (1.0f, 1.0f, 1.0f); | |
protected Color _hoverColor = new Color (0.8f, 0.8f, 0.8f); | |
protected Color _pressedColor = new Color (0.6f, 0.6f, 0.6f); | |
protected Color _disabledColor = new Color (1.0f, 1.0f, 1.0f); | |
//SE名 | |
protected string _seName = AudioName.SE_BUTTON; | |
//色が変わる時間 | |
protected float _transitionTime = 0.05f; | |
//ラベルとその初期色 | |
private struct ChildLabel | |
{ | |
public UILabel Label; | |
public Color InitialLabelColor, InitialLabelEffectColor; | |
} | |
private List<ChildLabel> _childLabelList = new List<ChildLabel> (); | |
//スプライトとその初期色 | |
private struct ChildSprite | |
{ | |
public UISprite Sprete; | |
public Color InitialSpriteColor; | |
} | |
private List<ChildSprite> _childSpriteList = new List<ChildSprite> (); | |
//================================================================================= | |
//初期化 | |
//================================================================================= | |
protected virtual void Awake () | |
{ | |
//ボタンの各色を設定 | |
GetComponent<UIButton> ().defaultColor = _normalColor; | |
GetComponent<UIButton> ().hover = _hoverColor; | |
GetComponent<UIButton> ().pressed = _pressedColor; | |
GetComponent<UIButton> ().disabledColor = _disabledColor; | |
//ボタンの色が変わる時間を設定 | |
GetComponent<UIButton> ().duration = _transitionTime; | |
//ボタンを押した時のSEが鳴るようにメソッドを登録 | |
EventDelegate.Add (GetComponent<UIButton> ().onClick, this.PlaySE); | |
//このオブジェクト及び子オブジェクトにラベルがあったらラベルと初期色を保持 | |
UILabel[] labelsInChildren = GetComponentsInChildren<UILabel> (); | |
foreach(UILabel label in labelsInChildren){ | |
ChildLabel childLabel; | |
childLabel.Label = label; | |
childLabel.InitialLabelColor = label.color; | |
childLabel.InitialLabelEffectColor = label.effectColor; | |
_childLabelList.Add (childLabel); | |
} | |
//このオブジェクト及び子オブジェクトにスプライトがあったらスプライトと初期色を保持 | |
UISprite[] spritesInChildren = GetComponentsInChildren<UISprite> (); | |
foreach(UISprite sprite in spritesInChildren){ | |
ChildSprite childSprite; | |
childSprite.Sprete = sprite; | |
childSprite.InitialSpriteColor = sprite.color; | |
_childSpriteList.Add (childSprite); | |
} | |
} | |
//================================================================================= | |
//更新 | |
//================================================================================= | |
protected virtual void Update () { | |
_isEnabled = !SceneManager.Instance.IsFading; | |
} | |
protected virtual void LateUpdate () | |
{ | |
GetComponent<UIButton> ().isEnabled = _isEnabled; | |
UpdateColor (); | |
} | |
/// <summary> | |
/// ラベルやらボタン以外の画像やらの色をボタンの色に合わせる | |
/// </summary> | |
protected void UpdateColor(){ | |
Color buttonColor = GetComponent<UIButton> ().tweenTarget.GetComponent<UISprite>().color; | |
foreach(ChildLabel childLabel in _childLabelList){ | |
childLabel.Label.color = childLabel.InitialLabelColor * buttonColor; | |
childLabel.Label.effectColor = childLabel.InitialLabelEffectColor * buttonColor; | |
} | |
foreach(ChildSprite childSprite in _childSpriteList){ | |
childSprite.Sprete.color = childSprite.InitialSpriteColor * buttonColor; | |
} | |
} | |
//================================================================================= | |
//SE | |
//================================================================================= | |
/// <summary> | |
/// SE再生 | |
/// </summary> | |
private void PlaySE(){ | |
if(_isEnabled){ | |
AudioManager.Instance.PlaySE (_seName); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment