LoginSignup
0
0

More than 3 years have passed since last update.

オブジェクトのFadeIn/FadeOut

Posted at

オブジェクトのフェードイン、フェードアウトの
自分なりのスクリプトです。
いろいろ応用してください。

色を徐々に変える際も使えます。
Colorのrbgaをいじってみてください。
① オブジェクトに任意のマテリアルをアタッチ
② オブジェクトのRendering ModeをFadeにする
③ 以下のスクリプトをオブジェクトにアタッチする

以下はフェードアウトですのでフェードインしたい場合はfの値を+=してください。

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

public class FadeObject : MonoBehaviour
{
    float f;

    // Start is called before the first frame update
    void Start()
    {
        f = 1f;

    }

    // Update is called once per frame
    void Update()
    {
        if (f >= 0)
        {
            FadeOut();
        }
        else
        {
            Debug.Log("FadeOut完了");

        }


    }

    void FadeOut()
    {
        //マテリアルを取得
        Material mat = GetComponent<Renderer>().material;
        //カラーを取得
        Color c = mat.color;
        //マテリアルのインスペクターでRendering ModeをFadeにすることを忘れずに
        c.a = f;
        f -= 0.001f;
        //colorはrgb
        //マテリアルのカラーに変更値を再代入(この場合アルファ値の変更)
        mat.color = new Color(c.r, c.g, c.b, c.a);

    }
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0