[ad_1]
(code up to date to combine yours straight) This covers resizing the picture, nevertheless it would not cowl the facet ratio. The facet ratio might be straightforward (targetHeight = TargetWidth * (peak/width)), or advanced if you’re attempting to make use of extra shaping options/positioning.
public Sprite[] Images;
//Index begins at one as a result of we’re setting the primary sprite in Start() methodology
public int _Index = 1;
void Start(){
//Set the picture to the primary one
this.gameObject.GetComponent<SpriteRenderer>().sprite = Images[0];
}
public void onClick(){
var go = gameObject;
var renderer = go.GetComponent(SpriteRenderer);
if (renderer == null) return;
//Reset again to 0 so it could possibly loop once more if the final sprite has been proven
if (_Index >= Images.Length)
_Index = 0;
//Set the picture to array at component index, then increment
renderer.sprite = Images[_Index++];
go.remodel.localScale = Vector3(1,1,1);
var width = renderer.sprite.bounds.measurement.x;
var peak = renderer.sprite.bounds.measurement.y;
var targetWidth = 16; // change this with the code to get the width you need
var targetHeight = 9; // change this with the code to get the peak you need
go.remodel.localScale.x = targetWidth / width;
go.remodel.localScale.y = targetHeight / peak;
}
However, You may simply use the UI Image, which ought to scale with the display screen measurement by default. https://unity3d.com/be taught/tutorials/subjects/user-interface-ui/ui-image
[ad_2]