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 Noah Dyer · Jan 01, 2015 at 08:20 PM · uicanvasparentingchild object

Can 3d objects be children of a canvas?

[Providing an answer to this question based on my own research and experimentation to save others the hassle]

Technically yes, but the behavior probably won't be what you are expecting, especially if you are using overlay or camera rendering.

Important things that won't work as you expect:

  1. If you disable the canvas (enabled = false), the 3d object will still render. To overcome this, you can use a foreach loop or other method to access child objects and SetActive to false manually.

  2. The object will not render based on it's position in the canvas, but rather it's position in the world relative to the camera. This basically means that your object will not appear to be part of the UI, and if it ever renders, it will be because the camera has coincidentally arrived at a position that includes your object, which is almost certainly not what you intend.

Hope this helps someone!

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 Umresh · Jan 02, 2015 at 11:49 AM 0
Share

Yes you can disable if you change the layer to UI for the 3D object also. In that way if you disable the canvas it won't render.

avatar image Noah Dyer · Jan 02, 2015 at 10:06 PM 0
Share

@umresh, I was initially excited by your tip, but it doesn't seem to be accurate. See my code below.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Noah Dyer · Jan 02, 2015 at 05:52 PM

[I wold have posted this as a comment, but it exceeds the character limit.]

@Umresh, I implemented your tip and it does not change the behavior I've described above. Here's a code snippet to try.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;

 public class TestCanvasScript : MonoBehaviour {

 Canvas testCanvas;

 void Start (){

     CreateCanvas();

}

 void Update () {
 
     // If you press space, swap the canvas state
     if(Input.GetKeyDown(KeyCode.Space)){
 
          testCanvas.enabled = !testCanvas.enabled;
 
     }
 
 }
 
 private void CreateCanvas()
     {
         
         //We have to create the canvas and all it's components first
         
         //Set up the base canvas
         GameObject canvasObject = new GameObject("Test Canvas");
         canvasObject.layer = LayerMask.NameToLayer ("UI");
         testCanvas = canvasObject.AddComponent<Canvas>();
         canvasObject.AddComponent<GraphicRaycaster>();
         testCanvas.renderMode = RenderMode.Overlay;
 
         //Create text object to satisfy that the canvas is working as expected
                 //This object is a little more complicated than desired for an example
                 //but ripping out my code would have taken longer than it was worth
                 //and the formatting may be instructional to someone
         GameObject textObject = new GameObject("Test Text");
                 //********** Notice that even though I don't set the layer, and it is therefore in the default layer, it will disable when the canvas is disabled
         
         //Create and configure the text component of the text object
         textObject.transform.parent = testCanvas.transform;
         Text testText = textObject.AddComponent<Text>();
                 testText.text = "Test Test Test!";
 
         //We want the text rect to be largely unchanged by resizing events
         //So we're anchoring to a single point in the top middle
         testText.rectTransform.anchorMin = new Vector2(.5f, 1f);
         testText.rectTransform.anchorMax = new Vector2(.5f, 1f);
         testText.rectTransform.anchoredPosition = Vector2.zero; //pivot from center
 
         //we want the text to be the width of the shortest screen dimension and 1/4 as tall
         float textWidth = Screen.width > Screen.height ? Screen.height : Screen.width;
         float textHeight = textWidth * .25f;
         testText.rectTransform.sizeDelta = new Vector2 (textWidth, textHeight);
 
         //We want the top of the text box to be at the top of the screen, but with a 10% border.
         testText.rectTransform.position = new Vector3(testText.rectTransform.position.x, (float)(Screen.height/2 + (Screen.height - textHeight)/2 - Screen.height * .1), testText.rectTransform.position.z);
 
         //configure font properties
         testText.font = Resources.FindObjectsOfTypeAll<Font>()[0];
         testText.fontSize = (int)(textHeight * .2); 
         testText.color = Color.white;
         testText.alignment = TextAnchor.UpperCenter;
 
         //test for 3d object
         GameObject testSphere = GameObject.CreatePrimitive (PrimitiveType.Sphere);
         testSphere.layer = LayerMask.NameToLayer ("UI");
         testSphere.transform.parent = canvasObject.transform;
                 //******** Notice that even though I've specified the UI layer, the  object does not render as UI is expected to render, and it does not disable when the canvas is disabled.
 
         
     }

 }
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Setting transform parents to Vectrosity canvases. 1 Answer

rendering panel w/children 0 Answers

Access and modify Text within a prefab 1 Answer

How to DontDestroyOnLoad a specific ui of a canvas? 1 Answer

Dialogue box wont show in game unless it's in the canvas but... 2 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