Search Unity

Problem loading Texture2D when exporting for Windows Store

Discussion in 'Windows' started by DanRP, Aug 29, 2013.

  1. DanRP

    DanRP

    Joined:
    May 26, 2013
    Posts:
    33
    Hello,

    I'm just getting started with exporting for Windows Store / Windows 8 and I'm getting a run-time error that I can't figure out. I've included the code below. Note: I'm using the 2D Toolkit add-on.

    Code (csharp):
    1. GameObject oGameObject = null;
    2. Texture2D oTexture = null;
    3.  
    4. oTexture = Resources.Load( pFilename, typeof(Texture2D) ) as Texture2D;
    5.        
    6. if (!ReferenceEquals(oTexture, null)) {
    7.     Rect tSize = new Rect(0, 0, oTexture.width, oTexture.height);
    8.     Vector2 tAnch = new Vector2( oTexture.width * OffsetX, oTexture.height * (1.0f - OffsetY));
    9.            
    10.     tk2dSpriteCollectionSize tkCollectionSize = tk2dSpriteCollectionSize.ForTk2dCamera();
    11.            
    12.     oGameObject = tk2dSprite.CreateFromTexture( oTexture, tkCollectionSize, tSize, tAnch );
    13.            
    14.     oSprite = oGameObject.GetComponent<tk2dSprite>();
    15. }
    I'm getting a the error "Exception: Object reference not set to an instance of an object." on this line:
    Code (csharp):
    1. oGameObject = tk2dSprite.CreateFromTexture( oTexture, tkCollectionSize, tSize, tAnch );
    I believe that the problem is with the oTexture object.

    Here's what I know:
    1) When I Debug.Log my oTexture object, it shows as "BaseObject: NotAvailableDuringDebugging"
    2) I know the resource is being loaded because the width and height properties are correct (100)
    3) This code works perfectly when exporting to android and iOS

    It seems that something is wrong with my oTexture object when exporting for Windows Store...

    Can anyone help me figure out why this code is failing?



    Thanks!
    Dan
     
  2. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,918
    What is tk2dSprite?
     
  3. DanRP

    DanRP

    Joined:
    May 26, 2013
    Posts:
    33
    tk2dSprite is a 2D sprite object that is part of the 2D Toolkit add-on.
     
  4. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,918
    That means CreateFromTexture belongs to 2D sprite object, do you know how it's implemented?
     
  5. DanRP

    DanRP

    Joined:
    May 26, 2013
    Posts:
    33
    I do have the source for the CreateFromTexture function...

    Upon a closer look at the log file, this is the full error:

    Code (csharp):
    1. Exception: Object reference not set to an instance of an object.
    2. InnerException:
    3. AdditionalInfo:
    4.   at tk2dSprite.Awake() in c:\data\Games\StackTheStatesUnity\Assets\TK2DROOT\tk2d\Code\Sprites\tk2dSprite.cs:line 25
    5.   at tk2dSprite.UnityFastInvoke_Awake()

    Here's the code around line 25:

    Code (csharp):
    1. 24: mesh = new Mesh();
    2. 25: mesh.hideFlags = HideFlags.DontSave;
    3. 26: GetComponent<MeshFilter>().mesh = mesh;

    It's almost as if the "mesh = new Mesh();" line maybe doesn't produce a valid instance of Mesh?
     
  6. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,918
    Try to debug it with Visual Studio, you can also enable all exceptions in Debug->Exceptions, and continue until you hit that exception
     
  7. DanRP

    DanRP

    Joined:
    May 26, 2013
    Posts:
    33
    I'll see what I can do. I haven't been able to setup Visual Studio to set breakpoints in my code. I tried following the instructions below, but haven't had any luck.
    https://docs.unity3d.com/Documentation/Manual/windowsstore-debugging.html

    I went to Debug->Exceptions and set it to stop on all exceptions. Execution did stop at a few places, but not in my code...
     
  8. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,918
    You should have hit "Object reference not set to an instance of an object." unless your bug is not deterministic
     
  9. DanRP

    DanRP

    Joined:
    May 26, 2013
    Posts:
    33
  10. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    Tomas,

    This is happening because RequireComponent isn't creating the prerequisite components (MeshRenderer MeshFilter) when running on Windows Store builds. It seems to work on all other builds (Web, Pc, iOS and Android). Is this expected behaviour? If not I'll file a bug.