Toggling Dialogue UI Raycasts

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
HughDMann
Posts: 8
Joined: Fri Feb 08, 2019 10:25 am

Toggling Dialogue UI Raycasts

Post by HughDMann »

Hi,

Is there a way to toggle the raycast target on the Dialogue System UI elements? Primarily for Images and Text UI?

The reason that I want this is I have a game that uses both the mouse to interact with various things, and WASD or controller movement controls, but, when the mouse is hovering over UI with raycast target elements, it will interfere with the movement controls. I.E. The Player cannot move while the mouse is over UI that has raycast targets, or, if the Player is already moving, they will get stuck moving and cannot stop.

Therefore, I would like to be able to choose when the raycast target is happening for Dialogue images and text. If for instance, the Player walks through a trigger box and I have a dialogue come up with only 1 option, this is basically an exposition scenario where I would set 'SetTimeOut(4)' or something to automatically cycle through the dialogue, and the Player doesn't really need to select an option, so the raycast of the UI isn't needed. But if there was a situation where the Player response choice was important, than I'd turn the raycasts back on.

Since the dialogue canvas appears to get duplicated at Start() I was having trouble setting direct references to the panel game objects, so I tried to set up a tag system and do something like this:

Code: Select all

    
    public void DialogueRaycastOff()
    {
        GameObject[] DialogueImagePanels = GameObject.FindGameObjectsWithTag("DialogueImagePanel");
            foreach (GameObject Panel in DialogueImagePanels)
            {
                Panel.GetComponent<Image>().raycastTarget = false;
                Debug.Log("Dialogue Image Panels " + Panel.name);
            }
 
        GameObject[] DialogueTextPanels = GameObject.FindGameObjectsWithTag("DialogueTextPanel");

            foreach (GameObject Panel in DialogueTextPanels)
            {
                Panel.GetComponent<Text>().raycastTarget = false;
                Debug.Log("Dialogue Text Panels " + Panel.name);
            }
    }

    public void DialogueRaycastOn()
    {
        GameObject[] DialogueImagePanels = GameObject.FindGameObjectsWithTag("DialogueImagePanel");
        foreach (GameObject Panel in DialogueImagePanels)
        {
            Panel.GetComponent<Image>().raycastTarget = true;
        }

        GameObject[] DialogueTextPanels = GameObject.FindGameObjectsWithTag("DialogueTextPanel");
        foreach (GameObject Panel in DialogueTextPanels)
        {
            Panel.GetComponent<Text>().raycastTarget = true;
        }
    }
But I think because FindGameObjectsWithTag only returns objects currently active in the hierarchy, I wasn't having luck getting all of the needed UI elements and toggling the raycast targets on the Image/Text components.

Is there a simpler approach or any kind of built in way to do this?

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

Re: Toggling Dialogue UI Raycasts

Post by Tony Li »

The dialogue UI is only instantiated as a copy if a prefab asset is assigned the Dialogue Manager's Dialogue UI field.

You can add an instance of the prefab directly to the Dialogue Manager's Canvas and assign that instance to the Dialogue UI field instead.

To turn off the Dialogue Manager's Canvas as a raycast target, you only need to disable its Graphic Raycaster component.

An easy way to set it up is:

1. Set the Dialogue Manager Canvas's Graphic Raycaster disabled.

2. Add a Dialogue System Events component to the Dialogue Manager. Configure it like this:
graphicRaycaster.png
graphicRaycaster.png (59.86 KiB) Viewed 640 times

When a conversation starts, the Dialogue System Events will enable the Dialogue Manager Canvas's Graphic Raycaster. When the conversation ends, it will disable it.
HughDMann
Posts: 8
Joined: Fri Feb 08, 2019 10:25 am

Re: Toggling Dialogue UI Raycasts

Post by HughDMann »

Ok, cool, that's nice and easy.

I switched the dialogue manager over to the instanced version and things got much easier from there, problem solved.

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

Re: Toggling Dialogue UI Raycasts

Post by Tony Li »

Happy to help!
Post Reply