| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | namespace DCL.Skybox |
| | 7 | | { |
| | 8 | | public class SatelliteReferences |
| | 9 | | { |
| | 10 | | public GameObject satelliteParent; |
| | 11 | | public GameObject orbitGO; |
| | 12 | | public GameObject satelliteGO; |
| | 13 | | public GameObject satellitePrefab; |
| | 14 | | public SatelliteLayerBehaviour satelliteBehavior; |
| | 15 | | } |
| | 16 | |
|
| | 17 | | public class SkyboxSatelliteElements |
| | 18 | | { |
| | 19 | | const string satelliteParentResourcePath = "SkyboxPrefabs/Satellite Parent"; |
| | 20 | |
|
| | 21 | | private readonly GameObject satelliteElements; |
| | 22 | | private GameObject satelliteParentPrefab; |
| | 23 | | private FollowBehaviour followBehavior; |
| | 24 | |
|
| 0 | 25 | | Dictionary<GameObject, Queue<SatelliteReferences>> satelliteReferences = new Dictionary<GameObject, Queue<Satell |
| 0 | 26 | | List<SatelliteReferences> usedSatellites = new List<SatelliteReferences>(); |
| | 27 | |
|
| 0 | 28 | | public SkyboxSatelliteElements(GameObject satelliteElements) |
| | 29 | | { |
| 0 | 30 | | this.satelliteElements = satelliteElements; |
| 0 | 31 | | Initialize(); |
| 0 | 32 | | } |
| | 33 | |
|
| | 34 | | private void Initialize() |
| | 35 | | { |
| 0 | 36 | | if (satelliteElements == null) |
| | 37 | | { |
| 0 | 38 | | Debug.LogError("Satellite Elements Container is null"); |
| 0 | 39 | | return; |
| | 40 | | } |
| | 41 | |
|
| 0 | 42 | | followBehavior = satelliteElements.GetOrCreateComponent<FollowBehaviour>(); |
| 0 | 43 | | followBehavior.followPos = true; |
| 0 | 44 | | followBehavior.ignoreYAxis = true; |
| 0 | 45 | | } |
| | 46 | |
|
| | 47 | | internal void ApplySatelliteConfigurations(SkyboxConfiguration config, float dayTime, float normalizedDayTime, L |
| | 48 | | { |
| 0 | 49 | | ResetObjects(); |
| 0 | 50 | | List<SatelliteReferences> satelliteRefs = GetSatelliteAllActiveSatelliteRefs(config.satelliteLayers); |
| | 51 | |
|
| 0 | 52 | | if (satelliteRefs.Count != config.satelliteLayers.Count) |
| | 53 | | { |
| 0 | 54 | | Debug.LogWarning("Satellite not working!, cause prefab is not assigned"); |
| 0 | 55 | | return; |
| | 56 | | } |
| | 57 | |
|
| 0 | 58 | | for (int i = 0; i < satelliteRefs.Count; i++) |
| | 59 | | { |
| | 60 | | // If satellite is disabled, disable the 3D object too. |
| 0 | 61 | | if (!config.satelliteLayers[i].enabled) |
| | 62 | | { |
| 0 | 63 | | if (satelliteRefs[i] != null) |
| | 64 | | { |
| 0 | 65 | | satelliteRefs[i].satelliteParent.SetActive(false); |
| 0 | 66 | | satelliteRefs[i].satelliteBehavior.ChangeRenderType(LayerRenderType.NotRendering); |
| | 67 | | } |
| 0 | 68 | | continue; |
| | 69 | | } |
| | 70 | |
|
| 0 | 71 | | if (satelliteRefs[i] == null) |
| | 72 | | { |
| | 73 | | #if UNITY_EDITOR |
| 0 | 74 | | Debug.LogWarning(config.satelliteLayers[i].nameInEditor + " Satellite not working!, prefab not assig |
| | 75 | | #endif |
| 0 | 76 | | continue; |
| | 77 | | } |
| | 78 | |
|
| 0 | 79 | | satelliteRefs[i].satelliteBehavior.AssignValues(config.satelliteLayers[i], dayTime, cycleTime); |
| | 80 | | } |
| 0 | 81 | | } |
| | 82 | |
|
| | 83 | | private void ResetObjects() |
| | 84 | | { |
| 0 | 85 | | if (usedSatellites != null) |
| | 86 | | { |
| 0 | 87 | | for (int i = 0; i < usedSatellites.Count; i++) |
| | 88 | | { |
| 0 | 89 | | SatelliteReferences sat = usedSatellites[i]; |
| 0 | 90 | | if (sat == null) |
| | 91 | | { |
| | 92 | | continue; |
| | 93 | | } |
| 0 | 94 | | sat.satelliteParent.SetActive(false); |
| 0 | 95 | | satelliteReferences[sat.satellitePrefab].Enqueue(sat); |
| | 96 | | } |
| 0 | 97 | | usedSatellites.Clear(); |
| | 98 | | } |
| 0 | 99 | | } |
| | 100 | |
|
| | 101 | | private List<SatelliteReferences> GetSatelliteAllActiveSatelliteRefs(List<Config3DSatellite> satelliteLayers) |
| | 102 | | { |
| 0 | 103 | | for (int i = 0; i < satelliteLayers.Count; i++) |
| | 104 | | { |
| 0 | 105 | | GetSatelliteObject(satelliteLayers[i]); |
| | 106 | | } |
| 0 | 107 | | return usedSatellites; |
| | 108 | | } |
| | 109 | |
|
| | 110 | | private SatelliteReferences GetSatelliteObject(Config3DSatellite config) |
| | 111 | | { |
| 0 | 112 | | SatelliteReferences tempSatellite = null; |
| | 113 | |
|
| 0 | 114 | | if (config.satellite == null) |
| | 115 | | { |
| 0 | 116 | | usedSatellites.Add(tempSatellite); |
| 0 | 117 | | return tempSatellite; |
| | 118 | | } |
| | 119 | |
|
| | 120 | | // Check if GO for this prefab is already in scene, else create new |
| 0 | 121 | | if (satelliteReferences.ContainsKey(config.satellite)) |
| | 122 | | { |
| | 123 | | // Check if there is any unused GO for the given prefab |
| 0 | 124 | | if (satelliteReferences[config.satellite].Count > 0) |
| | 125 | | { |
| 0 | 126 | | tempSatellite = satelliteReferences[config.satellite].Dequeue(); |
| 0 | 127 | | } |
| | 128 | | else |
| | 129 | | { |
| 0 | 130 | | tempSatellite = InstantiateNewSatelliteReference(config); |
| | 131 | | } |
| 0 | 132 | | } |
| | 133 | | else |
| | 134 | | { |
| 0 | 135 | | satelliteReferences.Add(config.satellite, new Queue<SatelliteReferences>()); |
| 0 | 136 | | tempSatellite = InstantiateNewSatelliteReference(config); |
| | 137 | | } |
| | 138 | |
|
| 0 | 139 | | usedSatellites.Add(tempSatellite); |
| 0 | 140 | | tempSatellite.satelliteParent.SetActive(true); |
| | 141 | |
|
| 0 | 142 | | return tempSatellite; |
| | 143 | | } |
| | 144 | |
|
| | 145 | | private SatelliteReferences InstantiateNewSatelliteReference(Config3DSatellite config) |
| | 146 | | { |
| 0 | 147 | | if (satelliteParentPrefab == null) |
| | 148 | | { |
| 0 | 149 | | satelliteParentPrefab = Resources.Load<GameObject>(satelliteParentResourcePath); |
| | 150 | | } |
| | 151 | |
|
| 0 | 152 | | GameObject obj = GameObject.Instantiate<GameObject>(satelliteParentPrefab); |
| 0 | 153 | | obj.layer = LayerMask.NameToLayer("Skybox"); |
| 0 | 154 | | obj.name = "Satellite Parent"; |
| 0 | 155 | | obj.transform.parent = satelliteElements.transform; |
| 0 | 156 | | obj.transform.localPosition = Vector3.zero; |
| | 157 | |
|
| 0 | 158 | | GameObject orbit = obj.transform.GetChild(0).gameObject; |
| 0 | 159 | | GameObject satelliteObj = GameObject.Instantiate(config.satellite); |
| 0 | 160 | | satelliteObj.transform.parent = obj.transform; |
| | 161 | |
|
| | 162 | | // Get satellite behavior and assign satellite |
| 0 | 163 | | SatelliteLayerBehaviour satelliteBehavior = obj.GetComponent<SatelliteLayerBehaviour>(); |
| 0 | 164 | | satelliteBehavior.satellite = satelliteObj; |
| | 165 | |
|
| 0 | 166 | | SatelliteReferences satellite = new SatelliteReferences(); |
| 0 | 167 | | satellite.satelliteParent = obj; |
| 0 | 168 | | satellite.orbitGO = orbit; |
| 0 | 169 | | satellite.satelliteGO = satelliteObj; |
| 0 | 170 | | satellite.satellitePrefab = config.satellite; |
| 0 | 171 | | satellite.satelliteBehavior = satelliteBehavior; |
| | 172 | |
|
| 0 | 173 | | return satellite; |
| | 174 | | } |
| | 175 | |
|
| | 176 | | public void ResolveCameraDependency(Transform currentTransform) |
| | 177 | | { |
| 0 | 178 | | if (followBehavior != null) |
| | 179 | | { |
| 0 | 180 | | followBehavior.target = currentTransform.gameObject; |
| | 181 | | } |
| 0 | 182 | | } |
| | 183 | | } |
| | 184 | | } |