| | 1 | | using System.Collections.Generic; |
| | 2 | | using UnityEngine; |
| | 3 | |
|
| | 4 | | namespace DCL |
| | 5 | | { |
| | 6 | | /// <summary> |
| | 7 | | /// This class handles the avatar's visibility. Different callers will determine whether they want the avatar to be |
| | 8 | | /// The avatar will be rendered only when all callers want the avatar to be visible. It only takes one caller to mak |
| | 9 | | /// </summary> |
| | 10 | | public class AvatarVisibility : MonoBehaviour |
| | 11 | | { |
| | 12 | |
|
| | 13 | | [Tooltip("Game objects that should be hidden/shown")] |
| | 14 | | public GameObject[] gameObjectsToToggle; |
| | 15 | | // A list of all the callers that want to hide the avatar |
| 1289 | 16 | | private readonly HashSet<string> callsToHide = new HashSet<string>(); |
| | 17 | |
|
| | 18 | | public event System.Action<bool> OnSetVisibility; |
| | 19 | |
|
| | 20 | | public void SetVisibility(string callerId, bool visibility) |
| | 21 | | { |
| 4 | 22 | | if (visibility) |
| | 23 | | { |
| 2 | 24 | | bool removed = callsToHide.Remove(callerId); |
| 2 | 25 | | if (removed && callsToHide.Count == 0) |
| | 26 | | { |
| | 27 | | // Show |
| 1 | 28 | | SetVisibilityForGameObjects(true); |
| | 29 | | } |
| 1 | 30 | | } |
| | 31 | | else |
| | 32 | | { |
| 2 | 33 | | bool added = callsToHide.Add(callerId); |
| 2 | 34 | | if (added) |
| | 35 | | { |
| | 36 | | // Hide |
| 2 | 37 | | SetVisibilityForGameObjects(false); |
| | 38 | | } |
| | 39 | | } |
| 4 | 40 | | OnSetVisibility?.Invoke(visibility); |
| 0 | 41 | | } |
| | 42 | |
|
| | 43 | | internal void SetVisibilityForGameObjects(bool value) |
| | 44 | | { |
| 24 | 45 | | foreach (GameObject gameObject in gameObjectsToToggle) |
| | 46 | | { |
| 7 | 47 | | if (gameObject == null) |
| | 48 | | continue; |
| | 49 | |
|
| 5 | 50 | | gameObject.SetActive(value); |
| | 51 | | } |
| 5 | 52 | | } |
| | 53 | | } |
| | 54 | | } |