Making selectable objects - mariusz-tang/KTaNE-Module-Template GitHub Wiki

To make selectable objects, such as buttons, you will need to use KMSelectable and KMHighlightable components.

Make it selectable

In Unity, add a KMSelectable component to the object you would like to make selectable, and a KMHighlightable component to the object you would like to be the highlight of this selectable—this is usually a slightly larger copy of the original object. Set the Highlight Scale field of the highlight to 1, 1, 1, and give it the HighlightOverlay material so you can see how it will look.

image

Tip: The highlight is only visible from the inside. This means that if it overlaps with anything, such as the module background, it will not be visible. If you cannot see the bottom of the highlight, you may need to raise it up until there is no overlap.

Remove the highlight's MeshRenderer component once you are happy with it.

Set the Parent field of the selectable to the KMSelectable component of the module, and set the Highlight field to the KMHighlightable component of the highlight. Add the selectable to the Children field of the module's KMSelectable.

Now, when you press Play, the highlight should show up when you mouse over it.

selectable

Make it do something

To have the button actually do something on interaction, you need to subscribe to the KMSelectable's OnInteract and/or OnInteractEnded events, as shown.

[SerializeField] private KMSelectable _button;

private void Awake() {
    _button.OnInteract += DoSomething;
    _button.OnInteractEnded += DoSomethingElse;
}

private bool DoSomething() {
    Debug.Log("Press!");
    return false;
}
private void DoSomethingElse() => Debug.Log("Release!");

The handler for the OnInteract event requires a bool return type. Return true if this selecting this object should give access to its child KMSelectables; otherwise, return false. For buttons, you'll usually want to return false, as above.