Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Unity Cookbook - Fifth Edition

You're reading from  Unity Cookbook - Fifth Edition

Product type Book
Published in Nov 2023
Publisher Packt
ISBN-13 9781805123026
Pages 780 pages
Edition 5th Edition
Languages
Authors (3):
Matt Smith Matt Smith
Shaun Ferns Shaun Ferns
Profile icon Shaun Ferns
Sinéad Murphy Sinéad Murphy
Profile icon Sinéad Murphy
View More author details

Table of Contents (22) Chapters

Preface 1. Displaying Data with Core UI Elements 2. Responding to User Events for Interactive UIs 3. Inventory and Advanced UIs 4. Playing and Manipulating Sounds 5. Textures, Materials, and 3D Objects 6. Creating 3D Environments with Terrains 7. Creating 3D Geometry with ProBuilder 8. 2D Animation and Physics 9. Animated Characters 10. Saving and Loading Data 11. Controlling and Choosing Positions 12. Navigation Meshes and Agents 13. Cameras, Lighting, and Visual Effects 14. Shader Graphs and Video Players 15. Particle Systems and Other Visual Effects 16. Mobile Games and Applications 17. Augmented Reality (AR) 18. Virtual and Extended Reality (VR/XR) 19. Advanced Topics – Gizmos, Automated Testing, and More 20. Other Books You May Enjoy
21. Index

Creating a UI Button to reveal an image

In this recipe, we’ll create a button that, when pressed, will make an image appear.

Getting ready

For this recipe, we have prepared the image that you need in a folder named Images in the 02_01 folder.

How to do it...

To create a UI Button to reveal an image, follow these steps:

  1. Create a new Unity 2D project.
  2. Import the unity_logo image into the Project folder.
  3. Drag the unity_logo image into the scene and in the Inspector panel, set the scale property to X 2 Y 2, and uncheck the check box as per the screenshot to make this GameObject inactive. An inactive GameObject is not displayed, and nor does it have any active behavior (so no scripts run and it does not respond to any event messages).
  4. Load the TextMeshPro Essential Resources, by choosing Window | TextMeshPro | Import TMP Essential Resources.
A screenshot of a computer

Description automatically generated with medium confidence

Figure 2.3: Inspector properties for an image in the scene

  1. In the Hierarchy window, right-click and select Create | UI | Button-TextMeshPro.
  2. Select GameObject Button in the Hierarchy window and click on the plus (+) button at the bottom of the Inspector window, to create a new OnClick event handler.
  3. Drag the unity_logo image from the Hierarchy window over the Object slot immediately below the menu that says Runtime Only.
  4. Select the SetActive (bool) method from the GameObject drop-down list (initially showing No Function) and click on the checkbox.
A screenshot of a computer

Description automatically generated with medium confidence

Figure 2.4: Settings for the OnClick event handler for the button

  1. Save your changes and run the scene. When you click the button, the image will appear.

How it works...

In this recipe, you created a new scene, imported an image, and unchecked the Visible in Runtime checkbox for the image so that it would not be seen at runtime.

You added a UI Button and a new OnClick event action that executes the GameObject.SetActive() method of the GameObject drop-down list of the button, and you checked the box so that the image (unity_logo) appears when the button is clicked.

There’s more...

As an alternative to having the image appear when clicking on a button, the same button could be used to make an image disappear by the checkbox on the image.

You have been reading a chapter from
Unity Cookbook - Fifth Edition
Published in: Nov 2023 Publisher: Packt ISBN-13: 9781805123026
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at ₹800/month. Cancel anytime}