Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Word · Mar 19, 2017 at 12:26 PM · guiobjectsclickuser interfacepopup

Open a popup window with text and images when clicking on an object - please help!

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Word · Mar 28, 2017 at 07:09 AM

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

Answer by Dacke · Oct 19, 2019 at 05:47 PM

I made it on this way: alt text

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:

  1. Right click on PopupMessage > UI > Panel - This is overlay (transparent on picture below)

  2. Right click on Panel > UI > Canvas and add Canvas and rename it to Frame. You can insert here image and color it in green.

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

  4. Right click on PopupMessage > UI > RawImage (This is white space in picture below, and it will be added through code)

  5. Right click on PopupMessage > UI > Text (You can write here what ever you want, because it will be changed through code)

  6. 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).

alt text

And you can call this on every other script:

 popupMessage.Open("NameOfTexture","This is some text");

Before call, declare it:

 PopupMessage popupMessage;
 GameObject gameController;
 
 // Use this for initialization
     void Start () {
         gameController = GameObject.Find("GameController");
         popupMessage = gameController.GetComponent<PopupMessage> ();
     }



001.png (326.4 kB)
002.png (86.6 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Word · Mar 28, 2017 at 07:16 AM

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dvandamme · Mar 28, 2017 at 07:28 AM 0
Share

follow this 3 part tut by unity... its gold, this will have you doing a bunch of stuff like what you describe with a lot of power. its for 4.6, so the UI system is similar enough, unlike anything prior

https://unity3d.com/learn/tutorials/modules/intermediate/live-training-archive/modal-window?playlist=17111 https://unity3d.com/learn/tutorials/modules/intermediate/live-training-archive/modal-window-pt2?playlist=17111 https://unity3d.com/learn/tutorials/modules/intermediate/live-training-archive/modal-window-pt3?playlist=17111

avatar image Word dvandamme · Mar 28, 2017 at 12:21 PM 0
Share

Thank you very much, I'll do that! :-)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

gui.button down 2 Answers

Popup Window from an EditorWindow? 1 Answer

Passing values between scripts 5 Answers

Create new GUI TextArea on click 1 Answer

Ignore GUI Clicks in Game 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges