You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity C# 使用Slider修改VFX Graph属性仅单个对象生效问题求助

问题根因
  • 代码中仅声明了单个VisualEffect类型的变量vfxval,在Start方法的遍历逻辑里,每次迭代都会用当前遍历到的VFX组件覆盖这个变量的值,遍历结束后vfxval只会保留数组中最后一个VFX对象的组件引用,所以Update阶段仅能更新最后一个对象的属性。
  • 没有对所有VFX的组件引用做持久化存储,前面遍历到的对象引用全部被后续赋值覆盖丢失。
修复方案

将单个VisualEffect变量改为数组类型,存储所有匹配到的VFX组件,Update阶段遍历整个数组统一赋值即可,修复后代码如下:

{
public GameObject[] vfxs;
// 改为数组存储所有VFX组件
VisualEffect[] allVfxComponents;
Slider slider;
float sliderVal;

// Start is called before the first frame update
void Start()
{
    vfxs = GameObject.FindGameObjectsWithTag("VFX");
    // 初始化组件数组长度和VFX对象数组长度一致
    allVfxComponents = new VisualEffect[vfxs.Length];
    for (int i = 0; i < vfxs.Length; i++)
    {
       allVfxComponents[i] = vfxs[i].GetComponent<VisualEffect>();
    }
    slider = this.GetComponent<Slider>();
}

// Update is called once per frame
void Update()
{
    sliderVal = slider.value * 100;
    // 遍历所有组件统一赋值
    foreach (var vfx in allVfxComponents)
    {
        vfx.SetFloat("Power", sliderVal);
    }
}
}

内容的提问来源于stack exchange,提问作者Jay Design

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 07:09:03