Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    Dismiss Notice

[Solved] UI Panels are not transformed correctly when Canvas set to "Screen Space - Camera"

Discussion in 'UGUI & TextMesh Pro' started by asperatology, Nov 21, 2015.

  1. asperatology


    Mar 10, 2015
    Using screenshots to show what is wrong.

    In this first picture, you can see that I have set the Canvas to "Screen Space - Camera". The Plane Distance is 10. Sorting Layer is Default. Order in Layer is 0.

    In the second picture, I show where the UI ScrollView is placed in the Canvas. The Content will contain 10 scripted UI panel elements that will be added in when the game launches.

    In this third picture, I showed the 10 scripted UI panel components that was added in. Note how huge the panel is. Scale is scaled by 46x, and the rotation is off by 288 on the X axis.

    In this fourth picture, this was how the UI panels look like.

    You may be wondering, "Why, just reset the rotation to zeroes, and scale the UI panels to (1f, 1f, 1f)? Simple."

    That is what I did:

    Code (CSharp):
    1. public void Setup() {
    2.     //For each level, instantiate a prefab and place it in the Content of the ScrollView.
    3.     //This allows the Attributes to show consistently the progression of the attributes for each level.
    4.     for (int i = 0; i < MAX_NUM_OF_LEVELS; i++) {
    5.         GameObject obj = MonoBehaviour.Instantiate<GameObject>(this.panelPrefab);
    6.         RectTransform rectTransform = obj.GetComponent<RectTransform>();
    7.         if (rectTransform != null) {
    8.             rectTransform.rotation = Quaternion.identity;
    9.             rectTransform.localScale = new Vector3(1f, 1f, 1f);
    10.         }
    11.         obj.transform.SetParent(this.transform);
    12.         this.prefabList.Add(obj);
    14.         Title title = obj.GetComponentInChildren<Title>();
    15.         if (title != null) {
    16.             title.titleText.text = "Level " + (i + 1).ToString();
    17.         }
    19.         Number number = obj.GetComponentInChildren<Number>();
    20.         if (number != null) {
    21.             //number.numberText.text = (0f).ToString();
    22.             number.numberText.text = this.unitAttributes.healthPrefabList[i].ToString();
    23.         }
    24.     }
    25. }
    The UI panels goes underneath the Quad game object.

    And the panels' rotations and scales are all back to (288f, 0f, 0f) and (46f, 46f, 46f), respectively.

    How do I fix the canvas scale via scripting? Or better, how do I fix this in general? I know I pixel = 1 Unity UI unit, but multiplying the scale by 0.00024 seems like a hack, and I want to avoid that.

  2. asperatology


    Mar 10, 2015
    I may have solved it.

    Code (CSharp):
    1. GameObject obj = MonoBehaviour.Instantiate<GameObject>(this.panelPrefab);
    2. RectTransform rectTransform = obj.GetComponent<RectTransform>();
    3. obj.transform.SetParent(this.transform);
    4. if (rectTransform != null) {
    5.     rectTransform.localPosition = new Vector3(0f, 0f, 0f);
    6.     rectTransform.localRotation = Quaternion.identity;
    7.     rectTransform.localScale = new Vector3(1f, 1f, 1f);
    8. }
    9. this.prefabList.Add(obj);
    It's a logic error. Set the UI panels' parents to be the main UI ScrollView, and then you set the local position, rotation, and scale. When setting the parent transform, the UI panels will have reverted back to "Screen Space - Overlay" positions and scales. This needs to be reverted back to "Screen Space - Camera" positions and scales.