This needs a little more explaining, because the headline makes it sound easier than it is. I searched the forums, the documentation and UA for this but didn’t quite find a solution for the new UI. Of course, I’m already happy if somebody points me into the right directions.
I have a few 3D objects that already have a specific tag. I want to assign each of them a script that basically references a few images and some description text, so if one of these objects is being clicked on in the game, then the detailed text and the images which belong to it appear in a standardized popup window. A simple image gallery and the text below or above it would probably work best. However, each of these objects is supposed to have a different kind of description and different images. I figured out how to put an individual canvas together, but I don’t know how to create a generic one where everything is interchangeable and depending on a script assigned to each of the objects of interest, each with different images and different text (at least, that’s how I imagine it might work best). How should I proceed?
First make a a UI canvas (Right click on scene hierarchy windows, in my case Level 001, Game Object > UI > Canvas, and give the name of canvas PopupMessage. This will automatically make for you EventSystem game object.
Next steps:
Right click on PopupMessage > UI > Panel - This is overlay (transparent on picture below)
Right click on Panel > UI > Canvas and add Canvas and rename it to Frame. You can insert here image and color it in green.
Right click on Panel > UI > Canvas and add Canvas and rename it to Popup. You can insert here image and color it in light green.
Right click on PopupMessage > UI > RawImage (This is white space in picture below, and it will be added through code)
Right click on PopupMessage > UI > Text (You can write here what ever you want, because it will be changed through code)
Right click on PopupMessage > UI > Button (Rename to OK)
When you make popup like on image below or like you want, disable PopupMessage game object (it will be enabled on some action).
Make class PopupMessage.cs and copy all code from example:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PopupMessage : MonoBehaviour {
public GameObject ui;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void Open(string inventoryStuffName, string message){
ui.SetActive (!ui.activeSelf);
if (ui.activeSelf) {
if(!string.IsNullOrEmpty(inventoryStuffName)){
var texture = TakeInvenotryCollecition (inventoryStuffName);
RawImage rawImage = ui.gameObject.GetComponentInChildren<RawImage>();
rawImage.texture = texture;
}
if (!string.IsNullOrEmpty (message)) {
Text textObject = ui.gameObject.GetComponentInChildren<Text> ();
textObject.text = message;
}
Time.timeScale = 0f;
}
}
public void Close(){
ui.SetActive (!ui.activeSelf);
if (!ui.activeSelf) {
Time.timeScale = 1f;
}
}
//You need to have Folder Resources/InvenotryItems
public Texture TakeInvenotryCollecition(string LoadCollectionsToInventory)
{
Texture loadedGO = Resources.Load("InvenotryItems/"+LoadCollectionsToInventory, typeof(Texture)) as Texture;
return loadedGO;
}
}
Make empty gameobject and name it GameController and put PopupMessage.cs script into GameController game object. There will be ui public GameObject property and drag PopupMessage to that place (picture below First part).
Now, go back to PopupMessage GameObject and on Button add click method. Drag and drop GameController, and choose metod PopupMessage.Close. (picture below Secound part).
This is what I tried by combining a few scripts I found:
using UnityEngine;
using System.Collections;
public class MenuAppearScript : MonoBehaviour {
public GameObject TheObjectTheMenuBelongsTo;
public GameObject menu;
private bool isShowing;
void Update() {
if (Input.GetMouseButtonDown(0)) {
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.TheObjectTheMenuBelongsTo(true))
{
if (menu.SetActive(true) {
menu.SetActive(false);
} else if (menu.SetActive(false))
{
menu.SetActive(true);
}
}
}
isShowing = !isShowing;
menu.SetActive(isShowing);
}
}
}
It doesn’t work yet. Could someone please help me with this? I also want to close the menu with a right-click, but I guess I can do that part myself.
For the record, I think I solved it myself. I found a script and edited it a little to fit my needs:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class asdf : MonoBehaviour
{
public GameObject abc;//Its your button
// Use this for initialization
void Start()
{
}
// Update is called once per frame
private void OnMouseDown()
{
if (Input.GetMouseButtonDown(0))
{
Debug.Log("Pressed left click.");
abc.SetActive(true);
}
}
public void Update ()
{
if (Input.GetMouseButtonDown(1))
{
abc.SetActive(false);
}
}
}
The only small problem that remains is how do I prevent Unity from showing more than one Canvas at a time?