- Home /
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:
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.
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!
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.
@umresh, I was initially excited by your tip, but it doesn't seem to be accurate. See my code below.
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.
}
}