我最近更新了我的项目以使用 Unity 2019,并且在很大程度上这是一个无缝过渡。然而,我在运行时更新图像 Sprite 时遇到了一个问题。
我正在开发的游戏在每个关卡开始时都有倒计时(3、2、1,GO!)。倒计时文本是一系列在计时器上更新的 Sprite ,并且所有 Sprite 都具有相同的分辨率。
在更新到 Unity 2019 之前,这工作正常,但现在当倒计时到达“GO!”时,它看起来好像被水平压扁了。
我还进行了一项测试,其中我反向运行倒计时(GO!、1、2、3),结果出现了相反的效果。在这种情况下,“走!”看起来很正常,1、2、3看起来像是水平展开的。
我设置了一个最小的测试场景,其中只有一个 Canvas /图像,并附加了以下脚本。出现相同的行为,如下面的 gif 所示。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ImageSwapTest : MonoBehaviour
{
Image image; //Image component
[SerializeField] Sprite[] countDownSprites; //Array of sprites used to populate the image component
void Start()
{
image = GetComponent<Image>();
StartCoroutine(UpdateImage());
}
IEnumerator UpdateImage()
{
int counter = 0;
while (counter < sprites.Length)
{
yield return new WaitForSeconds(1); //Wait 1 second
image.sprite = countDownSprites[counter]; //Update the image to the next sprite
counter++;
}
}
}
预期:
实际:
如果我颠倒顺序,则“GO!”是正常尺寸,数字已展开:
图像属性:
如您所见,所有 4 个 Sprite 都具有相同的分辨率。我不确定是什么导致它们在运行时像这样调整大小。
Unity 2019 中处理图像/ Sprite 的方式是否发生了变化?也许这只是一个错误?
最佳答案
同样的事情也发生在我身上。修复方法是:
- 在 Unity 中选择纹理
- 更改 Sprite 模式 > 网格类型从“紧密”更改为“全矩形”
关于c# - 在运行时更新 Sprite 后图像变形(Unity 2019),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55804225/