Guest User

Untitled

a guest
Nov 24th, 2019
117
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4.  
  5. public class Window_Confetti : MonoBehaviour {
  6.  
  7.     [SerializeField] private Transform pfConfetti;
  8.     [SerializeField] private Color[] colorArray;
  9.  
  10.     private List<Confetti> confettiList;
  11.     private float spawnTimer;
  12.     private const float SPAWN_TIMER_MAX = 0.033f;
  13.  
  14.     private void Awake() {
  15.         confettiList = new List<Confetti>();
  16.         SpawnConfetti();
  17.     }
  18.  
  19.     private void Update() {
  20.         foreach (Confetti confetti in new List<Confetti>(confettiList)) {
  21.             if (confetti.Update()) {
  22.                 confettiList.Remove(confetti);
  23.             }
  24.         }
  25.  
  26.         spawnTimer -= Time.deltaTime;
  27.         if (spawnTimer <= 0f) {
  28.             spawnTimer += SPAWN_TIMER_MAX;
  29.             int spawnAmount = Random.Range(1, 4);
  30.             for (int i = 0; i < spawnAmount; i++) {
  31.                 SpawnConfetti();
  32.             }
  33.         }
  34.     }
  35.  
  36.     private void SpawnConfetti() {
  37.         float width = transform.GetComponent<RectTransform>().rect.width;
  38.         float height = transform.GetComponent<RectTransform>().rect.height;
  39.         Vector2 anchoredPosition = new Vector2(Random.Range(-width / 2f, width / 2f), height / 2f);
  40.         Color color = colorArray[Random.Range(0, colorArray.Length)];
  41.         Confetti confetti = new Confetti(pfConfetti, transform, anchoredPosition, color, -height / 2f);
  42.         confettiList.Add(confetti);
  43.     }
  44.  
  45.     private class Confetti {
  46.  
  47.         private Transform transform;
  48.         private RectTransform rectTransform;
  49.         private Vector2 anchoredPosition;
  50.         private Vector3 euler;
  51.         private float eulerSpeed;
  52.         private Vector2 moveAmount;
  53.         private float minimumY;
  54.  
  55.         public Confetti(Transform prefab, Transform container, Vector2 anchoredPosition, Color color, float minimumY) {
  56.             this.anchoredPosition = anchoredPosition;
  57.             this.minimumY = minimumY;
  58.             transform = Instantiate(prefab, container);
  59.             rectTransform = transform.GetComponent<RectTransform>();
  60.             rectTransform.anchoredPosition = anchoredPosition;
  61.  
  62.             rectTransform.sizeDelta *= Random.Range(.8f, 1.2f);
  63.  
  64.             euler = new Vector3(0, 0, Random.Range(0, 360f));
  65.             eulerSpeed = Random.Range(100f, 200f);
  66.             eulerSpeed *= Random.Range(0, 2) == 0 ? 1f : -1f;
  67.             transform.localEulerAngles = euler;
  68.  
  69.             moveAmount = new Vector2(0, Random.Range(-50f, -150f));
  70.  
  71.             transform.GetComponent<Image>().color = color;
  72.         }
  73.  
  74.         public bool Update() {
  75.             anchoredPosition += moveAmount * Time.deltaTime;
  76.             rectTransform.anchoredPosition = anchoredPosition;
  77.  
  78.             euler.z += eulerSpeed * Time.deltaTime;
  79.             transform.localEulerAngles = euler;
  80.  
  81.             if (anchoredPosition.y < minimumY) {
  82.                 Destroy(transform.gameObject);
  83.                 return true;
  84.             } else {
  85.                 return false;
  86.             }
  87.         }
  88.  
  89.  
  90.     }
  91.  
  92. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×