简体   繁体   English

如何在Unity中保留对粒子系统模块结构的引用

[英]How to keep a reference to a Particle System module's struct in Unity

I'd like to keep a reference to a MinMaxCurve, a struct in some particle system module. 我想保留对MinMaxCurve(在某些粒子系统模块中的结构)的引用。

I need this ability to refer to any MinMaxCurve generically, rather than always dealing with the specific module and attribute. 我需要这种能力来通用地引用任何MinMaxCurve,而不是始终处理特定的模块和属性。

Here's the gist: 这是要点:

public class ParticleAttributeMultiplier : ParticleAttributeAdjuster {

    new private ParticleSystem particleSystem;
    private ParticleSystem.EmissionModule emission;
    private ParticleSystem.MinMaxCurve minMaxCurve;

    public override void Start () {
        particleSystem = GetComponent<ParticleSystem> ();
        emission = particleSystem.emission;
        minMaxCurve = emission.rateOverTime;
    }

    public override void Input (float intensity) {
        minMaxCurve = 1000f;
    }
}

This does not work because the emission and rateOverTime are both structs , returned by value: 这不起作用,因为mission和rateOverTime都是struct均按值返回:

public struct EmissionModule
{
    // ... 
    public MinMaxCurve rateOverTime { get; set; } // also a struct
    // ... 
}

Is it possible, in any way, to store a reference to rateOverTime as a MinMaxCurve? 是否可以通过任何方式将对rateOverTime的引用存储为MinMaxCurve?


This seems to be the crux of the problem: 这似乎是问题的症结所在:

// "A property or indexer may not be passed as an out or ref parameter"
ref ParticleSystem.MinMaxCurve minMaxCurve = ref emission.rateOverTime;

Really appreciate any geniuses out there who can solve my problem. 非常感谢能解决我问题的任何天才。

You can't. 你不能

ParticleSystem.MinMaxCurve is a structvalue type ParticleSystem.MinMaxCurve是一个struct值类型

Just like eg Vector3 the variable is stored by value not using a reference like it would the case for a class . 就像例如Vector3的变量由不使用像它会为一个的情况下的参照存储class

The only way is storing a reference to the according parent class which in your case is the particleSystem reference and access the value through it. 唯一的方法是存储对相应父class的引用,在您的情况下,该父classparticleSystem引用,并通过该引用访问值。 You could get this reference stored by making it 您可以通过制作此参考来存储它

 [SerializeField] private ParticleSystem _particleSystem;

and drag it in via the Inspector. 并通过检查器将其拖入。


You can however store and adjust a generic ParticleSystem.MinMaxCurve value via the Inspector if that is what you are looking for 但是,如果您要查找的是,则可以通过检查器存储和调整通用的ParticleSystem.MinMaxCurve值。

[SerializeField] private ParticleSystem.MinMaxCurve minMaxCurve;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM