playMaker

Author Topic: Particle System Set Random Between Two Colors  (Read 1211 times)

Christoph

  • Beta Group
  • Junior Playmaker
  • *
  • Posts: 79
Particle System Set Random Between Two Colors
« on: July 27, 2021, 06:00:09 PM »
Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory(ActionCategory.Effects)]
[Tooltip("Sets the Start Color of a ParticleSystem using Random Between Two Colors.")]
public class ParticleSystemSetRandomBetweenTwoColors : ComponentAction<ParticleSystem>
{
        [RequiredField]
        [Tooltip("The GameObject with the ParticleSystem.")]
        [CheckForComponent(typeof(ParticleSystem))]
        public FsmOwnerDefault gameObject;

        [Tooltip("Color 1 of the Start Color")]
        public FsmColor color1;

        [Tooltip("Color 2 of the Start Color")]
        public FsmColor color2;

        [ActionSection("Repeat")]
        [Tooltip("Repeat every frame. Useful if the color variables are changing.")]
        public bool everyFrame;

        private ParticleSystem currentParticleSystem;
        private ParticleSystem.MainModule main;

        public override void Reset()
        {
            gameObject = null;
            color1 = Color.black;
            color2 = Color.black;
            everyFrame = false;
        }

        public override void OnEnter()
{
            currentParticleSystem = gameObject.GameObject.Value.GetComponent<ParticleSystem>();
            main = currentParticleSystem.main;


            SetColors();

            if (!everyFrame)
            {
                Finish();
            }
        }

        public override void OnUpdate()
        {
            SetColors();
        }

        private void SetColors()
        {
            main.startColor = new ParticleSystem.MinMaxGradient(color1.Value, color2.Value);
        }
    }
}

Seems to work, any feedback welcome. Cheers!