Skip to content

Instantly share code, notes, and snippets.

@isaveu
Forked from EsProgram/AlphaGameObject.cs
Created May 16, 2016 00:40
Show Gist options
  • Save isaveu/3ffe67f8b534d9f2d02e83cd7bbc2d9f to your computer and use it in GitHub Desktop.
Save isaveu/3ffe67f8b534d9f2d02e83cd7bbc2d9f to your computer and use it in GitHub Desktop.
Unity:自身のゲームオブジェクトに関連付けられた全てのマテリアルのアルファ値を0に近付けていって全てが透明になったらゲームオブジェクトを破棄する(マテリアルのShaderはTransparentに設定しておく)
public class AlphaGameObject : MonoBehaviour
{
private Color alpha = new Color(0, 0, 0, 0.01f);
private List<Material> materials = new List<Material>();
private void Start()
{
//マテリアルの取得
foreach(Transform child in transform)
{
if(child != null && child.renderer != null && child.renderer.material != null)
foreach(Material mat in child.renderer.materials)
materials.Add(mat);
}
}
private void Update()
{
if(materials.Any(m => m.color.a > 0))
materials.ForEach(m => m.color -= alpha);
else
Destroy(this.gameObject);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment