【问题标题】:How to resize all particles from a particle system?如何调整粒子系统中所有粒子的大小?
【发布时间】:2023-03-03 03:31:02
【问题描述】:

我正在尝试使用滑块动态调整粒子大小,以及更改它们的颜色。

粒子用于在 ​​3D 散点图中显示数据点。我正在使用此代码:https://github.com/PrinzEugn/Scatterplot_Standalone

private ParticleSystem.Particle[] particlePoints;
void Update () {
    pointScale = sizeSlider.value;
    for (int i = 0; i < pointList.Count; i++) {
        Quaternion quaternion = Camera.current.transform.rotation;
        Vector3 angles = quaternion.eulerAngles;
        // Set point color
        particlePoints[i].startColor = new Color(angles.x, angles.y, angles.z, 1.0f);
        particlePoints[i].transform.localScale = new Vector3(pointScale, pointScale, pointScale);
    }

}

问题是粒子没有变换方法,改变“startColour”不会改变任何东西。

API 声明“粒子的当前大小是根据此值和活动大小模块按程序计算的。”

这是什么意思,我怎样才能改变粒子的大小?

【问题讨论】:

  • 您到底想对尺寸做什么?您可以检查Particle.startSize 以在开始时为它们提供不同的尺寸......稍后它们会根据您的设置进行更改,例如SizeBySpeedSizeOverLifetime
  • 我希望用户能够使用滑块(或任何其他输入)调整它们的大小。
  • 请确定您真正想要更改的内容是粒子大小还是粒子系统大小?第一个问题很简单,更改ParticleSystem.main.startSize 会有意义但看起来很奇怪。粒子系统外观不仅受大小影响,还要考虑速度、力、粒子数...

标签: unity3d particle-system


【解决方案1】:

感谢以前的答案,我设法让这个工作:

在 PlacePrefabPoints 方法中,我将每个实例化的预制件添加到一个列表中,并向滑块添加一个侦听器,如下所示:

void changedPointSize(){
    pointScale = sizeSlider.value;
    for (int i = 0; i < objects.Count; i++) {
        objects[i].transform.localScale = new Vector3(pointScale, pointScale, pointScale);
    }
}

谢谢大家!

【讨论】:

    【解决方案2】:

    我刚刚看过PointRenderer.cs -> CreateParticlesPlacePrefabPoints 很好地提示了必须更改的内容。

    所以我猜你会简单地改变比例值

    foreach (var point in particlePoints) 
    {
        Quaternion quaternion = Camera.current.transform.rotation;
        Vector3 angles = quaternion.eulerAngles;
        // Set point color
        point.startColor = new Color(angles.x, angles.y, angles.z, 1.0f);
        point.startSize = sizeSlider.value;
    }
    

    然后重新调用

    GetComponent<ParticleSystem>().SetParticles(particlePoints, particlePoints.Length);
    

    你是否真的会在Update 中这样做是值得怀疑的。我宁愿在sizeSlider.onValueChanged 中执行此操作,仅在必要时执行(您甚至可以在更新视图之前设置必须更改的某个阈值)但对于颜色而言,除了在@987654329 中执行此操作之外可能没有其他选择@ 但至少在那里我会使用阈值:

    privtae ParticleSystem ps;
    
    // I assume you have that referenced in the inspector
    public Slider sizeSlider;
    
    // flag to control whether system should be updated
    private bool updateSystem;
    
    privtae void Awake()
    {
        ps = GetComponent<ParticleSystem>();
    }
    
    private void OnEnable()
    {
        // add a listener to onValueChanged
        // it is secure to remove it first (even if not there yet)
        // this makes sure it is not added twice
        sizeSlider.onValueChanged.RemoveListener(OnsliderChanged());
        sizeSlider.onValueChanged.AddListener(OnsliderChanged());
    }
    
    private void OnDisable()
    {
        // cleanup listener
        sizeSlider.onValueChanged.RemoveListener(OnsliderChanged());
    }
    
    private void OnSliderChanged()
    {
        foreach (var point in particlePoints) 
        {
            point.startSize = sizeSlider.value; 
        }
    
        // do the same also for the instantiated prefabs
        foreach(Transform child in PointHolder.transform)
        {
            child.localScale = Vecto3.one * sizeSlider.value;
        }
    
        updateSystem = true;
    }
    
    private Quaternion lastCameraRot;
    
    public float CameraUpdateThreshold;
    
    private void Update()
    {
        if(Quaternion.Angle(Camera.current.transform.rotation, lastCameraRot) > CameraUpdateThreshold)
        {
            foreach (var point in particlePoints) 
            {
                Quaternion quaternion = Camera.current.transform.rotation;
                Vector3 angles = quaternion.eulerAngles;
                // Set point color
                point.startColor = new Color(angles.x, angles.y, angles.z, 1.0f);
            }
    
            lastCameraRot = Camera.current.transform.rotation;
            updateSystem = true;
        }
    
        if(!updateSystem) return;
    
        updateSystem = false;
        ps.SetParticles(particlePoints, particlePoints.Length);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-09
      • 2012-06-28
      • 1970-01-01
      • 1970-01-01
      • 2019-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多