Search Unity

How to toggle Canvas info window visibility with onclick?

Discussion in 'AR/VR (XR) Discussion' started by conan222, Feb 4, 2019.

  1. conan222

    conan222

    Joined:
    Dec 15, 2018
    Posts:
    26
    Hi,

    I've made up a 360 degree virtual tour in Unity (worldspace) which works great and has 5 scenes. I can go back and forth to each scene no problem.

    With each scene I have around 3-4 things I would like to show more details on so the idea is to have an info icon and when you click it with the pointer it opens up a canvas with text / image contents. Click the info icon again and the canvas closes.

    Can someone please let me know if it's possible to have a script that will trigger this event? Also something that's universal and works with all the info icon / canvas groups otherwise there would have to be a script for every icon.

    I did find something similar in a tutorial but it uses gaze where as I just want it to work when the pointer clicks on the info icon.


    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. namespace VRStandardAssets.Utils
    5. {
    6.     // This class should be added to any gameobject in the scene
    7.     // that should react to input based on the user's gaze.
    8.     // It contains events that can be subscribed to by classes that
    9.     // need to know about input specifics to this gameobject.
    10.     public class VRInteractiveItem : MonoBehaviour
    11.     {
    12.         public event Action OnOver;             // Called when the gaze moves over this object
    13.         public event Action OnOut;              // Called when the gaze leaves this object
    14.         public event Action OnClick;            // Called when click input is detected whilst the gaze is over this object.
    15.         public event Action OnDoubleClick;      // Called when double click input is detected whilst the gaze is over this object.
    16.         public event Action OnUp;               // Called when Fire1 is released whilst the gaze is over this object.
    17.         public event Action OnDown;             // Called when Fire1 is pressed whilst the gaze is over this object.
    18.  
    19.  
    20.         protected bool m_IsOver;
    21.  
    22.  
    23.         public bool IsOver
    24.         {
    25.             get { return m_IsOver; }              // Is the gaze currently over this object?
    26.         }
    27.  
    28.  
    29.         // The below functions are called by the VREyeRaycaster when the appropriate input is detected.
    30.         // They in turn call the appropriate events should they have subscribers.
    31.         public void Over()
    32.         {
    33.             m_IsOver = true;
    34.  
    35.             if (OnOver != null)
    36.                 OnOver();
    37.         }
    38.  
    39.  
    40.         public void Out()
    41.         {
    42.             m_IsOver = false;
    43.  
    44.             if (OnOut != null)
    45.                 OnOut();
    46.         }
    47.  
    48.  
    49.         public void Click()
    50.         {
    51.             if (OnClick != null)
    52.                 OnClick();
    53.         }
    54.  
    55.  
    56.         public void DoubleClick()
    57.         {
    58.             if (OnDoubleClick != null)
    59.                 OnDoubleClick();
    60.         }
    61.  
    62.  
    63.         public void Up()
    64.         {
    65.             if (OnUp != null)
    66.                 OnUp();
    67.         }
    68.  
    69.  
    70.         public void Down()
    71.         {
    72.             if (OnDown != null)
    73.                 OnDown();
    74.         }
    75.     }
    76. }
    I'm very new to Unity but really enjoying it all.

    Thanks for your time.

    Cheers,

    Andrew