Hello Guest

Author Topic: Windows Store: Problem loading Texture2D / Creating a tk2dSprite  (Read 5101 times)

DanRP

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 4
    • View Profile
Hello Everyone,

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.  Here's the code:

Code: [Select]
GameObject oGameObject = null;
Texture2D oTexture = null;

oTexture = Resources.Load( pFilename, typeof(Texture2D) ) as Texture2D;

if (!ReferenceEquals(oTexture, null)) {

Rect tSize = new Rect(0, 0, oTexture.width, oTexture.height);
Vector2 tAnch = new Vector2( oTexture.width * OffsetX, oTexture.height * (1.0f - OffsetY));

tk2dSpriteCollectionSize tkCollectionSize = tk2dSpriteCollectionSize.ForTk2dCamera();

oGameObject = tk2dSprite.CreateFromTexture( oTexture, tkCollectionSize, tSize, tAnch );

    oSprite = oGameObject.GetComponent<tk2dSprite>();
}

I'm getting an error on this line:
Code: [Select]
oGameObject = tk2dSprite.CreateFromTexture( oTexture, tkCollectionSize, tSize, tAnch );
Code: [Select]
Exception: Object reference not set to an instance of an object.
InnerException:
AdditionalInfo:
  at tk2dSprite.Awake() in c:\data\Games\StackTheStatesUnity\Assets\TK2DROOT\tk2d\Code\Sprites\tk2dSprite.cs:line 25

  at tk2dSprite.UnityFastInvoke_Awake()


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
4) I'm using 2D Toolkit version 2.1 final and Unity 4.2

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
« Last Edit: August 29, 2013, 10:28:14 pm by DanRP »

DanRP

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Windows Store: Problem loading Texture2D / Creating a tk2dSprite
« Reply #1 on: August 29, 2013, 05:14:19 pm »
Just to add...  There's another part of my app where I do a "Resources.Load" of a TK2D Sprite Collection.

Code: [Select]
tk2dSpriteCollectionData oPhysicsCollectionData = Resources.Load("Physics", typeof(tk2dSpriteCollectionData)) as tk2dSpriteCollectionData;
Debug.Log("oPhysicsCollectionData=" + oPhysicsCollectionData);

When I Debug.Log of the oPhysicsCollectionData object, I also get "BaseObject: NotAvailableDuringDebugging".

Is there something wrong with doing Resources.Load for Windows Store builds?

DanRP

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Windows Store: Problem loading Texture2D / Creating a tk2dSprite
« Reply #2 on: August 30, 2013, 12:59:07 am »
According to the log file, it's crashing at line 25 of tk2dSprite.cs.

Code: [Select]
Exception: Object reference not set to an instance of an object.
InnerException:
AdditionalInfo:
  at tk2dSprite.Awake() in c:\data\Games\StackTheStatesUnity\Assets\TK2DROOT\tk2d\Code\Sprites\tk2dSprite.cs:line 25
  at tk2dSprite.UnityFastInvoke_Awake()


Here's the code around line 25:

Code: [Select]
24: mesh = new Mesh();
25: mesh.hideFlags = HideFlags.DontSave;
26: GetComponent<MeshFilter>().mesh = mesh;


It's almost as if the "mesh = new Mesh();" line maybe doesn't produce a valid instance of Mesh for some reason?  And this causes line 25 to crash?

DanRP

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Windows Store: Problem loading Texture2D / Creating a tk2dSprite
« Reply #3 on: September 04, 2013, 02:26:41 am »
Ok, here's the problem...

There are some components used by tk2dsprite.cs and tk2dTextMesh.cs (and maybe others) that, for whatever reason, are null.  According to Unikron support, they are tagged as Required Components, but yet still seem to be null.

For instance, in the Awake() function of tk2dsprite.cs, there's a line:
Code: [Select]
GetComponent<MeshFilter>().mesh = mesh;
In this case, GetComponent<MeshFilter>() returns a null, so I had to change to code like this:
Code: [Select]
MeshFilter mf = GetComponent<MeshFilter>();
if (mf == null) { mf = gameObject.AddComponent<MeshFilter>(); }
mf.mesh = mesh;

There was one other place in tk2dsprite.cs and 2 places in tk2dTextMesh.cs where I had to do the same thing.

I hope this helps someone...

Dan