Last active
July 19, 2018 09:35
-
-
Save iwashihead/e5c0cf5a70acb1a05b464fd1da28889f to your computer and use it in GitHub Desktop.
CanvasGroupのアルファを用いたフェード時にText用のメッシュエフェクトを綺麗に見せる
This file contains 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
//------------------------------------------------------- | |
// @file CanvasGroupEnhansed.cs | |
// @brief | |
// CanvasGroupのアルファを用いたフェード時に | |
// Text用のメッシュエフェクトを綺麗に見せるコンポーネントです | |
// CanvasGroupにアタッチして使用してください | |
// | |
// @author haruki.tachihara | |
//------------------------------------------------------- | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.UI; | |
/// <summary> | |
/// CanvasGroupのアルファを用いたフェード時に | |
/// Text用のメッシュエフェクトを綺麗に見せるコンポーネントです. | |
/// </summary> | |
[ExecuteInEditMode] | |
[DisallowMultipleComponent] | |
[RequireComponent(typeof(CanvasGroup))] | |
public class CanvasGroupShadowFader : MonoBehaviour | |
{ | |
const float DIFF_AMOUNT = 0.001f; | |
const float ALPHA_REDUCTION_AMOUNT = 0.3333f; | |
private struct ShadowChunk | |
{ | |
public Shadow Shadow; | |
public float BaseAlpha; | |
public ShadowChunk(Shadow shadow) | |
{ | |
this.Shadow = shadow; | |
this.BaseAlpha = shadow ? shadow.effectColor.a : 0f; | |
} | |
} | |
private struct TextChunk | |
{ | |
public Text Text; | |
public ShadowChunk[] ShadowChunks; | |
public TextChunk(Text text, Shadow[] shadows) | |
{ | |
this.Text = text; | |
this.ShadowChunks = new ShadowChunk[shadows == null ? 0 : shadows.Length]; | |
if (shadows != null) | |
{ | |
for (int i = 0; i < shadows.Length; ++i) | |
{ | |
this.ShadowChunks[i] = new ShadowChunk(shadows[i]); | |
} | |
} | |
} | |
public void ApplyAlpha(float alpha) | |
{ | |
if (ShadowChunks == null) | |
return; | |
for (int i = 0; i < ShadowChunks.Length; ++i) | |
{ | |
if (!ShadowChunks[i].Shadow) | |
continue; | |
// アルファを減衰させることでメッシュの重なりの見え方を綺麗にする. | |
ShadowChunks[i].Shadow.effectColor = new Color( | |
ShadowChunks[i].Shadow.effectColor.r, | |
ShadowChunks[i].Shadow.effectColor.g, | |
ShadowChunks[i].Shadow.effectColor.b, | |
ShadowChunks[i].BaseAlpha * alpha | |
); | |
} | |
} | |
} | |
// WARNING: | |
// キャッシュを利用するかどうか. 基本的にtrueでOK | |
// 子階層Componentが動的生成される場合は取得が漏れる可能性がある. | |
public bool UseCache = true; | |
public bool EnableAlphaMeshEffect = true; | |
private CanvasGroup canvasGroup; | |
private float prevAlpha; | |
private List<TextChunk> cachedTextChunkList; | |
private List<TextChunk> TextChunkList { get { return GetChunkList(); } } | |
List<TextChunk> GetChunkList() | |
{ | |
// cacheが存在すればキャッシュを返す. | |
if (UseCache && cachedTextChunkList != null) | |
{ | |
return cachedTextChunkList; | |
} | |
var textChunkList = new List<TextChunk>(); | |
var texts = GetComponentsInChildren<Text>(); | |
for (int i = 0; i < texts.Length; ++i) | |
{ | |
if (!texts[i]) | |
continue; | |
textChunkList.Add(new TextChunk(texts[i], texts[i].GetComponents<Shadow>())); | |
BaseMeshEffect ef; | |
} | |
// キャッシュに保存 | |
if (UseCache) | |
cachedTextChunkList = textChunkList; | |
return textChunkList; | |
} | |
void Awake() | |
{ | |
canvasGroup = GetComponent<CanvasGroup>(); | |
GetChunkList(); | |
} | |
void LateUpdate() | |
{ | |
if (!IsDiff(prevAlpha, canvasGroup.alpha)) | |
return; | |
ApplyAlpha(); | |
prevAlpha = canvasGroup.alpha; | |
} | |
void ApplyAlpha() | |
{ | |
for (int i = 0; i < TextChunkList.Count; ++i) | |
{ | |
if (!TextChunkList[i].Text) | |
continue; | |
// canvasGroupのアルファを渡す. | |
TextChunkList[i].ApplyAlpha(canvasGroup.alpha); | |
} | |
} | |
bool IsDiff(float f1, float f2) | |
{ | |
return Mathf.Abs(f1 - f2) > DIFF_AMOUNT; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
通常 アルファ=1
通常 アルファ=0.2
このスクリプトを使用時 アルファ=1
このスクリプトを使用時 アルファ=0.2