Home > Mobile >  How can I open a canvas by clicking on an object in Unity
How can I open a canvas by clicking on an object in Unity

Time:12-05

As you can see in the first image, I have a store, which has many objects. My intention is to click on an object and a Canvas with the object's information is displayed, but I don't know how to do it. I created a canvas example (as you can see in the second image) that I open by pressing the "space", but I want to open it by clicking on a vase (gameobject), any ideas? Greetings.

Images:

Objects

Canvas piece information

CodePudding user response:

Step 1: Get a reference to your canvas

[SerializeField] private GameObject canvas;

Step 2: Enable canvas onClick

void TaskOnClick() {
    canvas.SetActive(true);
}

Make sure that the canvas is disabled by default.

CodePudding user response:

Add IPointerClickHandler interface to your script and use the method OnPointerClick to call a method that turns on your canvas GameObject. You will need a reference for your canvas, you can set it in the inspector after adding defining it in the script as shown in the code below. (You need to attach the script to the item game object)

public class ItemInfo : MonoBehaviour, IPointerClickHandler
{

    [SerializeField] private GameObject itemInfoCanvas;
    
    public void ShowItemInfo()
    {
        itemInfoCanvas.SetActive(true);
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if (eventData.button == PointerEventData.InputButton.Right) 
          ShowItemInfo();
    }
}

Another way to achieve it with the old input system:

public class ItemInfo : MonoBehaviour
{

    [SerializeField] private GameObject itemInfoCanvas;
    
    public void ShowItemInfo()
    {
        itemInfoCanvas.SetActive(true);
    }

    private void Update()
    {
        if (Input.GetMouseButtonUp(0)) //Right click, triggered after releasing.
          ShowItemInfo();
    }
}

Same way as above with the new input system:

    private void Update()
    {
        if (!Mouse.current.leftButton.wasReleasedThisFrame)
        {
           ShowItemInfo();
        }


Read more about the pointer click handlers here

CodePudding user response:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class CameraInteraction : MonoBehaviour
{
    private new Transform camera;

    private float rayDistance = 2;
    public Canvas PieceInformationCanvas;
    public Canvas MinimapCanvas;
    public Text titleText, descriptionText;
    private GameObject test;

    void Start()
    {
        camera = transform.Find("Camera");
        PieceInformationCanvas.enabled = false;
        test = PieceInformationCanvas.transform.GetChild(2).gameObject;
        test.SetActive(false);
    }


    void Update()
    {
        // Parametros:
        // 1) Desde donde iniciara el rayo
        // 2) Hacia donde se dirige el rayo (hacia adelante(eje x))
        // 3) Color del rayo
        Debug.DrawRay(camera.position, camera.forward * rayDistance, Color.red);
        if (Input.GetButtonDown("Interactable"))
        {
            // RaycastHit hit contiene la informacion del objeto que estamos mirando
            RaycastHit hit;
            if (Physics.Raycast(camera.position, camera.forward, out hit, rayDistance, LayerMask.GetMask("Interactable")))
            {
                hit.transform.GetComponent<Interactable>().Interact();
                ShowPieceCanvas();
            }
        }

    }
    public void ShowPieceCanvas()
    {
        string title = "Titulo Pieza Artesanal";
        string description = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
        titleText.text = title;
        descriptionText.text = description;
        test.SetActive(true);
        Cursor.lockState = CursorLockMode.None;
        PieceInformationCanvas.enabled = true;
        MinimapCanvas.enabled = false;
    }
    public void HidePieceCanvas()
    {

        test.SetActive(false);
        PieceInformationCanvas.enabled = false;
        Cursor.lockState = CursorLockMode.Locked;
        MinimapCanvas.enabled = true;
    }
}  
  • Related