RingsManager.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. public class RingsManager : MonoBehaviour
  4. {
  5. public Transform ringsAnchor;
  6. public PoolObject ringPrefab;
  7. const float SPACING_BETWEEN_RINGS = 475;
  8. List<PoolObject> _placedRings = new List<PoolObject>();
  9. void OnDisable()
  10. {
  11. foreach (PoolObject ring in _placedRings)
  12. {
  13. ring.Despawn();
  14. }
  15. _placedRings.Clear();
  16. }
  17. public void CreateUpTo(float endX)
  18. {
  19. //if the player is switching between global and facebook, we don't need to recreate the rings
  20. if (_placedRings.Count != 0)
  21. {
  22. return;
  23. }
  24. Pool pool = Pool.Get(ringPrefab);
  25. int numOfRings = (int)(endX / SPACING_BETWEEN_RINGS);
  26. for (int i = 0; i < numOfRings; i++)
  27. {
  28. PoolObject ring = pool.Spawn();
  29. _placedRings.Add(ring);
  30. /*if(ring.transform!=null){
  31. return;
  32. }*/
  33. Transform ringTransform = ring.transform;
  34. Vector3 originalScale = ringTransform.localScale;
  35. ringTransform.parent = ringsAnchor;
  36. ringTransform.localScale = originalScale;
  37. //The anchor is set to the right of the screen, and we go to the left
  38. ringTransform.localPosition = new Vector3(-i * SPACING_BETWEEN_RINGS, 0);
  39. float percentage = i / (float)numOfRings;
  40. ring.GetComponent<Ring>().SetAlphaFromPercentage(percentage);
  41. }
  42. }
  43. }