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

Show more comments
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. !

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Enable UI Button from Prefab 1 Answer

Having trouble with cloning with prefabs 1 Answer

prefab instantiated by script containing ShadowCaster2D bugs out 0 Answers

UI Text won't appear at all. 1 Answer

How to detect button presses and change child object text in the button's Parent Object script 0 Answers


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