| | 1 | | using System; |
| | 2 | | using DCL.Configuration; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Models; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | namespace DCL.Components |
| | 9 | | { |
| | 10 | | public class OnPointerEventColliders : IDisposable |
| | 11 | | { |
| | 12 | | public const string COLLIDER_NAME = "OnPointerEventCollider"; |
| | 13 | |
|
| | 14 | | internal MeshCollider[] colliders; |
| 53 | 15 | | Dictionary<Collider, string> colliderNames = new Dictionary<Collider, string>(); |
| | 16 | |
|
| | 17 | | public string GetMeshName(Collider collider) |
| | 18 | | { |
| 6 | 19 | | if (colliderNames.ContainsKey(collider)) |
| 3 | 20 | | return colliderNames[collider]; |
| | 21 | |
|
| 3 | 22 | | return null; |
| | 23 | | } |
| | 24 | |
|
| | 25 | | private IDCLEntity ownerEntity; |
| | 26 | |
|
| | 27 | | public void Initialize(IDCLEntity entity) |
| | 28 | | { |
| 103 | 29 | | Renderer[] rendererList = entity?.meshesInfo?.renderers; |
| | 30 | |
|
| 103 | 31 | | if (rendererList == null || rendererList.Length == 0) |
| | 32 | | { |
| 25 | 33 | | if (colliders != null && colliders.Length > 0) |
| 7 | 34 | | DestroyColliders(); |
| | 35 | |
|
| 25 | 36 | | return; |
| | 37 | | } |
| | 38 | |
|
| 78 | 39 | | if (AreCollidersCreated(rendererList)) |
| | 40 | | { |
| 21 | 41 | | UpdateCollidersWithRenderersMesh(rendererList); |
| 21 | 42 | | return; |
| | 43 | | } |
| | 44 | |
|
| 57 | 45 | | IShape shape = entity.meshesInfo.currentShape; |
| | 46 | |
|
| 57 | 47 | | ownerEntity = entity; |
| | 48 | |
|
| 57 | 49 | | DestroyColliders(); |
| | 50 | |
|
| 57 | 51 | | if (shape == null) |
| 0 | 52 | | return; |
| | 53 | |
|
| 57 | 54 | | colliders = new MeshCollider[rendererList.Length]; |
| | 55 | |
|
| 292 | 56 | | for (int i = 0; i < colliders.Length; i++) |
| | 57 | | { |
| 89 | 58 | | if (rendererList[i] == null) |
| | 59 | | continue; |
| 89 | 60 | | colliders[i] = CreateCollider(rendererList[i]); |
| | 61 | | } |
| 57 | 62 | | } |
| | 63 | |
|
| | 64 | | void UpdateCollidersWithRenderersMesh(Renderer[] rendererList) |
| | 65 | | { |
| 92 | 66 | | for (int i = 0; i < colliders.Length; i++) |
| | 67 | | { |
| 25 | 68 | | if(colliders[i].sharedMaterial == null) |
| 25 | 69 | | colliders[i].sharedMesh = rendererList[i].GetComponent<MeshFilter>().sharedMesh; |
| | 70 | | } |
| 21 | 71 | | } |
| | 72 | |
|
| | 73 | | MeshCollider CreateCollider(Renderer renderer) |
| | 74 | | { |
| 89 | 75 | | GameObject go = new GameObject(COLLIDER_NAME); |
| | 76 | |
|
| 89 | 77 | | go.name = COLLIDER_NAME; |
| 89 | 78 | | go.layer = PhysicsLayers.onPointerEventLayer; // to avoid character collisions with onclick collider |
| | 79 | |
|
| 89 | 80 | | var meshCollider = go.AddComponent<MeshCollider>(); |
| 89 | 81 | | meshCollider.sharedMesh = renderer.GetComponent<MeshFilter>().sharedMesh; |
| 89 | 82 | | meshCollider.enabled = renderer.enabled; |
| | 83 | |
|
| 89 | 84 | | if (renderer.transform.parent != null) |
| 89 | 85 | | colliderNames.Add(meshCollider, renderer.transform.parent.name); |
| | 86 | |
|
| 89 | 87 | | CollidersManager.i.AddOrUpdateEntityCollider(ownerEntity, meshCollider); |
| | 88 | |
|
| | 89 | | // Reset objects position, rotation and scale once it's been parented |
| 89 | 90 | | Transform t = go.transform; |
| | 91 | |
|
| 89 | 92 | | t.SetParent(renderer.transform); |
| 89 | 93 | | t.ResetLocalTRS(); |
| | 94 | |
|
| 89 | 95 | | return meshCollider; |
| | 96 | | } |
| | 97 | |
|
| | 98 | | private bool AreCollidersCreated(Renderer[] rendererList) |
| | 99 | | { |
| 78 | 100 | | if (colliders == null || colliders.Length == 0) |
| 57 | 101 | | return false; |
| | 102 | |
|
| 21 | 103 | | if (rendererList.Length != colliders.Length) |
| 0 | 104 | | return false; |
| | 105 | |
|
| 92 | 106 | | for (int i = 0; i < rendererList.Length; i++) |
| | 107 | | { |
| 25 | 108 | | bool foundChildCollider = false; |
| 62 | 109 | | for (int j = 0; j < colliders.Length; j++) |
| | 110 | | { |
| 31 | 111 | | if (colliders[j].transform.parent == rendererList[i].transform) |
| | 112 | | { |
| 25 | 113 | | foundChildCollider = true; |
| 25 | 114 | | break; |
| | 115 | | } |
| | 116 | | } |
| | 117 | |
|
| 25 | 118 | | if (!foundChildCollider) |
| 0 | 119 | | return false; |
| | 120 | | } |
| | 121 | |
|
| 21 | 122 | | return true; |
| | 123 | | } |
| | 124 | |
|
| 106 | 125 | | public void Dispose() { DestroyColliders(); } |
| | 126 | |
|
| | 127 | | void DestroyColliders() |
| | 128 | | { |
| 117 | 129 | | if (colliders == null) |
| 60 | 130 | | return; |
| | 131 | |
|
| 292 | 132 | | for (int i = 0; i < colliders.Length; i++) |
| | 133 | | { |
| 89 | 134 | | Collider collider = colliders[i]; |
| | 135 | |
|
| 89 | 136 | | if (collider != null) |
| 47 | 137 | | UnityEngine.Object.Destroy(collider.gameObject); |
| | 138 | | } |
| | 139 | |
|
| 57 | 140 | | colliders = null; |
| 57 | 141 | | } |
| | 142 | | } |
| | 143 | | } |