Improved code
user_8400650
csharp
3 years ago
3.1 kB
7
Indexable
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;
}
var tooFarForNickname = _cachedCtrlrGO.distToCam > FULL_NICKNAME_VISIBILITY_DISTANCE;
_cachedCtrlrGO.nickname_image.gameObject.setActive(tooFarForNickname)
_cachedCtrlrGO.nicknameUI_text.gameObject.setActive(tooFarForNickname)
// Updating the UI element position
// why lerp with a constant value?
// and why is constant greater than the clamped value range? [0-1]
//_activeUIelement.transform.position = Vector2.Lerp(_activeUIelement.transform.position, _cam.WorldToScreenPoint(_cachedCtrlrGO.nicknameHolder.position), 100);
_activeUIelement.transform.position = _cam.WorldToScreenPoint(_cachedCtrlrGO.nicknameHolder.position);
}
}
// Don't even waste time with the branching checks -- set to false and move along
void HideBothUIElements()
{
_cachedCtrlrGO.nickname_image.gameObject.SetActive(false);
_cachedCtrlrGO.nicknameUI_text.gameObject.SetActive(false);
}Editor is loading...