1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
| using System.Collections; using System.Collections.Generic; using UnityEngine;
public class : MonoBehaviour
public class circleParticle public float radius = 0.0f; public float angle = 0.0f; public float time = 0.0f; public float originRadius = 0.0f; public circleParticle(float radius, float angle, float time) this.radius = radius; this.angle = angle; this.time = time; this.originRadius = radius;
public ParticleSystem particleSystem; private ParticleSystem.Particle[] particlesArray; private circleParticle[] particleAttr; public int particleSum = 10000; public float minRadius = 5.0f; public float maxRadius = 10.0f; public int Part = 2; public float minSpeed = 0.09f; public float maxSpeed = 0.12f; public float speedLevelSum = 9; public float driftRange = 0.03f; public int mouseInRingTime; public int mouseInRingTimeLimit;
public Gradient colorGradient;
void Start() particleAttr = new circleParticle[particleSum]; particlesArray = new ParticleSystem.Particle[particleSum]; particleSystem.maxParticles = particleSum; particleSystem.Emit(particleSum); particleSystem.GetParticles(particlesArray);
GradientAlphaKey[] alphaKeys = new GradientAlphaKey[5]; alphaKeys[0].time = 0.0f; alphaKeys[0].alpha = 1.0f; alphaKeys[1].time = 0.4f; alphaKeys[1].alpha = 0.4f; alphaKeys[2].time = 0.6f; alphaKeys[2].alpha = 1.0f; alphaKeys[3].time = 0.9f; alphaKeys[3].alpha = 0.4f; alphaKeys[4].time = 1.0f; alphaKeys[4].alpha = 0.9f; GradientColorKey[] colorKeys = new GradientColorKey[2]; colorKeys[0].time = 0.0f; colorKeys[0].color = Color.white; colorKeys[1].time = 1.0f; colorKeys[1].color = Color.white; colorGradient.SetKeys(colorKeys, alphaKeys);
mouseInRingTime = 0; mouseInRingTimeLimit = 30;
for (int i = 0; i < particleSum; i++) float randomAngle = Random.Range(0.0f, 360.0f);
float midRadius = (maxRadius + minRadius) / 2; float minRate = Random.Range(1.0f, midRadius / minRadius); float maxRate = Random.Range(midRadius / maxRadius, 1.0f); float randomRadius = Random.Range(minRadius * minRate, maxRadius * maxRate);
float randomTime = Random.Range (0, 10f);
particleAttr[i] = new circleParticle(randomRadius, randomAngle, randomTime); particlesArray[i].position = new Vector3(randomRadius * Mathf.Cos(randomAngle), randomRadius * Mathf.Sin(randomAngle), 0.0f); particleSystem.SetParticles(particlesArray, particleSum);
void Update() float mouseToCenter = Mathf.Sqrt (Mathf.Pow((Input.mousePosition.x - Screen.width / 2), 2f) + Mathf.Pow((Input.mousePosition.y - Screen.height / 2), 2f)); bool mouseInRing = false; if (mouseToCenter < Screen.height / 4) mouseInRing = true; if (mouseInRingTime <= mouseInRingTimeLimit) mouseInRingTime++; else mouseInRing = false; if (mouseInRingTime > 0) mouseInRingTime--;
for (int i = 0; i < particleSum; i++) float speed = (i % Part == 0) ? Random.Range(minSpeed, maxSpeed) : - 2 * Random.Range(minSpeed, maxSpeed); float weightedSpeed = (i % speedLevelSum + 1) * speed;
particleAttr[i].angle += Mathf.Sqrt(2 * particleAttr [i].radius / maxRadius) * weightedSpeed; particleAttr[i].angle = particleAttr[i].angle % 360; float radian = particleAttr[i].angle / 180 * Mathf.PI; particleAttr [i].time += Time.deltaTime; particleAttr [i].radius += Mathf.PingPong(particleAttr [i].time / minRadius / maxRadius, driftRange) - driftRange / 2.0f;
if (mouseInRingTime == 0) particleAttr [i].originRadius = particleAttr[i].radius;
if (mouseInRing) particleAttr[i].angle -= 1 / 3 * Mathf.Sqrt(2 * particleAttr [i].radius / maxRadius) * weightedSpeed; particleAttr[i].angle = particleAttr[i].angle % 360; radian = particleAttr[i].angle / 180 * Mathf.PI; if (mouseInRingTime < mouseInRingTimeLimit) particleAttr [i].radius = minRadius + (particleAttr [i].radius - minRadius) * (particleAttr [i].radius / maxRadius); if (!mouseInRing && mouseInRingTime > 0) particleAttr[i].angle += mouseInRingTime / 5 * Mathf.Sqrt(2 * particleAttr [i].radius / maxRadius) * weightedSpeed; particleAttr[i].angle = particleAttr[i].angle % 360; radian = particleAttr[i].angle / 180 * Mathf.PI;
particleAttr [i].radius += (particleAttr [i].originRadius - particleAttr [i].radius) / Mathf.Sqrt(mouseInRingTime) * (particleAttr [i].radius / maxRadius);
particlesArray[i].position = new Vector3(particleAttr [i].radius * Mathf.Cos(radian), particleAttr [i].radius * Mathf.Sin(radian), 0f);
particlesArray[i].color = colorGradient.Evaluate(particleAttr[i].angle / 360.0f); particleSystem.SetParticles(particlesArray, particleSum);
|