How to use code to change the parameters of the new Unity particle system

Unity's latest particle system uses code to control various parameters

          The new particle system API has been changed a lot and adopted a lot of structures. Many of the original APIs are outdated, or there is no earlier. I found it on the Internet and it is very small. The following Xiaobai will show how to use code control Various parameters (here is only a small part, if you are interested, you can try it yourself, !! a hundred will!!)

  I used to write and write casually before, it may be messy, now I rearrange it, I use unity5.5

   

1.1 code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ParticleTest : MonoBehaviour
{
    public ParticleSystem particleSys;
    ParticleSystem.EmissionModule parEmission;
    ParticleSystem.ShapeModule parShape;
    //----------------------
    ParticleSystem.MinMaxCurve parMinMaxCurve;
    void Start()
    {
        parEmission = particleSys.emission;
        parShape = particleSys.shape;
        //-----------------
        parMinMaxCurve = new ParticleSystem.MinMaxCurve(20);
        parEmission.rateOverTime = parMinMaxCurve;
    }
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.O))
        {
            particleSys.Play();
        }
        if (Input.GetKeyDown(KeyCode.S))
        {
            particleSys.Stop();
        }
        if (Input.GetKeyDown(KeyCode.E))
        {
            SetParEmission(50, 1000);
        }
        if (Input.GetKeyDown(KeyCode.A))
        {
            ChangeParEmission(10);
        }
        if (Input.GetKeyDown(KeyCode.H))
        {
            SetParShape();
        }
    }
    /// <summary>
    /// 设置emission
    /// </summary>
    private void SetParEmission(float min, float max)
    {
        //单独设置
        //parEmission.rateOverTimeMultiplier = min;
        //or parEmission.rateOverDistanceMultiplier = min;
        //Curve设置
        parEmission.rateOverDistance = new ParticleSystem.MinMaxCurve(min, max);
        //or  parEmission.rateOverTime = new ParticleSystem.MinMaxCurve(min, max);//设置粒子数量,min10 max100;
    }
    private void ChangeParEmission(float addRate)
    {
        parMinMaxCurve.constant += 10;
        parEmission.rateOverTime = parMinMaxCurve;
        //另一种
        //如果设置
        //parMinMaxCurve = new ParticleSystem.MinMaxCurve(10,100);
        //parEmission.rateOverTime = parMinMaxCurve;
        //那么
        //if (parMinMaxCurve.constantMin < parMinMaxCurve.constantMax)
        //{
        //    parMinMaxCurve.constantMin += addRate;
        //    parEmission.rateOverTime = parMinMaxCurve;
        //}
    }
    private void SetParShape()
    {
        parShape.shapeType = ParticleSystemShapeType.Box;//设置为box形状
        parShape.box = new Vector3(2, 2, 2); //设置长宽高
    }
}



2: In addition, the new version of the example system allows particles to be affected by WindZone wind, just check External forces




Guess you like

Origin blog.csdn.net/K20132014/article/details/51973715