如何将2D SpriteRenderer调整为Canvas面板的大小?

时间:2019-07-05 09:08:50

标签: c# unity3d

如何通过脚本将2d SpriteRenderer调整大小或重新调整为Canvas面板的大小?我正在使用正交相机。这是我到目前为止尝试过的。

 private Vector2 spriteSize;
 private Vector2 spriteScale;

 [SerializeField] private SpriteRenderer sprRendImage;
 [SerializeField] private RectTransform panel;

 private void Start()
 {
     Vector3[] corners = new Vector3[4];
     panel.GetWorldCorners(corners);
     spriteSize = sprRendImage.bounds.size;
     float disX = Vector3.Distance(corners[0], corners[3]);
     float disY = Vector3.Distance(corners[2], corners[3]);
     spriteScale.x = disX / spriteSize.x;
     spriteScale.y = disY / spriteSize.y;
     sprRendImage.transform.localScale = new Vector2(spriteScale.x, spriteScale.y);
 }

0 个答案:

没有答案