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. Dismiss Notice

Unity2D: Scaling and Locking Image to anchor points

Discussion in 'UGUI & TextMesh Pro' started by wasicool7, Sep 1, 2016.

  1. wasicool7

    wasicool7

    Joined:
    Jul 28, 2016
    Posts:
    25
    I have a script that uses Index to change an objects sprite using OnClick to move through the sprites, like a slideshow. I want the image to scale to the size of my anchor points, because I've notice that when I stretch the game or put the game on maximize play the image stays the same size and doesn't scale to the size of my anchor points. How would I do that?
    This is my script:

    Code (CSharp):
    1.     public Sprite[] Images;
    2.     //Index starts at one because we are setting the first sprite in Start() method
    3.     public int _Index = 1;
    4.  
    5.     void Start(){
    6.         //Set the image to the first one
    7.         this.gameObject.GetComponent<SpriteRenderer>().sprite = Images[0];
    8.     }
    9.  
    10.     public void onClick(){
    11.         //Reset back to 0 so it can loop again if the last sprite has been shown
    12.         if (_Index >= Images.Length)
    13.             _Index = 0;
    14.      
    15.         //Set the image to array at element index, then increment
    16.         this.gameObject.GetComponent<SpriteRenderer>().sprite = Images[_Index++];
    17.     }
    Thank you. :)
     
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    What objects are you setting up? A UI IMage? a 2D sprite? Your talking about Anchor Points which Implies a UI element, but Sprite Render is on the 2D sprite by default.
     
  3. wasicool7

    wasicool7

    Joined:
    Jul 28, 2016
    Posts:
    25
    Yeah it was a UI Image, but I took the component off and place a 2D sprite renderer instead, so that the script can access it and change the sprite image by OnClick. I use the anchor points to lock the image in place but when I resize the game screen or click "Maximize On Play" the image isn't resizing to a larger size/ with the anchor points. It's just stay to it's original size.
     
  4. wasicool7

    wasicool7

    Joined:
    Jul 28, 2016
    Posts:
    25
    Thank you for your reply :)
     
  5. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Sounds like your anchor's aren't set right. Try this:
    1. Make a new project. Under Build settings make sure resizable is checked.
    2. Drag a sprite Image into the project.
    3. Create a UI Image. Drag your sprite asset onto it.
    4. Click its RectTransform and using Shift+Alt set it to upper left.
    5. Create a 2nd UI Image. Drag your sprite asset onto it.
    6. Click its RectTransform and using Shift+alt click the exact middle setting.
    7. Now Manualy change these numbers under Anchors:
    8. Min: x=0.25 Y =0.25 Max X=0.75 Y=0.75
    9. Build and run, start your program in Windowed Mode: 640x480.
    10. Then maximize it
    You should see it starts out with the image in the upper left and middle the same size
    After maximizing the top left image stays the same size, but the middle image expands