Improved code

 avatar
user_8400650
csharp
2 years ago
3.4 kB
1
Indexable
Never
    
    
    private GameObject _cachedPlayer;
    
    /// <summary>
    /// Refreshes the position and size of the nicknames on the UI
    /// </summary>
    public void RefreshUI()
    {
        // Caching
        var cam = CameraGO.instance.transform;
        _camPos = cam.position;
        _camForward = cam.forward;
        
        var controllers = GameGO.instance.controllerGOs;

        for (int i = 0; i < controllers.Count; i++)
        {
            // if you are certain the GOs aren't going to disappear mid loop you could revert this (not sure I trust the original approach)
            // not sure what the class is... used a dummy class
            if(controllers[i] is not CachedGOClass _cachedCtrlrGO){
                continue;
            }
            // Caching
            //_cachedCtrlrGO = controllers[i];

            
            // Consider caching player GO and not doing redundant calls every update
            if (_cachedCtrlrGO == _cachedPlayer)
            {
                continue;
            }
            
            // I assume you are trying to not draw 'same team' to UI?
            // What is the second check for? Seems like it would hide everything that is active...
            // And third check is to hide whatever is the target of the camera... so the player? If so, I think having the player register with the manager/singleton controller group/etc is a cleaner approach
            if (_cachedCtrlrGO.team == LocalClient.team || _cachedCtrlrGO.gameObject.activeSelf || _cachedCtrlrGO == CameraGO.instance.targetGO)
            {
                HideBothUIElements();
                continue;
            }

            // Player nickname is not in the camera's FoV
            // save yourself the calculation cost of normalization -- not needed
            if (Vector3.Dot((_cachedCtrlrGO.nicknameHolder.position - _camPos), _camForward) <= 0)
            {
                HideBothUIElements();
                continue;
            }

            // Determinating which UI element is used to represent the player (Either full text nickname or simple colored dot)
            if (_cachedCtrlrGO.distToCam > FULL_NICKNAME_VISIBILITY_DISTANCE)
            {
                _activeUIelement = _cachedCtrlrGO.nickname_image.gameObject;
                otherUIelement = _cachedCtrlrGO.nicknameUI_text.gameObject;
            }
            else
            {
                _activeUIelement = _cachedCtrlrGO.nicknameUI_text.gameObject;
                otherUIelement = _cachedCtrlrGO.nickname_image.gameObject;
            }

            // Updating the UI element position
            _activeUIelement.transform.position = Vector2.Lerp(_activeUIelement.transform.position, _cam.WorldToScreenPoint(_cachedCtrlrGO.nicknameHolder.position), 100);

            // Enabling the active UI element, disabling the other
            if (!_activeUIelement.activeSelf) _activeUIelement.SetActive(true);
            if (otherUIelement.activeSelf) otherUIelement.SetActive(false);
        }
    }

    void HideBothUIElements()
    {
        // Hiding all UI elements if they were being shown
        if (_cachedCtrlrGO.nickname_image.gameObject.activeSelf) _cachedCtrlrGO.nickname_image.gameObject.SetActive(false);
        if (_cachedCtrlrGO.nicknameUI_text.gameObject.activeSelf) _cachedCtrlrGO.nicknameUI_text.gameObject.SetActive(false);
    }