Search Unity

How to change canvas size during runtime?

Discussion in 'UGUI & TextMesh Pro' started by Edvard-D, Aug 24, 2014.

  1. Edvard-D

    Edvard-D

    Joined:
    Jun 14, 2012
    Posts:
    129
    I can't seem to figure out how to change the width of a canvas during runtime. I'm using UnityEngine.UI so that shouldn't be the problem, but I thought the Rect Transform component would be accessible automatically from a canvas object. For some context I'm trying to create an area selection box when you click and drag your mouse. The canvas is set up as a world space renderer.

    Thanks in advance for your help.
     
  2. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,225
    You need to modify the sizeDelta property of the RectTransform. See the scripting documentation for more information.
     
  3. Edvard-D

    Edvard-D

    Joined:
    Jun 14, 2012
    Posts:
    129
    Sorry, to clarify: I need to know how to access the RectTransform component of the canvas during runtime. From there I already know which properties of RectTransform I need to modify. For example, I thought it would be accessed using something like "canvasObject.gameObject.rectTransform" similar to how you can access the regular transform using "canvasObject.gameObject.transform".
     
  4. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
    GetComponent<RectTransform>();
     
  5. Edvard-D

    Edvard-D

    Joined:
    Jun 14, 2012
    Posts:
    129
    Oh duh, thank you :)
     
  6. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,225
    You can also do:
    var rectTransform = gameObject.transfrom as RectTransform;
     
    oranchad likes this.