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 /
This question was closed Aug 30, 2017 at 08:53 PM by Ianamus for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Ianamus · Aug 30, 2017 at 01:25 AM · canvasuser interfaceoverlay

Can't get UI Canvas to overlay the scene

I am creating a UI canvas via script and am trying to create a side bar that will overlay the screen during gameplay, but when I set the canvas render mode to "Screen Space - Overlay" it does not overlay the scene at all, and simply sticks the UI in the world space. The "Screen Space - Camera" render mode works correctly, but when using this mode I encountered a lot of clipping issues that the Overlay mode would prevent if I can get it to work.

Here is the code I use to create the UI canvas and the side bar:

         //  Create the canvas.
         canvas = new GameObject("Battle UI Canvas", typeof(RectTransform));
         canvas.transform.position = new Vector3(canvas.transform.position.x, canvas.transform.position.y, canvas.transform.position.z - 1.0f);
         canvas.AddComponent<Canvas>();
         canvas.GetComponent<Canvas>().renderMode = RenderMode.ScreenSpaceOverlay;
         canvas.GetComponent<Canvas>().worldCamera = Camera.main;
         canvas.GetComponent<Canvas>().planeDistance = 5.2f;
         canvas.AddComponent<CanvasScaler>();
         canvas.GetComponent<CanvasScaler>().uiScaleMode = UnityEngine.UI.CanvasScaler.ScaleMode.ScaleWithScreenSize;
         canvas.GetComponent<CanvasScaler>().referenceResolution = new Vector2(Screen.width, Screen.height);
         canvas.GetComponent<CanvasScaler>().matchWidthOrHeight = 0.5f;
         canvas.AddComponent<GraphicRaycaster>();
 
         //  Create the side bar
         int sideBoxHeight = (int)(Screen.height * 1.5);
         int sideBoxWidth = (int)(Screen.width * 0.170);
 
         GameObject sideBoxLeft = new GameObject("UI Box Left");
         SpriteRenderer boxLeft = sideBoxLeft.AddComponent<SpriteRenderer>();
 
         var mTexture = new Texture2D(sideBoxWidth, sideBoxHeight);
         Color[] myColours = new Color[sideBoxWidth * sideBoxHeight];
         for (int j = 0; j < (sideBoxWidth * sideBoxHeight); j++) { myColours[j] = Color.gray; }
 
         mTexture.SetPixels(myColours);
         mTexture.Apply();
 
         Sprite mSprite = Sprite.Create(mTexture, new Rect(0.0f, 0.0f, mTexture.width, mTexture.height), new Vector2(0.5f, 0.5f), 100.0f);
         boxLeft.sprite = mSprite;
         boxLeft.transform.localScale += new Vector3(69.0f, 69.0f, 69.0f);
        
         sideBoxLeft.AddComponent<RectTransform>();
         sideBoxLeft.GetComponent<RectTransform>().anchoredPosition = new Vector2(-(Screen.width / 2.0f) + (sideBoxWidth / 3.0f), 0);
         sideBoxLeft.transform.SetParent(canvas.GetComponent<Canvas>().transform, false);

And here is the result:

alt text

Instead of overlaying the scene the side bar is created in the same plane as the game objects and is massive. I don't understand why this is happening, as it displays correctly when the render mode is set to "Screen Space - Camera".

If anyone can point out what I am doing wrong I would really appreciate it!

unity-ui-issue.png (342.8 kB)
Comment
Add comment
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

2 Replies

  • Sort: 
avatar image
0

Answer by hexagonius · Aug 30, 2017 at 02:46 AM

It is world space which means it's following the rules of depth testing. If you pull the canvas up a little it will look fine. Right now it's at the exact same height as the other grey sprite. That's called z-fighting, it's not clear what's closer to the camera. Either use separate camera that render your UI and the grey rectangle one after the other, or make sure they're on differing heights.

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 Ianamus · Aug 30, 2017 at 09:13 AM 0
Share

It shouldn't be world space, I deliberately set it it to the "Screen Space - Overlay" render mode because I want it to act as an overlay. You can see in the image and code that this is the mode it is set to.

If I simply increase the height of the canvas the bar is still massive, in the wrong place and at the wrong angle. It's supposed to be a thin grey bar on the left hand side of the screen (and it is, if I set the render mode to "Screen Space - Camera").

But I don't want the UI to exist in the world space at all because of clipping issues with other game objects when the camera is zoomed in. I want it to function as an overlay.

avatar image hexagonius Ianamus · Aug 30, 2017 at 08:03 PM 0
Share

So you're saying UI Box left is the grey rectangle that's rendered from the camera? Could you turn the camera off and tell what's visible then? A screen space overlay must work without any camera whatsoever

avatar image
0

Answer by Ianamus · Aug 30, 2017 at 08:52 PM

Right, I've found the issue. The UI elements I'm trying to add to the canvas are using sprite renderers, which aren't supposed to be used for UI elements. Changing them to Image instead fixes this.

Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

72 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

Related Questions

Screen Space - Camera to Screen Space - Overlay switch issue 0 Answers

Why use different Canvas Render Modes 0 Answers

Scaling panel by different screen sizes 1 Answer

Universal Method of finding pixel dimensions of UI elements 1 Answer

How to scale according to different mobile resolutions? 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