Example #1
0
    public void AddNewParticleEffect()
    {
        string id   = System.Guid.NewGuid().ToString();
        string name = "My particle effect";

        ParticleEffectContent content = new ParticleEffectContent();

        content.burstCount = 10;
        content.duration   = 1;
        content.shapeType  = ParticleEffectContent.ShapeType.Sphere;
        content.speed      = 1;
        content.rotationOverLifetime.Add(new ParticleEffectContent.FloatStop(2, 0));
        content.rotationOverLifetime.Add(new ParticleEffectContent.FloatStop(2, 1));
        content.startSize = 1;
        content.endSize   = 1;
        content.colorOverLifetime.Add(new ParticleEffectContent.ColorStop(Color.cyan, 0));
        content.colorOverLifetime.Add(new ParticleEffectContent.ColorStop(Color.cyan, 1));

        ParticleEffect particleEffect = new ParticleEffect(id, name, content);

        particleEffectSystem.PutParticleEffect(particleEffect);

        RepopulateList();
        SelectParticleEffect(particleEffect.id);
    }
 public ParticleEffect(string id, string name, ParticleEffectContent content)
 {
     this.id      = id;
     this.name    = name;
     this.content = content;
 }
    private void SetParticleSystemProps(ParticleEffectContent content, GameObject go, bool stream)
    {
        ParticleSystem particleSystem = go.GetComponent <ParticleSystem>();

        ParticleSystem.MainModule main = particleSystem.main;
        main.simulationSpace = content.isLocal ? ParticleSystemSimulationSpace.Local : ParticleSystemSimulationSpace.World;
        main.duration        = content.duration;
        main.startLifetime   = content.duration;
        main.gravityModifier = content.gravityModifier;

        ParticleSystem.ShapeModule shape = particleSystem.shape;
        switch (content.shapeType)
        {
        case ParticleEffectContent.ShapeType.Cone:
            shape.shapeType = ParticleSystemShapeType.Cone;
            break;

        case ParticleEffectContent.ShapeType.Sphere:
            shape.shapeType = ParticleSystemShapeType.Sphere;
            break;

        case ParticleEffectContent.ShapeType.Box:
            shape.shapeType = ParticleSystemShapeType.Box;
            break;

        case ParticleEffectContent.ShapeType.Circle:
            shape.shapeType = ParticleSystemShapeType.Circle;
            break;

        case ParticleEffectContent.ShapeType.Rectangle:
            shape.shapeType = ParticleSystemShapeType.Rectangle;
            break;
        }
        shape.scale = Vector3.one * content.shapeSize;

        ParticleSystem.EmissionModule emission = particleSystem.emission;
        emission.enabled = true;
        if (stream)
        {
            emission.rateOverTime = content.burstCount;
        }
        else
        {
            emission.rateOverTime = 0;
            emission.SetBursts(
                new ParticleSystem.Burst[] { new ParticleSystem.Burst(0.0f, content.burstCount) });
        }

        ParticleSystem.VelocityOverLifetimeModule velocityOverLifetime = particleSystem.velocityOverLifetime;
        velocityOverLifetime.enabled       = true;
        velocityOverLifetime.speedModifier = content.speed;

        ParticleSystem.RotationOverLifetimeModule rotationOverLifetime = particleSystem.rotationOverLifetime;
        rotationOverLifetime.enabled = true;
        SetRotationOverLifetime(rotationOverLifetime, content.rotationOverLifetime);

        ParticleSystem.SizeOverLifetimeModule sizeOverLifetime = particleSystem.sizeOverLifetime;
        sizeOverLifetime.enabled = true;
        SetSizeOverLifetime(sizeOverLifetime, content.startSize, content.endSize);

        ParticleSystem.ColorOverLifetimeModule colorOverLifetime = particleSystem.colorOverLifetime;
        colorOverLifetime.enabled = true;
        colorOverLifetime.color   = ColorTuplesToGradient(content.colorOverLifetime);

        ParticleSystemRenderer renderer = go.GetComponent <ParticleSystemRenderer>();

        renderer.material = particleMaterial;
    }