SpriteRenderer在复制纹理后收缩

时间:2017-06-24 10:47:47

标签: unity3d sprite texture2d

我有一个带纹理的SpriteRenderer,我想制作纹理的副本并将此副本分配给相同的SpriteRenderer

//Copy of the texture that will be changes in runtime
[HideInInspector]
public Texture2D tempTexture;
//FilterMode of the copied texture
[SerializeField]
private FilterMode filterMode = FilterMode.Bilinear;

/// <summary>
/// Create a copy of the texture that is used by a SpriteRenderer and unload the original texture from the memory
/// </summary>
void Start()
{
    var spriteRenderer = GetComponent<SpriteRenderer>();
    if(spriteRenderer==null)
    {
        Debug.LogError("SpriteRenderer is null");
        return;
    }
    var tex = spriteRenderer.sprite.texture;
    if (tex == null)
    {
        Debug.LogError("Sprite's texture is null");
        return;
    }

    Debug.Log("original texture size is: " + tex.width + " : " + tex.height);

    tempTexture = new Texture2D(tex.width, tex.height, tex.format, false);
    tempTexture.filterMode = filterMode;
    try
    {
        var colors = tex.GetPixels();
        tempTexture.SetPixels(colors);
    }
    catch(Exception ex)
    {
        Debug.LogError(ex.Message);
        return;
    }
    tempTexture.Apply();
    Resources.UnloadAsset(tex);
    spriteRenderer.sprite = Sprite.Create(tempTexture, spriteRenderer.sprite.textureRect, Vector2.one * 0.5f);
}

我不会在Inspector中更改纹理的大小(或使其更大),因为它会覆盖一切完美的效果。但是如果我将纹理的大小设置得更小,那么当我的脚本完成它的工作时,精灵就会缩小。

再一次。如果纹理有大小,比方说大于512x512,我在检查器中将其更改为512,那么精灵缩小并变小两倍。

问题出现在我的脚本中,因为如果我禁用它 - 一切都没问题,即使我覆盖了纹理的大小。

有关如何修复它的任何想法?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案

arcsegment
精灵的

Viewbox属性负责其大小