- Home /
How do I set the order of elements in Canvas (Screen Space - Camera)?
I have a ScrollRect which content is a collection of GameObjects created at runtime. The hierarchy for the Canvas containing this scrollable list is as follows:
-
Canvasset to Screen Space - Camera- BackgroundPanel (
Panelwith anImageattached) that is supposed to act as a background for the scrollable list- ScrollView (
ScrollRect)- Content (Empty
Rect) that acts as parent for all of theGameObjects generatedChild1 (
GameObject)Child2 (
GameObject)...
- Content (Empty
- ScrollView (
- BackgroundPanel (
The problem I am facing is that the image on the BackgroundPanel is rendered on top of the children of the Content Rect as shown in the Image 1. What I want is to have that Image shown behind them as shown in Image 2.
I've tried altering the depth of all of the children of BackgroundPanel by setting their position on the Z-axis to a lower value, and as long as it is below 0 (The depth of the BackgroundPanel) they show on top as desired. However this only seems to work if I alter the position of the elements after the the list of children has been generated.
I have tried setting the depth to -1 on runtime when I create the list using the following code, but upon calling it a second time the GameObjects generated will have their z-position to 45. This means that they are once again hidden behind the BackgroundPanel.
foreach (Car car in storedCars){
// Spawn display model
GameObject displayModel = CarGenerator.instance.GenerateDisplayModelOfCar(car);
// Set the content panel as parent
displayModel.transform.SetParent(contentPanel.transform);
}
// Scale contentPanel to fit the newly added cars
RectTransform contentRect = contentPanel.GetComponent<RectTransform>();
contentRect.sizeDelta = new Vector2(contentRect.childCount * 100f, contentRect.sizeDelta.y);
contentRect.position = new Vector3(contentRect.childCount * 50f, 0f, -1f);
Any suggestions as to how to keep the child objects rendered above the BackgroundPanel?
Your answer
Follow this Question
Related Questions
GUI Layout problem 1 Answer
know GUILayout current screen position 2 Answers
Content Size Fitter woes (Solved: Link to download you may want) 1 Answer
Is there any way to make gui faster and effective 0 Answers
How do I scale a Toggle button 0 Answers