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 meta-ben · Sep 10, 2018 at 02:11 PM · uicanvasrendertexture

Tutorial dark overlay with cutouts

I'm trying to build a tutorial system that can darken the screen, except for one or more areas that I want to highlight to the user. Similar to this: https://imgur.com/gallery/ElHa1Im


I have a couple of requirements that make this non-trivial:

  1. There can be more than one cutout area at a time

  2. The cutout areas must follow any scaling or positioning of the UI elements they're attached to

  3. Cutout areas shouldn't be simple rectangles, they must allow for rounded edges or circles


My current approach is to use a second camera that renders to a RenderTexture. That camera has a clear color of black with 50% alpha. A separate Screen Space Camera canvas renders to that camera. The cutouts are UI Images on that canvas, which are opaque where I want the cutout to be and are rendered on that camera with a blend mode of Blend Zero One, Zero OneMinusSrcAlpha.


So my hierarchy looks like this:

  - Main Camera
  - Tutorial Camera (renders to RenderTexture)
  - Tutorial Render Canvas (SSC, renders to TutorialCamera)
     - Cutout Image
  - Main Canvas (SSC, renders to Main Camera)
     - Main UI Elements
     - Tutorial Overlay (uses RenderTexture)


Where I'm struggling is how to align them with the game UI on the main canvas. It works fine when I copy/paste them in the Unity editor, but they lose all positioning when I try to Instantiate them via code. So what I'm looking for is either a way to reliably reproduce any positioning and scaling of the first canvas hierarchy onto the second canvas, or a simpler solution for what I'm trying to achieve here.


Update: I figured it out. It is a bit complicated though, so if anyone knows of a simpler solution, please enlighten me.

I setup the scene like above. The Tutorial RenderTexture Canvas needs to have the same CanvasScaler settings as the main canvas. I also add an additional Tutorial Display Canvas that renders to the main camera, but that's on a separate sorting layer to ensure it is drawn on top of all the main UI.

Then each of the cutouts is an Image plus a custom Script. The script Instantiates the cutout under the RenderTexture Canvas, and on each Update adapts the position and size like this:

 var rt = cutout.GetComponent<RectTransform>();
 var srcRT = GetComponent<RectTransform>();
 
 rt.position = srcRT.position;
 rt.SetSizeWithCurrentAnchors( RectTransform.Axis.Horizontal, srcRT.rect.size.x );

In addition to that, I also need another script that creates the RenderTexture at runtime, because it needs to have the same resolution as the main camera.

It's a bit hacky, and it doesn't work with masks, but other than that it seems to work.

Comment
Add comment · Show 3
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 misher · Sep 10, 2018 at 02:48 PM 0
Share

For only one element, you can create dynamically a background image (black semitransparent) and put it behind the desired elemet, set dimentions to be huge enough to cover the rest of the screen (you can even scale it), put LayoutElement on background and toggle on ignore layout (to not break any Layout you may have)

avatar image meta-ben misher · Sep 10, 2018 at 02:57 PM 0
Share

That won't work because there are cases where I want to highlight only a part of an element, or a group of elements (e.g. a table where I want to highlight one row). Also there will be cases where I need to cut out two different regions of the screen.

avatar image misher meta-ben · Sep 10, 2018 at 03:15 PM 0
Share

you can go with secondary camera and render the portion of the screen (or all screen) to a texture, then apply those textures to dynamically created images in some new overlay canvas... too much work imo and may be not as performant as you might want(rendering textures is a part of Unity API and can't be offloaded to the background thread, so you may have some freezes).

0 Replies

· Add your reply
  • Sort: 

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

160 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

Related Questions

Weird Image Flickering Behavior on UI Canvas When Using RT Material 1 Answer

Display canvas on in game monitor 1 Answer

Use the canvas scaler to resize UI on a RenderTexture's resolution ? 0 Answers

UI interaction via RenderTexture 0 Answers

Using a Canvas with a Render Texture 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