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

76 行
2.4 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.VisualScripting;
[UnitCategory("Custom")]
public class LightNode : Unit
{
[DoNotSerialize]
public ControlInput enter { get; private set; }
[DoNotSerialize]
public ControlOutput exit { get; private set; }
[DoNotSerialize]
[PortLabel("TargetColor")]
public ValueInput TargetColorValueInput { get; private set; }
protected override void Definition()
{
enter = ControlInput("enter", (flow) =>
{
Color targetColor = flow.GetValue<Color>(TargetColorValueInput);
GameObject go = flow.stack.gameObject;
if (go != null)
{
Renderer renderer = go.GetComponent<Renderer>();
if (renderer != null)
{
// 启动颜色渐变协程
CoroutineRunner.instance.StartCoroutine(ChangeEmissionColor(renderer, targetColor));
}
}
// 继续执行流程
return exit;
});
// 定义输入和输出端口
exit = ControlOutput("exit");
TargetColorValueInput = ValueInput<Color>("TargetColor", Color.yellow);
// 连接节点
Succession(enter, exit);
Requirement(TargetColorValueInput, enter);
}
private IEnumerator ChangeEmissionColor(Renderer renderer, Color targetColor)
{
float duration = 1.0f; // 渐变的持续时间,你可以根据需要进行调整
float elapsedTime = 0.0f;
MaterialPropertyBlock mpb = new MaterialPropertyBlock();
renderer.GetPropertyBlock(mpb);
Color initialColor = mpb.GetColor("_EmissionColor");
while (elapsedTime < duration)
{
float t = elapsedTime / duration;
Color newColor = Color.Lerp(initialColor, targetColor, t);
mpb.SetColor("_EmissionColor", newColor);
renderer.SetPropertyBlock(mpb);
renderer.material.EnableKeyword("_EMISSION");
elapsedTime += Time.deltaTime;
yield return null; // 等待下一帧
}
// 确保最终颜色与目标颜色匹配
mpb.SetColor("_EmissionColor", targetColor);
renderer.SetPropertyBlock(mpb);
renderer.material.EnableKeyword("_EMISSION");
}
}