イラスト、モデリング、Unity、VR関連

unityとかblenderとかvr関連の作業メモ

Unity FadeOut

マテリアルのRendating ModeをFadeにする

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FadeOutBreaked : MonoBehaviour
{
  [SerializeField] float LifeTime = 3.0f;
  private float StartTime{get;set;}
  private Color StartColor{get;set;}
  private Color EndColor{get;set;}
  void Start()
  {
    StartTime = Time.time;
    SetColors();
    //Color c = GetComponent().material.color;
    //c.a = 0;
    //GetComponent().material.color = c;
    StartCoroutine("DestoySelf");
  }

  void SetColors(){
    StartColor = GetComponent().material.color;
    Color sc = StartColor;
    sc.a = 0;
    EndColor = sc;
  }

  IEnumerator DestoySelf(){
    yield return new WaitForSeconds(LifeTime);
    Destroy(gameObject);
  }

  void Update(){
    float dif = Time.time - StartTime;
    Color fading = Color.Lerp(StartColor,EndColor,dif/LifeTime);
    GetComponent().material.SetColor("_Color",fading);
  }

}