- Home /
Question by
El-Deiablo · Jul 09, 2016 at 02:37 PM ·
scaleui imagescreen size
UI Image Scaler
I have a code that can scale my sprite renderer image, but I have been unable to get the UI image scaler to work. It should be almost the same I would think, but with my code the image scales down real small. How could I make this work?
Here's my code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ImageScaler : MonoBehaviour {
public Camera cam;
// Use this for initialization
void Start () {
Image sr = GetComponent<Image>();
if (sr)
{
float worldScreenHeight = (float)(cam.orthographicSize * 2.0);
float worldScreenWidth = worldScreenHeight / Screen.height * Screen.width;
float newLocalScale = (worldScreenWidth / sr.sprite.bounds.size.x);
transform.localScale = new Vector3(newLocalScale, newLocalScale, 1);
}
}
// Update is called once per frame
void Update () {
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612084509im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to stretch photo from camera to look like video streaming from camera to screen 1 Answer
How do I size a UI Image to fit the sprite exactly? 2 Answers
Scale gameobject to the width of screen 1 Answer
How do you handle a scaledown animation on a UI button? 0 Answers
UI and Collision Detection zone 0 Answers