Created
December 30, 2015 14:41
-
-
Save nkjzm/cf0b875be7c9056af452 to your computer and use it in GitHub Desktop.
UnityでShadowのアルファ値をTextのアルファ値と同期させるスクリプト
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 UnityEngine.UI; | |
using UniRx; | |
using System.Collections; | |
public class ShadowAlphaAdapter : MonoBehaviour | |
{ | |
private Text text; | |
private Shadow[] shadows; | |
void Start () | |
{ | |
text = GetComponent<Text> (); | |
// Shadowを継承しているOutlineも取得する | |
shadows = GetComponents<Shadow> (); | |
// テキストとアルファ値の監視 | |
// .AddTo ()を使うとGameObjectの破棄と同時にDisposeする | |
text.ObserveEveryValueChanged (x => x.color.a) | |
.Subscribe (x => UpdateAlpha (x)) | |
.AddTo (gameObject); | |
text.ObserveEveryValueChanged (x => x.text) | |
.Subscribe (_ => UpdateAlpha ()) | |
.AddTo (gameObject); | |
} | |
private void UpdateAlpha() | |
{ | |
float alpha = text.color.a; | |
UpdateAlpha (alpha); | |
} | |
private void UpdateAlpha(float alpha) | |
{ | |
foreach (Shadow s in shadows) { | |
s.effectColor = new Color( | |
s.effectColor.r, | |
s.effectColor.g, | |
s.effectColor.b, | |
alpha | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment