Skip to content

Instantly share code, notes, and snippets.

@EsProgram
Last active May 16, 2016 00:40
Show Gist options
  • Save EsProgram/0c1b47918772c2c12ba8 to your computer and use it in GitHub Desktop.
Save EsProgram/0c1b47918772c2c12ba8 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