Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Can't set any new Particle System properties through script

Discussion in '5.3 Beta' started by ericwd, Oct 3, 2015.

  1. ericwd

    ericwd

    Joined:
    Aug 25, 2015
    Posts:
    3
    All the newly exposed modules seem to be read only. And even then they don't return what I'd expect. For example: Can't set Rotation over Lifetime to a constant value via script nor does it return the constant value I have set in the inspector.

    particleSystemProperties.jpg
     
    rikey, Ikaros and GibTreaty like this.
  2. Ikaros

    Ikaros

    Joined:
    Nov 7, 2013
    Posts:
    5
    Yup, the struct are read-only, the members of the struct are get/set'able, but then again you can't modify them individually so you're stuck in it. Gotta keep sticking with the deprecated variables until that's fixed or the workflow is documented.
     
  3. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    3,551
    Not sure why they are read only, I know that the z value is the one you would set if you are not using separate axis but not much help while its read only. Ill poke the developer.
     
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,669
    Actually, you can. The returned struct has custom setters that piggy-back to the original particle system. It was implemented this way for performance reasons.
     
    andrew-lukasik and karl_jones like this.
  5. markiboy2all

    markiboy2all

    Joined:
    Nov 6, 2013
    Posts:
    4
    Using the following code I was able to access most of the members. But when I try to get any value while the curvemode is not set to constant the following error is logged:
    and the curveMax and curveMin are always empty. Is this intentional, a bug or am I doing something wrong?

    Code (CSharp):
    1. var VelLifeTimeModule =this.GetComponent<ParticleSystem>().velocityOverLifetime;
    2. var Xcurve = VelLifeTimeModule.x;
    3. if(Xcurve.mode == ParticleSystemCurveMode.Constant)
    4. {
    5.     VelLifeTimeModule.space = ParticleSystemSimulationSpace.Local;
    6.     Xcurve.constantMax =1;
    7.     Debug.Log(VelLifeTimeModule.x.constantMax);
    8.     VelLifeTimeModule.x = Xcurve;
    9.     Debug.Log(VelLifeTimeModule.x.constantMax);
    10. }else if(Xcurve.mode == ParticleSystemCurveMode.Curve){
    11.     Debug.Log( Xcurve.curveMax);
    12.     Debug.Log( Xcurve.curveMin);
    13. }
     
  6. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    1,213
    Hey!

    It seems quite a lot of people are struggling to successfully use the new particle scripting API.
    It's not immediately intuitive, so we will improve the documentation to provide code examples.

    As mentioned already, you don't need to set the module back to the particle system, it is sufficient to say:

    Code (CSharp):
    1. var vel = GetComponent<ParticleSystem>().velocityOverLifetime;
    2. vel.myProp = 5.0f;
    Furthermore, the error you are seeing about getting curves is to be expected.
    Due to the way curves are compressed in the players, it is not possible to get curves unless they are using one of the two constant modes. We hope to address this in the future - we understand it can be quite limiting.

    However, you can build your own curves in script and set them, using any mode, for example here is a random between 2 curves, with a multiplier of 5: (curves must be defined in the 0-1 range, so the multiplier is used for building curves outside this range.)

    Code (CSharp):
    1. var rot = GetComponent<ParticleSystem>().rotationOverLifetime;
    2.  
    3. AnimationCurve curve = new AnimationCurve();
    4. curve.AddKey( 0.0f, 0.1f );
    5. curve.AddKey( 0.75f, 0.6f );
    6.  
    7. AnimationCurve curve2 = new AnimationCurve();
    8. curve2.AddKey( 0.0f, 0.2f );
    9. curve2.AddKey( 0.5f, 0.9f );
    10.  
    11. rot.angularVelocity = new ParticleSystem.MinMaxCurve( 5.0f, curve, curve2 );
    Anyway, we will get all this functionality documented, so it's easier to understand!
     
  7. markiboy2all

    markiboy2all

    Joined:
    Nov 6, 2013
    Posts:
    4
    Hi,


    Thank you for your quick response! Too bad that I can't get the curves outside of the constant modes. But since it is possible to set them, I am going to try to make a custom editor script in which curves can be defined that will replace the curves in the particlesystem.


    Since I am trying to create some sort of slow-motion effect for articles that are inside of a sphere ( I want to slow down individual particles) I need some way of reading the velocity of the particle but the velocity set in the velocity over lifetime module isn't added to the particle velocity. I wonder why this doesn't happen.


    Because of this I wanted to use the curve and find the corresponding velocity using AnimationCurve.Evaluate to get the velocity. But for the random values this is, of course, not possible. Is there a way to recalculate the random value?


    Sorry for all these questions, but I promise these are the last (for now :p )
     
  8. Chris-Herold

    Chris-Herold

    Joined:
    Nov 14, 2011
    Posts:
    91
    Very excited to see these particle system improvements!

    However, i wonder why the access to modules was implemented via properties instead of
    GetModule<T> where T: IParticleSystemModule.

    That doesn't seem to be very future proof - unless there is no intention to ever let us implement our own modules.
    Or if that is coming one day, i see API changes down the road and migration hassle...

    I applauded when you removed all the MonoBehaviour shortcuts (.renderer, .camera, .collider),
    imho this would be the right time to give ParticleSystem the same treatment ;)
     
    landon912 likes this.
  9. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    7,502
    I understand why the curves wouldn't be available for play since they're being optimized away, but what about in editor? A lot of the usefulness of direct script control of particle systems is for custom editors or augmentation of the existing tools.
     
  10. RSERyan

    RSERyan

    Joined:
    Sep 19, 2012
    Posts:
    6
    I also can't seem to set particleSystem.emission.enabled = false. enabled is get/set, but emission is a struct so can't set it directly (like you can't do transform.position.x = value). But emission is get only, so I can assign it to a local value, change emission.enabled, and set it back
     
  11. chrismarch

    chrismarch

    Joined:
    Jul 24, 2013
    Posts:
    318
    I ran into the same issue when upgrading. I decided to try using ParticleSystem.Play/Stop again, although we decided not to use that in the past in favor of the obsolete emissionEnabled property for reasons I am not aware of at the moment.

    [EDIT] found the recommended solution which sets emission.enabled after getting a struct reference: http://forum.unity3d.com/threads/enabling-emission.364258/#post-2356966
     
    Last edited: Dec 2, 2015
    rikey and RSERyan like this.