c# - 在运行时更新 Sprite 后图像变形(Unity 2019)

标签 c# unity-game-engine

我最近更新了我的项目以使用 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++;
        }
    }
}

预期:

enter image description here

实际:

enter image description here

如果我颠倒顺序,则“GO!”是正常尺寸,数字已展开:

enter image description here

图像属性:

enter image description here

enter image description here

如您所见,所有 4 个 Sprite 都具有相同的分辨率。我不确定是什么导致它们在运行时像这样调整大小。

Unity 2019 中处理图像/ Sprite 的方式是否发生了变化?也许这只是一个错误?

最佳答案

同样的事情也发生在我身上。修复方法是:

  1. 在 Unity 中选择纹理
  2. 更改 Sprite 模式 > 网格类型从“紧密”更改为“全矩形”

关于c# - 在运行时更新 Sprite 后图像变形(Unity 2019),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55804225/

相关文章:

c# - 获取完整的英文月份名称

javascript - C# RadioButton List 使用 $ ("input[name=' <%=​​ 获取 JS 中的值

c# - 当玩家触摸边缘时,2D 游戏平台相机会升起

c# - 轨迹渲染器在传送时留下轨迹

android - 如何在android Unity应用程序中禁用android运行时位置权限提示

c# - Samsung Gear VR - 创建菜单

c# - 对象 A 引用了对象 B,而对象 B 将对象 A 作为公共(public)属性 - 设计不好?

c#:Enumerable 类中的 Where()、OrderBy() 和 Select() 不应该采用委托(delegate)类型、lambda 表达式或匿名类型作为参数

c# - 序列化类关键字的好处

unity-game-engine - 在 MAMP 本地主机上运行 Unity WebGL 构建时出错