Search Unity

Pop Up Info

Discussion in 'Scripting' started by Undiscovered, Jul 2, 2017.

  1. Undiscovered

    Undiscovered

    Joined:
    Apr 5, 2014
    Posts:
    53
    Hi all

    I'm relatively new to Unity and game engines as my background only really consists of 3ds Max.

    I am currently working on a college project where i've created an interior visualisation in 3ds Max and imported to Unity. What I would love to do next is allow the player to click on an object (e.g a door) and a little graphic will pop up with information about the door such as door size colour etc.

    I have no idea whether all of this is something that would have to be scripted or whether there is an asset available on the store which would help.

    Hope that all makes sense.

    Thanks in advance.
     
  2. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    387
    You could have a button on the door that has a script on it. In that script you could have a method that instantiate a pop up (guessing you would have this as a prefab?).

    This method can then be called by using the on click event in the Inspector of the button
     
  3. Vipsu

    Vipsu

    Joined:
    Oct 8, 2012
    Posts:
    88
    Showing simple pop-ups isn't that difficult in Unity but finding the best way could take quite a bit of time whitout previous knowledge about Unity and it's UI.

    Wrote couple simple scripts for you to achieve this fairly easily.
    Not the prettiest solution but should get you going I think.

    Attach this to the GameObject you want to click to display its data.
    Make sure it has 3D collider attached to it and that your Main Camera has Physics Raycaster 3D attached to it.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3.  
    4. public class ShowDetailsOnClick : MonoBehaviour, IPointerClickHandler
    5. {
    6.     public string ID = "NoName";
    7.     public Color color = Color.white;
    8.     public Vector3 size = Vector3.one;
    9.  
    10.     //If you want to get the values from the attached GameObject.
    11.     public bool GetValuesFromGameObject = false;
    12.  
    13.     public void Awake()
    14.     {
    15.         if(GetValuesFromGameObject)
    16.             FetchValues();
    17.     }
    18.  
    19.     public void FetchValues()
    20.     {
    21.         MeshRenderer mr = GetComponent<MeshRenderer>();
    22.         if (mr == null)
    23.             mr = GetComponentInChildren<MeshRenderer>();
    24.  
    25.         if(mr)
    26.             color = mr.material.color;
    27.  
    28.         ID = gameObject.name;
    29.         size = transform.lossyScale;
    30.     }
    31.  
    32.     public void OnPointerClick(PointerEventData eventData)
    33.     {
    34.         if(InformationBox.current != null)
    35.             InformationBox.current.Show(ID, color, size);
    36.     }
    37. }
    Then create a new Panel (right click -> UI/Panel or from top -> GameObject/UI/Panel) and Unity should automatically create you Canvas with Panel in it for you to modify. You can Rename the canvas as InfoPopUp and then set it to use center anchor presets. You can resize the panel the way you want so that it won't take the whole screen now.

    Then add 3 new Text objects to the panel (right click InfoPopUp -> UI/Text ) and a button. Place them the way you want to the panel and name them to something more fitting like for this chase IDLabel, ColorLabel, SizeLabel and the button could be named CloseButton.

    Attach this to the InfoPopUp panel and drag the text elements and button element to the script on their correct places.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class InformationBox : MonoBehaviour
    5. {
    6.     public static InformationBox current;
    7.  
    8.     public Text NameText;
    9.     public Text SizeText;
    10.     public Text ColorText;
    11.     public Button CloseButton;
    12.  
    13.     public void Awake()
    14.     {
    15.         current = this;
    16.         CloseButton.onClick.AddListener(Hide);
    17.         Hide();
    18.     }
    19.  
    20.     public void Show(string name, Color color, Vector3 size)
    21.     {
    22.         NameText.text = name;
    23.         ColorText.text = ColorUtility.ToHtmlStringRGBA(color);
    24.         SizeText.text = size.ToString();
    25.      
    26.         //Alternate way with more control
    27.         //SizeText.text = "x = " + size.x + " y = " + size.y + " z = " + size.z;
    28.  
    29.         gameObject.SetActive(true);
    30.     }
    31.  
    32.     public void Hide()
    33.     {
    34.         gameObject.SetActive(false);
    35.     }
    36. }
    37.  
    Now the GameObjects with ShowDetailsOnClick attached should display the InformationBox when clicked.

    GetValuesFromGameObject option is mainly there to show you how you could fetch information from the GameObject itself if you don't want to input everything manually.
     
    Last edited: Jul 2, 2017
    Undiscovered likes this.
  4. Undiscovered

    Undiscovered

    Joined:
    Apr 5, 2014
    Posts:
    53
    Many thanks for your detailed response. Sorry for the late reply I have been away on vacation. Will test out the scripts above and report back. Thanks again