Search Issue Tracker

Postponed means that the issue was either a feature request or something that requires major refactoring on our side. Since that makes the issue not actionable in the close future we choose to close it as Postponed and add it on our internal roadmaps and technical debt pages instead.

Postponed

Votes

13

Found in

2017.4.0f1

2018.3.0a1

2018.3.0f2

2019.1.0a1

Issue ID

1110225

Regression

No

NullReferenceException from GenerateTiledSprite when SpriteAtlas is not included in the build and Image Type is set to 'Tiled'

uGUI

-

How to reproduce:
1. Open user-submitted project (IssueRepeatedImageWithSpriteAtlas) and 'SampleScene' scene
2. Enter Play Mode

Expected result: no errors are displayed in the Console window
Actual result: NullReferenceException is displayed in the Console window

Reproducible with: 2017.4.18f1, 2018.3.1f1, 2019.1.0a13

Notes:
'Image Type' must be set to 'Tiled'
'Include in Build' option on the SpriteAtlas must be unchecked

  1. Resolution Note (2019.2.X):

    As noted the ticket is low priority and they deemed we can close the case. Not sure why it was triggered as high priority.

Comments (6)

  1. mWundke

    Sep 01, 2021 07:05

    We fixed the issue using our own Image component with the following changes to it's OnPopulateMesh method:

    /// <summary>
    /// Our UI Image component fixing the Tiled Image and Sprite Late Binding bug
    /// </summary>
    public class Image : UnityEngine.UI.Image
    {
    /// <summary>
    /// Update the UI renderer mesh.
    /// </summary>
    protected override void OnPopulateMesh(VertexHelper toFill)
    {
    // In tiled mode we have to ensure we have a valid Texture. This is to prevent the following bug
    // https://issuetracker.unity3d.com/issues/nullreferenceexception-from-generatetiledsprite-when-spriteatlas-is-not-included-in-the-build-and-image-type-is-set-to-tiled
    if ((overrideSprite != null && overrideSprite.texture == null) && type == Type.Tiled)
    {
    OnPopulateMeshForTiledImageBug(toFill);
    return;
    }
    base.OnPopulateMesh(toFill);
    }

    /// <summary>
    /// Copied from UnityEngine.UI.Graphic to solve:
    /// https://issuetracker.unity3d.com/issues/nullreferenceexception-from-generatetiledsprite-when-spriteatlas-is-not-included-in-the-build-and-image-type-is-set-to-tiled
    /// </summary>
    /// <param name="vh"></param>
    private void OnPopulateMeshForTiledImageBug(VertexHelper vh)
    {
    var r = GetPixelAdjustedRect();
    var v = new Vector4(r.x, r.y, r.x + r.width, r.y + r.height);

    Color32 color32 = color;
    vh.Clear();
    vh.AddVert(new Vector3(v.x, v.y), color32, new Vector2(0f, 0f));
    vh.AddVert(new Vector3(v.x, v.w), color32, new Vector2(0f, 1f));
    vh.AddVert(new Vector3(v.z, v.w), color32, new Vector2(1f, 1f));
    vh.AddVert(new Vector3(v.z, v.y), color32, new Vector2(1f, 0f));

    vh.AddTriangle(0, 1, 2);
    vh.AddTriangle(2, 3, 0);
    }
    }

  2. tarpaha

    Aug 31, 2021 20:07

    Also got this exception.
    Unity, you need to change

    if (activeSprite != null && (hasBorder || activeSprite.packed || activeSprite.texture.wrapMode != TextureWrapMode.Repeat))

    to the

    if (activeSprite != null && activeSprite.texture != null && (hasBorder || activeSprite.packed || activeSprite.texture.wrapMode != TextureWrapMode.Repeat))

    In the Image.cs

  3. joe_nk

    Jul 21, 2020 21:04

    I'm seeing this too in 2019.4.1f1. I'm a bit miffed as to why a common, easily reproducible crash has been considered low priority?

  4. jason_unity266

    Oct 21, 2019 21:22

    2019.2.6f1

  5. MikePage_Artrix

    Sep 09, 2019 00:36

    you can get around this by ensuring the atlas is loaded before the call to GenerateTiledSprite

  6. MikePage_Artrix

    Sep 04, 2019 05:09

    2019.2.2

Add comment

Log in to post comment

All about bugs

View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.