Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Open / close Canvas window with onclick?

Discussion in 'Editor & General Support' started by conan222, Feb 5, 2019.

  1. conan222

    conan222

    Joined:
    Dec 15, 2018
    Posts:
    26
    Hi,

    Apologies as I first posted this in the wrong forum.

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

    Within 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.

    I can make the canvas and contents no problem but can someone please let me know if it's possible to have a script that will trigger this event to open and close it? 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. Perhaps the canvas itself may need an X on the top right that you can click on to close?

    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 System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using VRStandardAssets.Utils;
    6.  
    7. namespace ZenvaVR
    8. {
    9.  
    10.     [RequireComponent(typeof(VRInteractiveItem))]
    11.     public class VRCalloutController : MonoBehaviour
    12.     {
    13.  
    14.         // canvas that will be shown/hidden
    15.         public Canvas canvas;
    16.  
    17.         // is it showing by default?
    18.         public bool isInitiallyVisible = false;
    19.  
    20.         // activate when hovering the reticle over?
    21.         public bool isHoverActivated = false;
    22.  
    23.         // activate when clicking?
    24.         public bool isClickActivated = false;
    25.  
    26.         // vr interactive item component
    27.         VRInteractiveItem vrInteractive;
    28.  
    29.         void Awake()
    30.         {
    31.             //get the component
    32.             vrInteractive = GetComponent<VRInteractiveItem>();
    33.  
    34.             //is it initially visible
    35.             canvas.enabled = isInitiallyVisible;
    36.         }
    37.  
    38.         void OnEnable()
    39.         {
    40.             //hook on to hovering events
    41.             if (isHoverActivated)
    42.             {
    43.                 vrInteractive.OnOver += ShowCanvas;
    44.                 vrInteractive.OnOut += HideCanvas;
    45.             }
    46.  
    47.             //hook on to the click event
    48.             if (isClickActivated)
    49.             {
    50.                 vrInteractive.OnClick += ToggleCanvas;
    51.             }
    52.         }
    53.  
    54.         void OnDisable()
    55.         {
    56.             //remove hook for hovering events
    57.             if (isHoverActivated)
    58.             {
    59.                 vrInteractive.OnOver -= ShowCanvas;
    60.                 vrInteractive.OnOut -= HideCanvas;
    61.             }
    62.  
    63.             //remove hook for the click event
    64.             if (isClickActivated)
    65.             {
    66.                 vrInteractive.OnClick -= ToggleCanvas;
    67.             }
    68.         }
    69.  
    70.         void ToggleCanvas()
    71.         {
    72.             canvas.enabled = !canvas.enabled;
    73.         }
    74.  
    75.         private void HideCanvas()
    76.         {
    77.             canvas.enabled = false;
    78.         }
    79.  
    80.         private void ShowCanvas()
    81.         {
    82.             canvas.enabled = true;
    83.         }
    84.  
    85.     }
    86. }
    87.  

    I'm pretty new to Unity and steep learning curve but really enjoying it all.

    Thanks for your time.

    Cheers,

    Andrew
     
  2. sstachiw

    sstachiw

    Joined:
    Jan 5, 2019
    Posts:
    13
    I would create public game objects to store each of your canvases in. Then create unique names for each of your hot spot game objects. Then create a screen cast to ray and use the hit.transform.name to pull the name and call a function that activates that particular object.

    Just make sure you have a box collider on anything you want to hit. The below code is rough from memory but should work once cleaned up.


    //create public game objects and drag your canvas to them in the editor
    public GameObject canvas1;
    public GameObject canvas2;
    private string hitname;

    void Update (){

    //if the left mouse button is clicked...
    if(input.GetMouseButtonDown(0)){
    //create a ray variable and store the point where the mouse is when it is clicked...
    Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition));
    //create a variable called Hit of the type RaycastHit
    RaycastHit Hit;
    //now if that ray did hit something...
    if (Physics.Raycast(ray, out Hit))
    {
    //store the name of the item that was hit in the variable called hitname, then run the function hitcheck

    hitname = Hit.transform.name;
    hitcheck();

    }
    }
    }

    //now create a public function to turn on the canvas

    public void hitcheck(){

    switch (hitname){
    case "canvas1":
    canvas1.SetActive(true);
    break;

    case "canvas2":
    canvas2.SetActive(true);
    break;

    default:
    break;
    }





    }
     
  3. sstachiw

    sstachiw

    Joined:
    Jan 5, 2019
    Posts:
    13
    And, if you want the same object to deactive the canvas, just use this change to the hitcheck function:

    public void hitcheck(){

    switch (hitname){
    case "canvas1":
    canvas1.SetActive(!canvas1.activeSelf);
    break;

    case "canvas2":
    canvas2.SetActive(!canvas2.activeSelf);
    break;

    default:
    break;
    }





    }
     
  4. conan222

    conan222

    Joined:
    Dec 15, 2018
    Posts:
    26
    Wow... thanks very much for that detailed replied. Appreciate it :)

    I'll study it carefully and see if I can adapt it to the clicking of the VR hand pointer as the rest of the tour uses it. Apologies as I didn't explain that correctly in my post.


    Cheers,

    Andrew