2D Toolkit Forum

2D Toolkit => Support => Topic started by: fryza on April 26, 2013, 07:53:32 pm

Title: Dynamically Load Collection
Post by: fryza on April 26, 2013, 07:53:32 pm
I have 10 different levels but only 1 scene. I want to set the background image at runtime. The problem is not all of my 10 backgrounds fit on one sprite. So I suppose I need to load different collections at run time. Is that right? What is the best way to do this?

I was thinking:

if (level 1 -3)
loadcollection1;
setbackgroundSprite;

if (level 4-7)
etc

How should I proceed?
Title: Re: Dynamically Load Collection
Post by: unikronsoftware on April 27, 2013, 05:07:28 pm
Well if it is JUST the background image you care about, and you aren't really bothered about dynamically loading other things, the simplest solution to this will be to:

1. Load the texture at runtime using Resources.Load. http://docs.unity3d.com/Documentation/ScriptReference/Resources.Load.html

2. Once you have the texture loaded, you can create a sprite from this texture by using tk2dSprite.CreateFromTexture http://unikronsoftware.com/2dtoolkit/doc/html/classtk2d_sprite.html#a63a90d3e086c76bca6be406de79d5159

Check sample #13 which shows you other options on how to create a sprite at runtime. Each of these functions takes a Texture2D which you can obtain from step 1 above, or any other means

Just don't store it in the inspector in your scene - that will mean it gets loaded with your scene.
Title: Re: Dynamically Load Collection
Post by: larryapple on May 25, 2013, 07:47:00 pm
Hi, I am trying to understand the details of sample #13, and when I set a breakpoint with the Mono Debugger in the function tk2dSpriteCollectionData CreateFromTexture, Unity crashes with "The Debugger is waiting for an expression evaluation to finish, Method: UnityEngine.Object.GetInstanceID".

I am using the latest Unity Pro 4.1.3.
Title: Re: Dynamically Load Collection
Post by: larryapple on May 25, 2013, 11:16:12 pm
I apologize, it is apparently a problem with Unity 4.1.3. I reverted to 4.1.2, and now debugging works.
Title: Re: Dynamically Load Collection
Post by: unikronsoftware on May 26, 2013, 12:30:11 am
Good to know about the 4.1.3 debugging issues - I've not updated to that version, might put that off until these issues are resolved.