Skip to content

Controller HUD

Ad edited this page Jul 26, 2022 · 11 revisions

Overview

The Controller HUD is a system spanning across multiple scripts. I will describe here how each of them relates to each other and explain how you can leverage it to expose in-game object parameters without having to create the UI for it.

This system solves the problem of not having to create individual HUD panels for each interactable GameObject, like stars, for which you want to expose the parameters to the user. Hence, this system will dynamically generate a HUD panel next to the controller as the user interacts with different objects. The HUD panel will be filled from basic UI elements for which there are premade assets called Prefabs under Resources/HUD.

Implementation Details

Under the Scripts/HUD folder you will find:

  • OcaInteractable.cs
  • OcaControllerHUD.cs
  • ElementBlueprint.cs
  • GuiElementDescriptor.cs

Oca* prefixed scripts derive from MonoBehaviour, meaning they are meant to be attached to GameObjects. However the OcaInteractable script is special and is not meant to be attached to a GameObject, but rather derived from in your own script instead of deriving from MonoBehaviour (take a look at FastRotator.cs as an example). This class acts as a wrapper around the MonoBehaviour to provide provide critical elements for the system to work. It exposes a HUDElements List and a reference to the OcaControllerHUD from the controller that will be holding the GameObject.

The List<HUDElement> HUDElements List is of special interest here, since this is the list that must the populated with the parameters to be exposed.

Example

public class AwesomeStar : OcaInteractable
{
 // Public fields we want to expose
 public String name;
 [Range(.1f, 3f)] public float brighness;
 public float radius;
 public float a;
 public bool myToggle;
 ...

 void Start()
 {
  ...
  
  this.HUDElements.Add(new HUDElement(ElementType.Label, "name"));
  this.HUDElements.Add(new HUDElement(ElementType.Slider, "brightness", "Brightness"));
  this.HUDElements.Add(new HUDElement(ElementType.Slider, "radius", "Radius"));
  this.HUDElements.Add(new HUDElement(ElementType.Slider, "a", "Some Parameter"));
  this.HUDElements.Add(new HUDElement(ElementType.Slider, "myToggle", "Some Toggle"));


 }
}

Element Blueprints

Each HUD element has a name, a prefab and can have an initialization and update functions. The purpose of this class is to act as a data container for HUD elements that can be instantiated from this class, hence the 'Blueprint' name. Each HUD element must be initialized with values if there are any, and for those that are interactable like Sliders and Toggles, an update function must be provided.

Hence, each element 'can' implement initialization and update functions to be used by the ControllerHUD system.

Adding new Element Blueprints

  • First make sure you create a prefab for your new element under Resources/HUD.[image]

  • Inside OcaControllerHUD load your new prefab as shown

  • Then add an enum type to ElementType inside the HUDElement.cs script.

  • Lastly, make a new entry in elementBlueprints list as shown in the screenshot.

If your element has values, provide an initialization function. If your element has values than the user can interact with then also provide an update function. Otherwise set to null.

Now you can use that element like in the example of the section above.

Clone this wiki locally