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 cdunne1 · Jul 18, 2017 at 11:53 AM · 2duiprefabpause menu

Prefab a 2D UI?

Hi Unity people,

I've done some digging and am still confused....

I want to effectively prefab my Pause Screen UI across a number of scenes to ensure consistency and optimise project efficiency. Basically, when the player pauses, a semi-transparent white image should appear with three buttons (play, restart and exit) with the appropriate "on click" functions.

Is it possible to prefab the Pause Screen/menu UI? If so, how do you go about doing this?

Thanks in advance :)

Comment
Add comment · Show 16
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 cdunne1 · Jul 18, 2017 at 02:52 PM 0
Share

I had a canvas (created automatically a la Unity) created but can delete and extract just the actual elements,

Any idea as to why its cloning in the hierarchy but not showing up in the scene/game view?

Thanks so much"!

avatar image agarcialeon cdunne1 · Jul 18, 2017 at 03:08 PM 0
Share

I don't have enough visual information about your setup but be sure of:

  1. Drag the elements to the project view to convert them in prefabs.

  2. In a script load the instatiate the prefab as a gameobject and set the gameobject as child of your existant canvas to be able to render it correctly.

avatar image cdunne1 agarcialeon · Jul 18, 2017 at 04:00 PM 0
Share

Okay.. still a bit confused on point 2. How do I set the gameobject (which I presume is the prefab) as a child of the existant canvas? I thought that once the Pause Screen canvas was set up and converetd to a prefab it could be deleted from the hierarchy, in which case, the prefab cannot be made a child of a non-existant object (if you get me).

In the inspector, I have attached a test prefab of a simple red image to test and it's not working. I think it's to do with the Vector3 positions. I'm not sure if I need them for this case (they are included in the code you kindly linked https://docs.unity3d.com/$$anonymous$$anual/InstantiatingPrefabs.html)

Thought I had posted the code - here it is (I'm using Csharp). I have commented out the dummy versions I tested with Vector3 positions - they weren't working either.

public GameObject PauseScreenPrefab;

private float prefabX = 0; //private variables used to try simulate the code linked at https://docs.unity3d.com/$$anonymous$$anual/InstantiatingPrefabs.html private float prefabY = 0;

 void PausePlay()
 {
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.P))            //p button as pause input
     {
         Debug.Log("just pressed p");
         if (Time.timeScale == 1)
         {
             Debug.Log("game is paused");
             Time.timeScale = 0;                
             showPaused();
             }
         else if (Time.timeScale == 0)
         {
             Debug.Log("game in play");
             Time.timeScale = 1;
         }
     }
 } 

 public void showPaused()                            
 {
     /*for (int y = 0; y < 1; y++)
     {
         for (int x = 0; x < 1; x++)
         {*/

     Debug.Log("about to instatiate prefab");                     //DOES NOT APPEAR IN LOG
     Instantiate(PauseScreenPrefab, new Vector3(prefabX, prefabY), Quaternion.identity);
     Debug.Log("instatiated prefab");                  //THIS APPEARS

     /*    }
     }*/
 }
avatar image agarcialeon · Jul 18, 2017 at 04:45 PM 0
Share

This is the code you need in your method when you call the method to make appear your pause menu.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Pause$$anonymous$$enu : $$anonymous$$onoBehaviour
 {
     // Use this for initialization
     void Start ()
     {
         
     }
     
     // Update is called once per frame
     void Update ()
     {
         
     }
 
     public void ShowPause$$anonymous$$enu()
     {
         GameObject prefab = Instantiate(Resources.Load<GameObject>("Pause$$anonymous$$enu")); //Load the prefab
         prefab.transform.SetParent(this.transform, false); //Current gameobject where the script is attached
     }
 }

The prefab you can do it this way: Video

avatar image cdunne1 agarcialeon · Jul 18, 2017 at 05:08 PM 0
Share

Thanks - that link led to a dead link and I am almost more confused now than before.

  • Do I still declare a public GameObject prefab at the beginning (before start) I dont know whether you omitted that for breivity or because of the resources.load code

  • What's the value of resources as opposed to the standard prefab set up? I presume the item in inverted commas after needs to be the exact name of the prefab object in the resources folder

  • Can I do this without the REsources piece and just Instantiate(prefab.transform.SetParent(this.transform, false) ins$$anonymous$$d?

  • Don't get the SetParent reference in the code or its relevance... is this something to do with the canvas?

I do really appreciate your help - bear with me. I am a newcomer to Unity and ask an waful lot of questions. !

avatar image agarcialeon cdunne1 · Jul 18, 2017 at 05:34 PM 0
Share

Sorry, I forgot that deleting the link deletes the video and excuse if I'm confusing you more than neccesary. I have little time using this forums to help people and also my first language is not english.

I hope with the next info the problem solves: First, you don't need to declare any public gameobject. You only create a prefab in your project.

Second, I'm using Resources folder because it's the easiest way to show you how to load a prefab into your script code you can create any of them that you want and modify them later. Of course you can do it with other folders, but as in Unity, it's kind of a standard I just recomend you to use it.

About the name of the prefab, you're correct, you reference it by the name (you can use which you want).

The main point of this is: - Create a prefab object with the contents you want. - In the scenes you want to add the prefab, just add a script in your canvas (since you have one already created). And in that script add a function that does the 2 lines in the method I wrote before.

The first line loads a gameobject so you can have a reference to it. BUT, this is the important thing, it's that if you want to add a prefab to a gameobject you have to use the method Instantiate. The second line is also important because Unity rendering order taking into account in the hierarchy so if you don make the prefab child of the canvas you won't be able to see it.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by agarcialeon · Jul 18, 2017 at 12:17 PM

Of course you can, any of the gameobjects in the hierarchy can be converted into a prefab to instantiate it later when you want.

The simplest way to do this is select your menu gameobject (if it's inside a canvas select the canvas, if not just the ui elements you want to reuse) and drag them into your project view.

Then, when you want to instantiate it in another screen you can do use this in any of your scripts to add it to your hierarchy again and use it's functionality.

Hope it helps.

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 CoryButler · Jul 18, 2017 at 02:11 PM 1
Share

I'm not sure you would want to select the canvas as part of the prefab. I have always added UI prefabs, which do not include a canvas, to a canvas already existing in my scene. In my experience, this has allow for consistent usability of all my UI prefabs.

Of course canvases can be included in a prefab. I just want to note that it is a good idea to plan how you want to implement them up front.

avatar image agarcialeon CoryButler · Jul 18, 2017 at 02:32 PM 0
Share

Yeah, well I just was taking into account both cases if in your case you hadn't any canvas created previously but yes, It's a better approach to only create prefabs from UI components without the canvas.

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

163 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Having trouble with cloning with prefabs 1 Answer

Enable UI Button from Prefab 1 Answer

Disable GameObject through Prefab? 2 Answers

positioning ui buttons in a scrollview 1 Answer

[4.6 2D] Making UI image stick to the side 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