Dynamically build UI elements on Canvas: ScreenSpace-Camera, previously ScreenSpace-Overlay

Hello,

My application works fine, 100% expected results with Canvas set on ScreenSpace-Overlay. I have written a function that takes an array of integers and based on its values it dynamically builds vertical bars inside a panel. I have an UI-Image as a prefab and that I instantiated multiple time, set its anchor to bottom left position and increase the x-offset from that point in increments of its width+padding. Everything works fine, check the screenshots.

I have now a fancy idea of animating the vertical bars… to have Unity Particle Effects on the top of each bar and to play it. From what I searched… you cannot get Particle Effects working on Canvas-UI unless the Canvas is set as ScreenSpace-Camera. I managed to obtain success with Particle Effects and ScreenSpace-Camera canvas… everything works… but now the same code that dynamically builds the UI doesnt work anymore… the vertical bars are set on a total new position outside the UI.

My code:

private void ArrangeImagesOnCanvas(List<int> varArray)
    {
        Canvas canvas = FindObjectOfType<Canvas>();
        //float h = canvas.GetComponent<RectTransform>().rect.height * canvas.GetComponent<RectTransform>().localScale.y;
        //float w = canvas.GetComponent<RectTransform>().rect.width * canvas.GetComponent<RectTransform>().localScale.x;

        float h = _panelImageForSortingAlgorithm.GetComponent<RectTransform>().rect.height * canvas.GetComponent<RectTransform>().localScale.y;
        float w = _panelImageForSortingAlgorithm.GetComponent<RectTransform>().rect.width * canvas.GetComponent<RectTransform>().localScale.x;

        float minH = 20.0f;
        float maxH = h - 20;

        int minList = varArray.Min();
        int maxList = varArray.Max();

        _imageWidth = (w - (varArray.Count * _PADDING)) / varArray.Count;
   
        List<float> heightsList = new List<float>();

        float _tempFloat = 0;
        for (int i = 0; i < varArray.Count; i++)
        {
            _tempFloat = ScaleIntervals(varArray[i], new Vector2(minList, maxList), new Vector2(minH, maxH));
            heightsList.Add(_tempFloat);
        }

        for(int i = 0; i<_instancesOfImages.Count;i++)
        {
            Destroy(_instancesOfImages[i]);
        }
        _instancesOfImages.Clear();
        for (int i = 0; i < varArray.Count; i++)
        {
            GameObject instantiatedImage = Instantiate(_imageToInstance, _imageToInstance.transform.position, Quaternion.identity);
            instantiatedImage.transform.SetParent(_panelImageForSortingAlgorithm.transform);
            Image img = instantiatedImage.GetComponent<Image>();
            img.rectTransform.anchorMin = new Vector2(0, 0);
            img.rectTransform.anchorMax = new Vector2(0, 0);
            img.rectTransform.pivot = new Vector2(0, 0);

            img.rectTransform.position = new Vector3(i * (_imageWidth + _PADDING), 0, 0);
            img.rectTransform.sizeDelta = new Vector2(_imageWidth, heightsList[i]);

            _instancesOfImages.Add(instantiatedImage);
        }
    }

For the same code the vertical bars can be seen in Forum3.png image… if I put

float w = _panelImageForSortingAlgorithm.GetComponent<RectTransform>().rect.width;```

then the vertical bars are like in Forum4.png image.

How can I position correctly the vertical bars as in ScreenSpace-Overlay but this time to be on ScreenSpace-Camera ? Please help or give me some advices or tips...

![8416644--1113000--Forum1.png|1352x379](upload://zyXkViihtt25yt4va4VWtCpedFK.png)
![8416644--1113003--Forum2.png|1058x426](upload://ru2b6LdxBkI9ig4FGGeZiedheqx.png)
![8416644--1113006--Forum3.png|1461x448](upload://ppefsni1xdht2tZx7E9EnN1HjEm.png)
![8416644--1113009--Forum4.png|1063x459](upload://a9e7qXI533D7WItZ3lXPsnsAHjZ.png)

I’ll move your post to the UI forum. The 2D forum is not related to UI.