LoginSignup
2
2

More than 3 years have passed since last update.

Unity ~画像のフェード~

Posted at

使い方

  1. オブジェクトのImageの透明度を0にします。
  2. スクリプトをアタッチします。
  3. 動作させるとフェードインし始めます。

※活用
事前にオブジェクトを非アクティブ化させておき、
フェードインさせたいタイミングでsetActive(true)することで任意のタイミングでフェードインさせることが出来ます。

動作

フェードさせたいオブジェクトにアタッチすると、fadeStartの時間経過後にフェードインを開始します。
publicにしているので、数値を変えることで開始時間及び速度を変更できます。
fadeInのbool変数が仕事してないですが、拡張することでフェードアウトもできるのでもしよければ。

コード


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

public class fade : MonoBehaviour
{

    GameObject me; // 自分のオブジェクト取得用変数
    public float fadeStart = 1f; // フェード開始時間
    public bool fadeIn = true; // trueの場合はフェードイン
    public float fadeSpeed = 1f; // フェード速度指定


    // Start is called before the first frame update
    void Start()
    {
        me = this.gameObject; // 自分のオブジェクト取得
    }

    // Update is called once per frame
    void Update()
    {
        if (fadeStart > 0f)
        {
            fadeStart -= Time.deltaTime;
        }
        else
        {
            if (fadeIn)
            {
                fadeInFunc();
            }
        }
    }

    void fadeInFunc()
    {
        if (me.GetComponent<Image>().color.a < 255)
        {
            UnityEngine.Color tmp = me.GetComponent<Image>().color;
            tmp.a = tmp.a + (Time.deltaTime * fadeSpeed);
            me.GetComponent<Image>().color = tmp;
        }
    }
}
2
2
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
2
2