RainSplashController.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Funly.SkyStudio
  5. {
  6. public class RainSplashController : MonoBehaviour, ISkyModule
  7. {
  8. SkyProfile m_SkyProfile;
  9. float m_TimeOfDay;
  10. List<RainSplashRenderer> m_SplashRenderers = new List<RainSplashRenderer>();
  11. private void Start()
  12. {
  13. // Verify GPU instancing is supported.
  14. if (SystemInfo.supportsInstancing == false) {
  15. Debug.LogWarning("Can't render rain splashes since GPU instancing is not supported on this platform.");
  16. enabled = false;
  17. return;
  18. }
  19. ClearSplashRenderers();
  20. }
  21. public void UpdateForTimeOfDay(SkyProfile skyProfile, float timeOfDay)
  22. {
  23. m_SkyProfile = skyProfile;
  24. m_TimeOfDay = timeOfDay;
  25. }
  26. void Update()
  27. {
  28. if (m_SkyProfile == null || m_SkyProfile.IsFeatureEnabled(ProfileFeatureKeys.RainSplashFeature) == false) {
  29. ClearSplashRenderers();
  30. return;
  31. }
  32. if (m_SkyProfile.rainSplashArtSet == null || m_SkyProfile.rainSplashArtSet.rainSplashArtItems == null ||
  33. m_SkyProfile.rainSplashArtSet.rainSplashArtItems.Count == 0) {
  34. ClearSplashRenderers();
  35. return;
  36. }
  37. if (m_SkyProfile.rainSplashArtSet.rainSplashArtItems.Count != m_SplashRenderers.Count) {
  38. ClearSplashRenderers();
  39. CreateSplashRenderers();
  40. }
  41. // Assign a style to each renderer.
  42. for (int i = 0; i < m_SkyProfile.rainSplashArtSet.rainSplashArtItems.Count; i++) {
  43. RainSplashArtItem style = m_SkyProfile.rainSplashArtSet.rainSplashArtItems[i];
  44. RainSplashRenderer r = m_SplashRenderers[i];
  45. r.UpdateForTimeOfDay(m_SkyProfile, m_TimeOfDay, style);
  46. }
  47. }
  48. public void ClearSplashRenderers()
  49. {
  50. for (int i = 0; i < this.transform.childCount; i++) {
  51. Destroy(this.transform.GetChild(i).gameObject);
  52. }
  53. m_SplashRenderers.Clear();
  54. }
  55. public void CreateSplashRenderers()
  56. {
  57. for (int i = 0; i < m_SkyProfile.rainSplashArtSet.rainSplashArtItems.Count; i++) {
  58. GameObject go = new GameObject("Rain Splash Renderer");
  59. RainSplashRenderer r = go.AddComponent<RainSplashRenderer>();
  60. r.transform.parent = this.transform;
  61. m_SplashRenderers.Add(r);
  62. }
  63. }
  64. }
  65. }