unity3d 随时间推移淡出和淡入纹理

iovurdzv  于 2023-02-05  发布在  其他
关注(0)|答案(1)|浏览(154)

我正在根据事件触发器更改纹理。此时更改相当突然。我希望进行过渡,以便当前纹理淡出,下一个纹理淡入(使用_Glow参数。两个过渡都应在3秒内发生。
到目前为止,我能够使淡入,但它是12秒。我应该使用两个协程吗?

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

public class VFXController : MonoBehaviour
{
    private AssetBundle assetBundle;
    private Texture texture;
    private Texture nextTexture;
    private Renderer renderer;
    public GameObject visual;

    private float currentGlowValue = 0.0f;
    private float targetGlowValue = 12.6f;

    private IEnumerator toggleTexture;

    // Start is called before the first frame update
    void Start()
    {
        assetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, "session_vfx_textures"));
        if (assetBundle == null)
        {
            Debug.Log("Failed to load AssetBundle!");
            return;
        }

        renderer = visual.GetComponent<Renderer>();

        toggleTexture = ToggleTexture(12.6f);

        InitEvents();
    }

    private void InitEvents()
    {
        EventsManager.current.onChangeVFXTexture += ChangeTexture;
    }

    private void OnDestroy()
    {
        EventsManager.current.onChangeVFXTexture -= ChangeTexture;
    }

    private void ChangeTexture(SessionNames name)
    {
        nextTexture = assetBundle.LoadAsset<Texture>(name.ToString());
        StartCoroutine(toggleTexture);
    }

    IEnumerator ToggleTexture(float duration)
    {
        for (float t = 0f; t < duration; t += Time.deltaTime)
        {
            renderer.material.SetFloat("_Glow", currentGlowValue + t);

            yield return null;

            if (renderer.material.GetFloat("_Glow") >= 12.0f)
            {
                renderer.material.SetTexture("_MainTex", nextTexture);
                StopCoroutine(toggleTexture);
            }
        }
    }
}
lyr7nygr

lyr7nygr1#

请检查着色器中的Range_GLOW属性。它可能被设置为Range(0,15)或其他值。如果您想将其设置为3秒,请将range更改为_Glow ("Intensity", Range(0, 3)) = 1

相关问题