如何使spriterenderer保持目标方面

问题描述

我正在和一个spriterenderer女巫一起工作,我需要保持目标纵横比为16:9

类似UI Image PreserveAspect的事情。

我正在对宽高比更改脚本使用相同的宽高比

         // Determine ratios of screen/window & target,respectively.
     float screenRatio = Screen.width / (float)Screen.height;
     float targetRatio = targetAspect.x / targetAspect.y;

     if(Mathf.Approximately(screenRatio,targetRatio)) {
         // Screen or window is the target aspect ratio: use the whole area.
         _camera.rect = new Rect(0,1,1);
     }
     else if(screenRatio > targetRatio) {
         // Screen or window is wider than the target: pillarBox.
         float normalizedWidth = targetRatio / screenRatio;
         float barThickness = (1f - normalizedWidth)/2f;
         _camera.rect = new Rect(barThickness,normalizedWidth,1);
     }
     else {
         // Screen or window is narrower than the target: letterBox.
         float normalizedHeight = screenRatio / targetRatio;
         float barThickness = (1f - normalizedHeight) / 2f;
         _camera.rect = new Rect(0,barThickness,normalizedHeight);
     }

然后我只是像这样调整精灵的大小

         sprite.transform.position = _myCamera.transform.position + Vector3.forward * this.transform.position.z;

     sprite.transform.localScale = new Vector3(1,1);
     Vector3 lossyScale = sprite.transform.lossyScale;

     float width = sprite.sprite.bounds.size.x;
     float height = sprite.sprite.bounds.size.y;

     float worldScreenHeight = Camera.main.orthographicSize * 2f;
     float worldScreenWidth = worldScreenHeight / Screen.height * Screen.width;

     Vector3 xWidth = sprite.transform.localScale;
     xWidth.x = worldScreenWidth / width;
     sprite.transform.localScale = xWidth;
     //transform.localScale.x = worldScreenWidth / width;
     Vector3 yHeight = sprite.transform.localScale;
     yHeight.y = worldScreenHeight / height;
     sprite.transform.localScale = yHeight;

     Vector3 newLocalScale = new Vector3(sprite.transform.localScale.x / lossyScale.x,sprite.transform.localScale.y / lossyScale.y,sprite.transform.localScale.z / lossyScale.z
     );

     sprite.transform.localScale = newLocalScale;

但这是结果

enter image description here

enter image description here

有人可以帮助我弄清楚我做错了什么吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)