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(this.targetTransparency); GameObject go = flow.stack.gameObject; if (go != null) { Renderer renderer = go.GetComponent(); if (renderer != null) { CoroutineRunner.instance.StartCoroutine(ChangeTransparency(renderer, targetTransparencyValue)); } } return exit; }); exit = ControlOutput("exit"); targetTransparency = ValueInput("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); } }