Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Spikeh · Jan 20, 2015 at 02:38 PM · uiguilayersscreenspace

Procedurally generated uGUI elements render under world space objects

Got to the point where I'm banging my head against a wall now, so here goes!

I have a uGUI canvas prefab with a non-trivial hierarchy:

alt text

HackingUI_DeviceCanvas is the object in question. This canvas is loaded (as a prefab) using Instantiate() when the game starts, and I assign the relevant camera to it:

 private void CreateHackingDeviceDetailsUI(Camera cam) {
     // Create the UI overlay
     var hackingUIDeviceDetailsGO = Instantiate(HackingUIDeviceDetailsCanvasPrefab) as GameObject;
     if (hackingUIDeviceDetailsGO == null) {
         Debug.LogError("Unable to instantiate the hacking UI device details UI canvas");
         return;
     }

     _hackingUIDeviceDetailsCanvas = hackingUIDeviceDetailsGO.GetComponent<Canvas>();
     _hackingUIDeviceDetailsCanvas.worldCamera = cam;

     // Get all UI panel references
     _pnlCamera = GetChildGameObjectByName(_hackingUIDeviceDetailsCanvas.gameObject, "pnlCameraView");
     _pnlRelayRules = GetChildGameObjectByName(_hackingUIDeviceDetailsCanvas.gameObject, "pnlRelayRules");
     _pnlRelayRuleList = _pnlRelayRules.transform.Find("pnlRuleList").Find("pnlContent") as RectTransform;

     var deviceNamePanel = GetChildGameObjectByName(_hackingUIDeviceDetailsCanvas.gameObject, "pnlDeviceName");
     if (deviceNamePanel != null) {
         _txtDeviceName = deviceNamePanel.GetComponentInChildren<Text>();
     }
     var deviceStatePanel = GetChildGameObjectByName(_hackingUIDeviceDetailsCanvas.gameObject, "pnlDeviceState");
     if (deviceStatePanel != null) {
         _txtDeviceState = deviceStatePanel.GetComponentInChildren<Text>();
     }
     var deviceSecurityRatingPanel = GetChildGameObjectByName(_hackingUIDeviceDetailsCanvas.gameObject, "pnlSecurityRating");
     if (deviceSecurityRatingPanel != null) {
         _txtDeviceSecurityRating = deviceSecurityRatingPanel.transform.Find("txtSecurityRating").GetComponent<Text>();
     }

     // Disable the canvas / some panels by default
     _hackingUIDeviceDetailsCanvas.enabled = false;
     ActivateCameraPanel(false);
     _pnlRelayRules.SetActive(false);
 }

This all works beautifully. However, when I try to add another prefab (instantiated in the same way), and child it to the _pnlRelayRules object above, it renders behind the camera's world space objects. Code (the prefab is instantiated in the for loop):

 protected void OnMessage_ShowRelayRules(CyberspaceUIDevice device) {
     var rules = device.RelatedNetworkDevice.NetworkDeviceRelayRules;
     if (rules.Length <= 0) {
         return;
     }

     foreach (var rule in rules) {
         var ruleListItem = Instantiate(HackingUIRelayRuleListItemPrefab) as GameObject;
         ruleListItem.transform.localScale = Vector3.one;
         ruleListItem.transform.localRotation = Quaternion.identity;
         ruleListItem.transform.SetAsLastSibling();

         var controller = ruleListItem.GetComponent<pnlRuleController>();
         // TODO: Set values

         // Save to the panel list
         ruleListItem.transform.SetParent(_pnlRelayRuleList, false);
     }

     _pnlRelayRules.SetActive(true);
 }

Rendered camera:

alt text

You can see the other (3D) menu that is rendered to the camera (which is overlayed and resized on the game's main camera) overlays ONLY the instantiated child. The hierarchy that is created with the above code is as follows:

alt text

If I create pnlRule in the editor, things work perfectly fine - but when I add it programatically, it renders like this. I have messed about with layering and hierarchy ordering, as well as adjusting / removing / adding various layout components to test with, to no avail.

Can anyone point me in the right direction?

UPDATE

I have since found that changing the second camera's view to orthographic mode sorts the problem. The problem I have here, though, is that the second camera needs to be in perspective mode!

UPDATE 2

I have just tried disabling a mask on pnlRuleList - this also makes the child elements render in the correct order, but obviously the mask no longer works!

UPDATE 3

OK, creating pnlRule in the editor also creates this problem. Sorry for the misinformation above! This is definitely a "Screen Space - Camera" + using a mask issue!

UPDATE 4

Another revelation! Just tried changing the lighting mode; I use Deferred Lighting - changing it to Vertex or Forward lighting solves the problem. Again, not the desired effect, but at least I'm getting closer to a solution.

hackinguirenderunderworld-1.jpg (196.8 kB)
hackinguirenderunderworld-2.jpg (69.1 kB)
Comment
Add comment · Show 2
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 Galactic_Muffin · Feb 26, 2015 at 08:12 PM 0
Share

did you ever fix this problem? I'm also trying to render Ugui in world space behind 3D models int he scene so some GUI elements can be seen even if they are behind objects.

avatar image Spikeh · Feb 27, 2015 at 08:25 AM 0
Share

I did, yes - I had to apply a shader I found as a mask on top of the UI elements in question. It's been a while, so I can't remember ther specific implementation, but I will post the shader and a quick screenshot in an answer

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Spikeh · Feb 27, 2015 at 08:30 AM

In response to a comment, here's the solution I found.

I had to create a new uGUI element, size it to the same size as the internal panel (pnlRuleList), but render it on top of pnlRuleList:

alt text

"MaskClear" is a custom material, which uses the following shader (I found this after searching for days - I can't find it again, and I probably renamed the shader itself as I tend to do to make more sense to me):

 Shader "UI/MaskClear"
 {
     SubShader
     {
         Tags
         {
             "Queue"="Transparent"
             "IgnoreProjector"="True"
             "RenderType"="Transparent"
             "PreviewType"="Plane"
             "CanUseSpriteAtlas"="True"
         }
      
         Stencil
         {
             Ref 0
             Comp Always
             Pass Zero
             ReadMask 255
             WriteMask 255
         }
  
         Cull Off
         Lighting Off
         ZWrite Off
         ZTest [unity_GUIZTestMode]
         Fog { Mode Off }
         Blend Zero One
         ColorMask 0
  
         Pass
         {
         CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #include "UnityCG.cginc"
          
             struct appdata_t
             {
                 float4 vertex   : POSITION;
                 float4 color    : COLOR;
                 float2 texcoord : TEXCOORD0;
             };
  
             struct v2f
             {
                 float4 vertex   : SV_POSITION;
                 fixed4 color    : COLOR;
             };
          
             fixed4 _Color;
  
             v2f vert(appdata_t IN)
             {
                 v2f OUT;
                 OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
                 OUT.color = IN.color;
                 return OUT;
             }
  
             fixed4 frag(v2f IN) : SV_Target
             {
                 return IN.color;
             }
         ENDCG
         }
     }
 }


pnlrulelist.jpg (147.3 kB)
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

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

26 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

Related Questions

GUI in front of 4.6 UI 1 Answer

[4.6 - UI] Force GUI to render on top 1 Answer

How can I calculate whether an UI element is inside my camera view? 0 Answers

uGUI - Empty GameObjects vs Canvas 2 Answers

Version 5: OnGui() over UI Objects, and vice versa 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