unity获取image组件的长度
时间: 2023-10-16 17:07:30 浏览: 139
您好,通过获取Image组件中的Sprite对象并使用它的bounds属性可以获得Image的长度和宽度。可以使用以下代码来实现:
```csharp
Image img = GetComponent<Image>();
float width = img.sprite.bounds.size.x * img.transform.localScale.x;
float height = img.sprite.bounds.size.y * img.transform.localScale.y;
```
其中,img.sprite.bounds.size.x和img.sprite.bounds.size.y表示Sprite对象在世界空间中的长度和宽度,img.transform.localScale.x和img.transform.localScale.y表示Image在世界空间中的缩放比例。
阅读全文
相关推荐










