void Dofit()
{
Transform canvasObj = GameObject.Find("Canvas").transform;
float width = canvasObj.GetComponent<RectTransform>().sizeDelta.x;
float height = 1;
if (GetComponent<RawImage>())
{
Texture tex = GetComponent<RawImage>().texture;
float tw = tex.width;
float th = tex.height;
//图片宽高比例
float xyper = tw / th;
height = width / xyper; //根据宽度计算高度
}
else
{
Sprite sp= GetComponent<Image>().sprite;
float xyper = sp.rect.size.x / sp.rect.size.y;
height = width / xyper; //根据宽度计算高度
}
GetComponent<RectTransform>().sizeDelta = new Vector2(width, height);
}
|