using UnityEngine;
using System.Collections;
[RequireComponent( typeof( ParticleSystem ) )]
public class CustomParticleSystem : MonoBehaviour
{
#region Properties
public Mesh[] EmissionShapes;
public float EmissionShapeSwitchSpeed = 0.5f;
public float EmissionRate = 10f;
#endregion
float _shapeIndex = 0f;
float _timeToEmission = 0f;
ParticleSystem _particleSystem;
float EmissionPeriod { get { return 1f / EmissionRate; } }
void Awake ()
{
_particleSystem = GetComponent<ParticleSystem>();
_particleSystem.emissionRate = 0f;
_timeToEmission = EmissionPeriod;
}
void Start ()
{
}
void Update ()
{
_shapeIndex += EmissionShapeSwitchSpeed * Time.deltaTime;
if( _shapeIndex > EmissionShapes.Length-1 ) _shapeIndex -= (EmissionShapes.Length-1);
_timeToEmission -= Time.deltaTime;
if( _timeToEmission <= 0f )
{
_timeToEmission = EmissionPeriod - _timeToEmission;
Mesh currentShape = EmissionShapes[(int)_shapeIndex];
int triangleIndex = Random.Range( 0, currentShape.triangles.Length/3 );
int vertexIndex0 = currentShape.triangles[triangleIndex*3];
int vertexIndex1 = currentShape.triangles[triangleIndex*3+1];
int vertexIndex2 = currentShape.triangles[triangleIndex*3+2];
Vector3 v0 = currentShape.vertices[vertexIndex0];
Vector3 v1 = currentShape.vertices[vertexIndex1];
Vector3 v2 = currentShape.vertices[vertexIndex2];
Vector3 n0 = currentShape.normals[vertexIndex0];
Vector3 n1 = currentShape.normals[vertexIndex1];
Vector3 n2 = currentShape.normals[vertexIndex2];
float u = Random.Range( 0f, 1f );
float v = Random.Range( 0f, 1f );
float w = Random.Range( 0f, 1f );
float uvw = u+v+w;
u /= uvw;
v /= uvw;
w /= uvw;
Vector3 randomPosition = v0*u + v1*v + v2*w;
Vector3 normalAtRandomPosition = n0*u + n1*v + n2*w;
randomPosition = this.transform.localToWorldMatrix.MultiplyPoint( randomPosition );
normalAtRandomPosition = this.transform.localToWorldMatrix.MultiplyVector( normalAtRandomPosition );
_particleSystem.Emit(
randomPosition,
normalAtRandomPosition * _particleSystem.startSpeed,
_particleSystem.startSize,
_particleSystem.startLifetime,
_particleSystem.startColor
);
}
}
}