粒子系统算是最近作业中相对比较简单的
目的是实现这个效果:http://i-remember.fr/en
老师提供了可供参考的博客,我参考的是这一篇:
http://blog.csdn.net/gunnerczh/article/details/51291348
实现效果如下:
代码如下:
HaloParticle.cs(方便粒子属性的处理)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HaloParticle {
public HaloParticle(float r = 0, float a = 0) {
radius = r;
angle = a;
}
public float radius {
get;
set;
}
public float angle {
get;
set;
}
}
InnerHalo.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InnerHalo : MonoBehaviour {
public ParticleSystem particleSystem;
private ParticleSystem.Particle[] particleArr;
private int haloResolution = 3000;
private float minRadius = 2F;
private float maxRadius = 4.5F;
private HaloParticle[] haloParticle;
// Use this for initialization
void Start () {
particleSystem = this.GetComponent<ParticleSystem> ();
particleArr = new ParticleSystem.Particle[haloResolution];
haloParticle = new HaloParticle[haloResolution];
particleSystem.maxParticles = haloResolution;
particleSystem.Emit (haloResolution);
particleSystem.GetParticles (particleArr);
for (int i = 0; i < haloResolution; i++) {
//radius
float deltaMinRadius = Random.Range (1, ((minRadius+maxRadius)/2)/minRadius);
float deltaMaxRadius = Random.Range (((minRadius+maxRadius)/2)/maxRadius, 1);
float radius = Random.Range (minRadius * deltaMinRadius, maxRadius * deltaMaxRadius);
//angle
float deltaMinAngle = Random.Range(1, (Mathf.PI * 3 / 2)/ Mathf.PI);
float deltaMaxAngle = Random.Range ((Mathf.PI * 3 / 2) / Mathf.PI / 2, 1);
float angle = Random.Range(Mathf.PI * deltaMinAngle, Mathf.PI * 2 * deltaMaxAngle) - Mathf.PI / 4;
if (Random.Range (0, 100) > 50) {
angle -= Mathf.PI;
}
haloParticle [i] = new HaloParticle (radius, angle);
particleArr [i].position = new Vector3 (radius * Mathf.Cos(angle), radius * Mathf.Sin(angle), 0);
}
particleSystem.SetParticles (particleArr, particleArr.Length);
}
// Update is called once per frame
void Update () {
for (int i = 0; i < haloResolution; i++) {
haloParticle [i].angle -= Random.Range (0, 1F/360);
particleArr [i].position = new Vector3 (haloParticle[i].radius * Mathf.Cos(haloParticle[i].angle), haloParticle[i].radius * Mathf.Sin(haloParticle[i].angle), 0);
}
particleSystem.SetParticles (particleArr, particleArr.Length);
}
}
OuterHalo.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OuterHalo : MonoBehaviour {
public ParticleSystem particleSystem;
private ParticleSystem.Particle[] particleArr;
private int haloResolution = 3000;
private float minRadius = 2F;
private float maxRadius = 4.5F;
private HaloParticle[] haloParticle;
// Use this for initialization
void Start () {
particleSystem = this.GetComponent<ParticleSystem> ();
particleArr = new ParticleSystem.Particle[haloResolution];
haloParticle = new HaloParticle[haloResolution];
particleSystem.maxParticles = haloResolution;
particleSystem.Emit (haloResolution);
particleSystem.GetParticles (particleArr);
for (int i = 0; i < haloResolution; i++) {
//radius
float deltaMinRadius = Random.Range (1, ((minRadius+maxRadius)/2)/minRadius);
float deltaMaxRadius = Random.Range (((minRadius+maxRadius)/2)/maxRadius, 1);
float radius = Random.Range (minRadius * deltaMinRadius, maxRadius * deltaMaxRadius);
//angle
float angle = Random.Range(0, Mathf.PI * 2);
haloParticle [i] = new HaloParticle (radius, angle);
particleArr [i].position = new Vector3 (radius * Mathf.Cos(angle), radius * Mathf.Sin(angle), 0);
}
particleSystem.SetParticles (particleArr, particleArr.Length);
}
// Update is called once per frame
void Update () {
for (int i = 0; i < haloResolution; i++) {
haloParticle [i].angle -= Random.Range (0, 1F/360);
particleArr [i].position = new Vector3 (haloParticle[i].radius * Mathf.Cos(haloParticle[i].angle), haloParticle[i].radius * Mathf.Sin(haloParticle[i].angle), 0);
}
particleSystem.SetParticles (particleArr, particleArr.Length);
}
}