您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

66 行
2.1 KiB

using System.Collections;
using UnityEngine;
using Unity.VisualScripting;
[UnitCategory("Custom")]
public class TransparencyNodeURP : Unit
{
[DoNotSerialize]
public ControlInput enter { get; private set; }
[DoNotSerialize]
[PortLabel("TargetTransparency")]
public ValueInput targetTransparency { get; private set; }
[DoNotSerialize]
public ControlOutput exit { get; private set; }
protected override void Definition()
{
enter = ControlInput("enter", (flow) =>
{
float targetTransparencyValue = flow.GetValue<float>(this.targetTransparency);
GameObject go = flow.stack.gameObject;
if (go != null)
{
Renderer renderer = go.GetComponent<Renderer>();
if (renderer != null)
{
CoroutineRunner.instance.StartCoroutine(ChangeTransparency(renderer, targetTransparencyValue));
}
}
return exit;
});
exit = ControlOutput("exit");
targetTransparency = ValueInput<float>("TargetTransparency", 0.5f);
Succession(enter, exit);
Requirement(targetTransparency, enter);
}
private IEnumerator ChangeTransparency(Renderer renderer,float targetTransparency)
{
float duration = 1.0f; // 透明度变化的持续时间
float elapsedTime = 0.0f;
Material material = renderer.material;
Texture baseMap = material.GetTexture("_BaseMap");
Color initialColor = material.GetColor("_BaseColor");
Color targetColor = new Color(initialColor.r, initialColor.g, initialColor.b, targetTransparency); // 设置目标Alpha值为1
while (elapsedTime < duration)
{
float t = elapsedTime / duration;
Color newColor = Color.Lerp(initialColor, targetColor, t);
material.SetColor("_BaseColor", newColor);
elapsedTime += Time.deltaTime;
yield return null; // 等待下一帧
}
// 设置最终颜色
material.SetColor("_BaseColor", targetColor);
}
}