How do I make text "usable" when you click it?

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
AoF
Posts: 241
Joined: Sun May 12, 2019 8:36 pm

How do I make text "usable" when you click it?

Post by AoF »

Hello. I have a panel and some text in it. When you click on the text, I want to start some dialogue. I've already figured out how to do this with one of my characters and it works fine. I thought I could follow the same steps on some text, but for some reason, it's not working. I'm probably doing something stupid.

Here's my scene and the inspector for the canvas: I want a conversation to start when you click on the text that says "Sleep and restore energy". I've tried setting the canvas to overlay - camera and world space, but it doesn't make a difference.

Here's the inspector of the text:



I think I followed the directions here: viewtopic.php?t=1915

Here's my Selector (on my camera):

I would expect for my mouse to hover over that text and for the selector elements to show up and when I click on it for a conversation to start, but none of these things happen.

I created a debug script and can confirm that when I click on this text, its OnMouseEnter and OnMouseDown are fired. The log doesn't appear to have anything informative in it:
AoF
Posts: 241
Joined: Sun May 12, 2019 8:36 pm

Re: How do I make text "usable" when you click it?

Post by AoF »

I tried going a different route, and it almost works. I created a game object that's outside a canvas and underneath the text. In this game object, I put the Usable/Trigger/Collider, and it works as expect IF I set the canvas to Active(false). But, if the text is on the screen, the game object no longer is clickable. I'm not sure why... I've done everything I can think of to put the text behind the game object.

Image
Image
User avatar
Tony Li
Posts: 22056
Joined: Thu Jul 18, 2013 1:27 pm

Re: How do I make text "usable" when you click it?

Post by Tony Li »

What about making it a UI Button? You can point the button's OnClick() event to DialogueSystemTrigger.OnUse.
AoF
Posts: 241
Joined: Sun May 12, 2019 8:36 pm

Re: How do I make text "usable" when you click it?

Post by AoF »

That'll work, too. BTW, the solution I found to this problem is to remove this script on my canvas: Image
User avatar
Tony Li
Posts: 22056
Joined: Thu Jul 18, 2013 1:27 pm

Re: How do I make text "usable" when you click it?

Post by Tony Li »

I see. The Selector ignores mouse hovers over selectable UI elements. This makes the UI element unselectable, which means the Selector can ignore it and detect the collider underneath it.

Be careful of the way UI elements shift in different resolutions and aspect ratios. A safer variation might be to make the 2D collider GameObject into the main GameObject, and add a world space canvas as a child of it. This way the canvas will always perfectly overlay the main GameObject even if the screen size changes.
AoF
Posts: 241
Joined: Sun May 12, 2019 8:36 pm

Re: How do I make text "usable" when you click it?

Post by AoF »

In the end, I went with your button idea. Thanks!
AoF
Posts: 241
Joined: Sun May 12, 2019 8:36 pm

Re: How do I make text "usable" when you click it?

Post by AoF »

Similar situation: I have these buttons in my UI that are icons.

I think it would be a nice experience if I could put a working Usable script on them so the Usable script could function as a tool tip and give a little explanation of what will happen if you click on the icon. But just like w/ the text, if I put a Usable on a button, it doesn't have any effect. Is there a way to have a working Usable on a button?
This makes the UI element unselectable, which means the Selector can ignore it and detect the collider underneath it.
Why is that useful?
User avatar
Tony Li
Posts: 22056
Joined: Thu Jul 18, 2013 1:27 pm

Re: How do I make text "usable" when you click it?

Post by Tony Li »

AoF wrote: Tue Jun 11, 2019 10:37 pmSimilar situation: I have these buttons in my UI that are icons. Image

I think it would be a nice experience if I could put a working Usable script on them so the Usable script could function as a tool tip and give a little explanation of what will happen if you click on the icon. But just like w/ the text, if I put a Usable on a button, it doesn't have any effect. Is there a way to have a working Usable on a button?
You can use a small subclass of Selector and Usable:
SelectorAlsoUI.cs

Code: Select all

using PixelCrushers.DialogueSystem;
using UnityEngine;

public class SelectorAlsoUI : Selector
{
    public static SelectorAlsoUI instance;

    private void Awake()
    {
        instance = this;
    }

    public void Select(Usable usable)
    {
        if (usable == null || !usable.enabled)
        {
            DeselectTarget();
        }
        else
        {
            this.usable = usable;
            selection = usable.gameObject;
            OnSelectedUsableObject(usable);
        }
    }

    protected override void Update()
    {
        if (usable != null && usable.GetComponent<RectTransform>() != null)
        {
            if (IsUseButtonDown()) UseCurrentSelection();
        }
        else
        {
            base.Update();
        }
    }
}
UsableAlsoUI.cs

Code: Select all

using UnityEngine;

namespace PixelCrushers.DialogueSystem
{

    public class UsableAlsoUI : Usable
    {
        public void SelectMe()
        {
            SelectorAlsoUI.instance.Select(GetComponent<Usable>());
        }

        public void DeselectMe()
        {
            SelectorAlsoUI.instance.Select(null);
        }
    }
}
Here's an example scene: SelectorAlsoUI_2019-06-12.unitypackage

Configure your UI button like this:

Image
AoF wrote: Tue Jun 11, 2019 10:37 pm
This makes the UI element unselectable, which means the Selector can ignore it and detect the collider underneath it.
Why is that useful?
This lets the Selector detect the GameObject that's otherwise covered by the Canvas. Normally you don't want to select world objects that are covered by UIs. Take a typical RPG for example. In normal play, you can click on NPCs and other usables to interact with them. But if you've opened your inventory window and you're clicking around in it to rearrange your inventory, you don't want those clicks to also select any NPCs that are underneath the inventory window.
AoF
Posts: 241
Joined: Sun May 12, 2019 8:36 pm

Re: How do I make text "usable" when you click it?

Post by AoF »

Great, thanks a lot!
This lets the Selector detect the GameObject that's otherwise covered by the Canvas. Normally you don't want to select world objects that are covered by UIs.
Makes sense.
Post Reply