Search Unity

Particle Scaling for Particle-UI-Elements

Discussion in 'UGUI & TextMesh Pro' started by tawsm, May 27, 2015.

  1. tawsm

    tawsm

    Joined:
    Nov 12, 2013
    Posts:
    43
    Hi,
    I still cannot find a way to scale my particle systems if I use them integrated in the UI. They work perfectly within the new UI, sorting, rearranged etc., but they can't be scaled, which is crucial for different resolutions!

    It was possible to perfectly scale particle systems via code, by using e.g.:
    SerializedObject.FindProperty("Porpertyname").floatValue *= ScalingValue;
    Unfortunately SerializedObject is not accessible in runtime anymore.
    (http://forum.unity3d.com/threads/ac...dobject-serializedproperty-in-runtime.205738/)

    Please Unity, give us the possibility to access these values somehow. It would solve my problem in no-time.

    (Here is a hacky workaround which may work for some cases:
    http://forum.unity3d.com/threads/shocked-particle-emitter-not-scalable-solved.104817/
    But it does not scale a complete particleSystems with curves and everything, so still no soultion.)

    BTW, here is in old thread for ppl having problems integrating PS in the new UI.
    http://forum.unity3d.com/threads/particles-within-the-new-gui.265215/#post-2130471

    If anyone has a solution, i would be eternally grateful!
    Cheers
     
    Last edited: May 27, 2015
  2. tawsm

    tawsm

    Joined:
    Nov 12, 2013
    Posts:
    43
    Particle System Scaling - Not working for velocities
    Update:

    the 5.3 patch release claims full particle scaling, but as i see it velocities over lifetime are not being scaled at all. Here is an example, size scales, velocities don't:

    Am i mistaken? This is crucial when scaling particle systems, so will it come in the future?

    I also have one other question: the patchnotes say "Particles: All UI settings exposed to scripting API", are they read only or can i access the velocities this way and scale them by hand?

    Cheers
     
    Last edited: Jan 5, 2016
  3. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,283
  4. tawsm

    tawsm

    Joined:
    Nov 12, 2013
    Posts:
    43
    After i wasted an hour on trying to scale my velocity curves via script, I stumbled upon this thread and obviously it's not possible to scale velocity curves because you cannot get them:
    http://forum.unity3d.com/threads/cant-set-any-new-particle-system-properties-through-script.358964/
    Particle scaling needs velocity and force scaling to work for complex particle simulations. Why does Unreal know that and Unity doesn't? Please don't state complete particle scaling and "All UI settings exposed to scripting API" in your patch notes, if it's not correct. Why tell us this in a forum post only? This just costs us time.

    Thank you for your answer anyway.
     
  5. glennpow

    glennpow

    Joined:
    Jan 30, 2012
    Posts:
    56
  6. tawsm

    tawsm

    Joined:
    Nov 12, 2013
    Posts:
    43
    Since the Unity 5.4 update particle scaling works just fine now, here is the script i'm using:


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. /* UI Particle Scaling script by Lost Mountain, questions: tobias@lostmountaingames.com, http://lostmountaingames.com/
    6. * Just drag this script on any canvas which has child particle systems underneath.
    7. * Keep in mind, you should run this script when you change resolutions.
    8. * Prewarmed particle system emitting on begin play, will probably not be affected by the script the first time they fire.
    9. * Feel free to use this script in any project.
    10. * If you change/enhance this script, please share your optimizations with the community.
    11. */
    12.  
    13.  
    14. [RequireComponent(typeof(Canvas))]
    15. public class UIParticleScaler: MonoBehaviour {
    16.  
    17.     private Canvas refCanvas;
    18.     private float scale;
    19.     private ParticleSystem[] particleSystems;
    20.  
    21.     //Initialization
    22.     void Start() {
    23.         refCanvas = GetComponent<Canvas>();
    24.         scale = refCanvas.transform.localScale.x;
    25.         ApplyScale();
    26.     }
    27.  
    28.     //Scale all child particle systems
    29.     public void ApplyScale() {
    30.         particleSystems = this.gameObject.GetComponentsInChildren<ParticleSystem>();
    31.         foreach (ParticleSystem p in particleSystems) {
    32.             ScaleParticleValues(p);
    33.         }
    34.     }
    35.  
    36.     //Scale individiual particle system values
    37.     private void ScaleParticleValues(ParticleSystem ps) {
    38.         //BASE VALUES
    39.         //StartSize
    40.         ps.startSize *= scale;
    41.         //Gravity
    42.         ps.gravityModifier *= scale;
    43.         //StartSpeed
    44.         if (ps.startSpeed > 0)
    45.             ps.startSpeed *= scale;
    46.  
    47.  
    48.         //MODULES
    49.         //Shape (divided instead of multiplied)
    50.         var shape = ps.shape;
    51.         if (shape.enabled) {
    52.             shape.radius /= scale;
    53.             shape.box = shape.box / scale;
    54.         }
    55.         //Emisison (divided instead of multiplied)
    56.         ParticleSystem.EmissionModule em = ps.emission;
    57.         if (em.enabled) {
    58.             ParticleSystem.MinMaxCurve emRate = em.rate;
    59.             em.rate = ScaleMinMaxCurve(emRate, scale, false);
    60.         }
    61.  
    62.         //Velocities
    63.         ParticleSystem.VelocityOverLifetimeModule vel = ps.velocityOverLifetime;
    64.         if(vel.enabled) {
    65.             vel.x = ScaleMinMaxCurve(vel.x, scale);
    66.             vel.y = ScaleMinMaxCurve(vel.y, scale);
    67.             vel.z = ScaleMinMaxCurve(vel.z, scale);      
    68.         }
    69.  
    70.         //ClampVelocities
    71.         ParticleSystem.LimitVelocityOverLifetimeModule clampVel = ps.limitVelocityOverLifetime;
    72.         if(clampVel.enabled) {
    73.             clampVel.limitX = ScaleMinMaxCurve(clampVel.limitX, scale);
    74.             clampVel.limitY = ScaleMinMaxCurve(clampVel.limitY, scale);
    75.             clampVel.limitZ = ScaleMinMaxCurve(clampVel.limitZ, scale);
    76.         }
    77.  
    78.         //Forces
    79.         ParticleSystem.ForceOverLifetimeModule force = ps.forceOverLifetime;
    80.         if (force.enabled) {
    81.             force.x = ScaleMinMaxCurve(force.x, scale);
    82.             force.y = ScaleMinMaxCurve(force.y, scale);
    83.             force.z = ScaleMinMaxCurve(force.z, scale);
    84.         }
    85.     }
    86.  
    87.     private ParticleSystem.MinMaxCurve ScaleMinMaxCurve(ParticleSystem.MinMaxCurve curve, float scale, bool multiply = true) {
    88.         if (multiply) {
    89.             curve.curveScalar *= scale;
    90.             curve.constantMin *= scale;
    91.             curve.constantMax *= scale;
    92.         } else {
    93.             curve.curveScalar /= scale;
    94.             curve.constantMin /= scale;
    95.             curve.constantMax /= scale;
    96.         }
    97.         ScaleCurve(curve.curveMin, scale, multiply);
    98.         ScaleCurve(curve.curveMax, scale, multiply);
    99.         return curve;
    100.     }
    101.  
    102.     private void ScaleCurve(AnimationCurve curve, float scale, bool multiply = true) {
    103.         if (curve == null) { return; }
    104.         if (multiply)
    105.             for (int i = 0; i < curve.keys.Length; i++) { curve.keys[i].value *= scale; }
    106.         else
    107.             for (int i = 0; i < curve.keys.Length; i++) { curve.keys[i].value /= scale; }
    108.     }
    109. }
    110.  
     
    Last edited: Nov 18, 2016