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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Set Native Size By Script

Discussion in 'UGUI & TextMesh Pro' started by Cuicui_Studios, Mar 13, 2015.

  1. Cuicui_Studios

    Cuicui_Studios

    Joined:
    May 3, 2014
    Posts:
    69
    Hi, I was trying to set a UI image on button native size by script. And after a few tries and a web research, I need some help.

    I´m using I2Localize for a sprite on button and the sprites on each language have different size. So I tried this code:

    Code (CSharp):
    1. public void SetSize(){
    2.  
    3.     for(int i=0;i<Targets.Count;i++){
    4.  
    5.         Targets[i].GetComponent<Image>().SetNativeSize();
    6.  
    7.         }
    8.  
    9.     }
    This code has no error on console but neither has no effect.

    Someone tried this before?

    Thanks so much!
     
    hawwka likes this.
  2. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,684
    Try applying it in "LateUpdate" else the anchor updates in the Update loop might interfere with changing it

    However I've not personally seen this working. Better to store the native size and then update the "SizeDelta" of the RectTransform when you need to rather than using "SetNativeSize"
     
    Cuicui_Studios likes this.
  3. psyydack

    psyydack

    Joined:
    Aug 30, 2013
    Posts:
    93
    Here is working normally. (on Start, on Update, on Awake, etc)
    Are you sure this getting right Image Component?

    This is native size :

    Code (CSharp):
    1. Sprite overrideSprite = GetComponent<Image>().overrideSprite;
    2.         print ( overrideSprite.rect );
     
    Cuicui_Studios likes this.
  4. Cuicui_Studios

    Cuicui_Studios

    Joined:
    May 3, 2014
    Posts:
    69
    This works perfectly. Any way I will report this to Unity because I assume that the SetNativeSize Function should work like other Unity functions...

    Thanks for the help!
     
  5. Cuicui_Studios

    Cuicui_Studios

    Joined:
    May 3, 2014
    Posts:
    69
    Thanks for the help. I´m sure with the Component because I just move my code to LateUpdate and it works. The override method did not work for me.

    Thanks!
     
  6. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,684
    It's by design @CuiCuiStudios .
    The processing of UI with Unity is a bit complex, we just need to learn the new new new way of doing things.
    During the normal processing, Unity is doing a lot of work under the hood to calculate the postion and the final render of the UI. Hence way such things are only available in LateUpdate. If you make changes, you are effectively targeting the next frame, not the current one :D
     
  7. guneyozsan

    guneyozsan

    Joined:
    Feb 1, 2012
    Posts:
    99
    I used SetNativeSize() on two objects that download the texture online (Unity 2017.1.2p1). Same function call, duplicate objects. One worked well, the other defaulted to the size 2x2 (or 4x4 not remember well). I guess when the function runs internally matters and if implemented this way there is no way to know when it will work.

    I can't use LateUpdate as suggested as the function runs when the download is complete. So I postponed setting the native size of both objects to the moment the object becomes visible to the user (it is a UI element). This way it works fine.
     
  8. sandeepsmartest

    sandeepsmartest

    Joined:
    Nov 7, 2012
    Posts:
    138
    how to set native size in editor mode using editor script??i tried it but not working.Any clue??
     
  9. losingisfun

    losingisfun

    Joined:
    May 26, 2016
    Posts:
    35
    If anyone is still interested in this you can use sizeDelta on a RectTransform quite easily:

    Code (CSharp):
    1. RectTransform rectTransform;
    2. Image image;
    3.  
    4. rectTransform.sizeDelta = new Vector2(image.sprite.texture.width, image.sprite.texture.height);
    If you're using different Pixel per units on your canvas scaler and texture you can account for it like so (if you're not using a sprite from a sheet)
    Code (CSharp):
    1. CanvasScaler canvasScaler;
    2. float delta = image.sprite.pixelsPerUnit / canvasScaler.referencePixelsPerUnit;
    3.         rectTransform.sizeDelta = new Vector2(image.sprite.texture.width, image.sprite.texture.height) / delta;
     
    Tiollo likes this.
  10. adrianfrancisco

    adrianfrancisco

    Joined:
    Jul 29, 2021
    Posts:
    14
    Thank you!! But, if the image is from a sprite sheet, do you know how to get the width and height of that specific sprite from a bigger sheet? Because otherwise it takes the size of the whole atlas...

    Edit: Nevermind, I changed the anchor to a point anchor, and then image.SetNativeSize(); worked prefectly for me even with sprites from a spritesheet
     
    Last edited: Feb 10, 2022