Global Variables to easy access UCC components

Haytam95

Active member
Here's an simple class that i have created to access quickly to the UCC components of the player. It's not anything fancy, just a help class.

C#:
/// <summary>
/// Proporciona fácil acceso a variables, gameObjects y componentes globales que otros scripts podrían
/// necesitar.
///
/// Así como también, posee algunas funciones Utils para agilizar ciertas tareas.
/// </summary>
public class GlobalVariables : MonoBehaviour
{
    private static GlobalVariables _instance;
    private GameObject _playerCharacter;
    private UltimateCharacterLocomotion _playerUltimateCharacterLocomotion;
    private Camera _playerCamera;
    private CameraController _playerCameraController;
    private AudioListener _playerAudioListener;
    private Health _playerHealth;
    private Animator _animator;
        
    public Animator Animator
    {
        get
        {
            if (_animator == null)
            {
                _animator = PlayerCharacterGameObject.GetComponent<Animator>();
            }
            return _animator;
        }
    }


    public Health PlayerHealth
    {
        get
        {
            if (_playerHealth == null)
            {
                _playerHealth = PlayerCharacterGameObject.GetComponent<Health>();
            }
            return _playerHealth;
        }
    }

    /// <summary>
    /// Permite obtener la instancia de forma estática de éste componente, para así poder acceder a sus métodos.
    /// </summary>
    public static GlobalVariables Instance
    {
        get => _instance;
    }


    /// <summary>
    /// Permite obtener el capsule collider del personaje que el jugador está controlando.
    /// </summary>
    public Collider PlayerCapsuleCollider
    {
        get
        {
            CapsuleColliderPositioner ccp = PlayerCharacterGameObject.GetComponentInChildren<CapsuleColliderPositioner>();
            return ccp.gameObject.GetComponent<CapsuleCollider>();
        }
    }

    /// <summary>
    /// Permite obtener el Gameobject del personaje que el jugador está controlando.
    /// </summary>
    public GameObject PlayerCharacterGameObject
    {
        get
        {
            if (_playerCharacter == null)
            {
                CameraController cm = PlayerCameraController;
                _playerCharacter = cm.Character;
            }

            return _playerCharacter;
        }
    }

    /// <summary>
    /// Permite obtener el componente UltimateCharacterLocomotion del personaje que el jugador está controlando.
    /// </summary>
    public UltimateCharacterLocomotion PlayerUltimateCharacterLocomotion
    {
        get
        {
            if (_playerUltimateCharacterLocomotion == null)
            {
                GameObject character = PlayerCharacterGameObject;
                _playerUltimateCharacterLocomotion = character.GetComponent<UltimateCharacterLocomotion>();
            }

            return _playerUltimateCharacterLocomotion;
        }
    }

    /// <summary>
    /// Permite obtener el componente Camera del personaje que el jugador está controlando.
    /// </summary>
    public Camera PlayerCamera
    {
        get
        {
            if (_playerCamera == null)
            {
                var cm = PlayerCameraController;
                _playerCamera = cm.gameObject.GetComponent<Camera>();
            }

            return _playerCamera;
        }
    }

    /// <summary>
    /// Permite obtener el componente CameraController del personaje que el jugador está controlando.
    /// </summary>
    public CameraController PlayerCameraController
    {
        get
        {
            if (_playerCameraController == null)
            {
                _playerCameraController = FindObjectOfType<CameraController>();
            }

            return _playerCameraController;
        }
    }

    /// <summary>
    /// Devuelve el audio listener del personaje que el jugador está controlando
    /// </summary>
    public AudioListener PlayerAudioListener
    {
        get
        {
            if (_playerAudioListener == null)
            {
                _playerAudioListener = PlayerCamera.gameObject.GetComponent<AudioListener>();
            }

            return _playerAudioListener;
        }
    }
    
    private void Awake()
    {
        if (_instance == null)
        {
            _instance = this;
        }
    }
}

Ok, so... Why would you use this? And how to use it?

It provides a shortcut to access the most common components in the controller. Let's say you are working on a Health Monitor, it allows you to easy access to your player health by simply writting:

C#:
GlobalVariables.Instance.PlayerHealth.Value

And the result is always cached. (Global Variable script should be in a Empty gameobject in order to work)

Instead of

- Associate a gameobject in the Unity inspector
-
C#:
Health _health = gameObject.GetComponent<Health>();

_health.Value

What it provides?

- CameraController
- Player Gameobject
- Player Animator
- Health component
- Player capsule collider
- Player UCC
- Look source camera
- Player Audio listener

You can apply the same logic to have a shortcut to others components.

It's designed to work with only one player (It retrieves the player by detecting which character is being followed by the Camera Controller, so if there is more than one Camera Controller it won't work correctly).

Some examples:

C#:
// Retrieve ability
GlobalVariables.Instance.PlayerUltimateCharacterLocomotion.GetAbility<Interact>();

// Get player position
GlobalVariables.Instance.PlayerCharacterGameObject.transform.position

// Get & Change view type
ViewType viewType = GlobalVariables.Instance.PlayerCameraController.GetViewType<Combat>();
if (viewType == null) return;
GlobalVariables.Instance.PlayerCameraController.SetViewType(viewType.GetType(), false);

// Check if the player collide with a collider

private void OnTriggerEnter(Collider col)
{
   if (col != GlobalVariables.Instance.PlayerCapsuleCollider) return; // He wasn't
  
   // Yes he was, perform actions
}

Hope you find it useful, and let me know if i can improve it more!
 
Top