Search Unity

Question A better way to show UI icons on objects in the world?

Discussion in 'UGUI & TextMesh Pro' started by Project-NSX, Nov 26, 2022.

  1. Project-NSX

    Project-NSX

    Joined:
    Apr 11, 2019
    Posts:
    25
    Hi all,

    I've been making a small system to show UI elements on a canvas in the position of game objects in the world.
    I'm trying to make it pretty flexible and I'm just looking for general advice for improvements or rework of this system to improve flexibility and/or performance.

    Here is a script I've been putting on the game objects in the world:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class PointOfInterest : MonoBehaviour
    7. {
    8.     public GameObject pOIIconPrefab;
    9.     public Vector3 offset = new Vector3(0, 0, 0);
    10.     public bool iconSpawned;
    11.  
    12.     [HideInInspector]
    13.     public Image icon;
    14. }
    15.  
    This just holds the icon to use, an offset and if the icon has been instantiated onto the canvas or not.

    Here's a script I've placed on the player to display the points of interest icons on a canvas:
    Code (CSharp):
    1. using Cinemachine;
    2. using Cinemachine;
    3. using System;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6. using UnityEngine;
    7. using UnityEngine.UI;
    8.  
    9. public class PlayerDisplayPointsOfInterest : MonoBehaviour
    10. {
    11.     [SerializeField] Canvas canvas;
    12.     [SerializeField] Camera playerCamera;
    13.  
    14.     [SerializeField] float viewRange = 5f;
    15.     [SerializeField] float interactRange = 2f;
    16.  
    17.  
    18.  
    19.     private void Awake()
    20.     {
    21.         // Using the CameraUpdateEvent from Cinemachine to prevent elements lagging behind-
    22.         // -when vSync is on
    23.         CinemachineCore.CameraUpdatedEvent.AddListener(DrawGUI);
    24.     }
    25.  
    26.     void DrawGUI(CinemachineBrain arg0)
    27.     {
    28.         Collider[] colliderArray = Physics.OverlapSphere(transform.position, viewRange);
    29.         foreach (Collider col in colliderArray)
    30.         {
    31.             if (col.TryGetComponent(out PointOfInterest poi))
    32.             {
    33.                 if (poi.iconSpawned == false && Vector3.Distance(col.transform.position, playerCamera.transform.position) <= viewRange)
    34.                 {
    35.                     poi.icon = Instantiate(poi.pOIIconPrefab, canvas.transform).GetComponent<Image>();
    36.                     poi.icon.transform.SetAsFirstSibling();
    37.                     poi.iconSpawned = true;
    38.                 }
    39.                 else if (poi.iconSpawned == true && Vector3.Distance(col.transform.position, playerCamera.transform.position) > viewRange && poi.icon != null)
    40.                 {
    41.                     Destroy(poi.icon.gameObject);
    42.                     poi.iconSpawned = false;
    43.                     continue;
    44.                 }
    45.  
    46.                 if (poi.iconSpawned == true)
    47.                 {
    48.                     poi.icon.transform.position = playerCamera.WorldToScreenPoint(poi.transform.position + poi.offset);
    49.                 }
    50.             }
    51.         }
    52.     }
    53. }
    As you can see, this is using Cinemachine and I'm running the update method when the camera updates. This was just to solve an issue to do with the order of execution which was causing the icons to lag behind a little.

    Is this a bit excessive for an icons system? I feel like there's a lot going on and there may be a simpler and more performant way of doing it.

    These scripts are fully working however and I could just be overthinking it, so obviously feel free to use this code if you have a use for it.

    Thanks!
     
    Last edited: Nov 26, 2022
  2. Project-NSX

    Project-NSX

    Joined:
    Apr 11, 2019
    Posts:
    25
    Anyone??