Unity3D(五)粒子系统

粒子系统算是最近作业中相对比较简单的
目的是实现这个效果: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);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值