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.

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:
    124
    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,165
    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:
    124
    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:
    124
    Oh duh, thank you :)
     
  6. Tim-C

    Tim-C

    Unity Technologies

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