Hello Guest

Author Topic: Dynamic Asset Loading and code wise manipulation  (Read 27581 times)

auzette

  • Newbie
  • *
  • Posts: 3
    • View Profile
Dynamic Asset Loading and code wise manipulation
« on: January 26, 2012, 10:41:28 pm »
Hello there. I bought a copy of 2D Toolkit to evaluate for a project that we're going to be working on... it's a multi-player game that will be 2D and involve quite a lot of customization and artistic variances between players characters.

I'm looking at the toolkit, and I like the editors and I like the simplicity and power, and the efficiency on mobile platforms for draw calls (batching is good. :) ).

One thing that's going to be a critical make or break for us is being able to dynamically load content (sprites, animations, etc...) as players enter and leave various areas of the game (its an MMO style game). Because of the crafting, customization of character look and feel is going to be a high priority. To that end, we'll need to be able to swap out art pieces on animated sprites, weapons will be swapped out on charactesr in the game, etc.

It's infeasible to load all the possible permutations of these combinations ahead of time into one scene (for that matter, the scenes themselves will need to be dynamically loaded via asset bundles and downloaded and instantiated. Also, the various monsters and such will need to be such that we can programmatically instantiate them (not have them hard coded in the scene). In fact, very little will be hard coded into the scene.

I know that the emphasis of the toolkit is heavily on easy editing, and a lot of it gears towards having your assets in the scene. My question is... has someone been able to achieve what we're asking with 2D toolkit in a manner that's flexible, maintainable, and efficient? Or if not, does the developer(s) have some insight on to how this may work. If it's really trying to fit a square peg in a round hole, we're not shy about going with a different solution (and we can always consider the toolkit for a different game in the future, since we bought it already). But in evaluating this great toolkit, I definitely want to know how easy it is to dynamically create sprites and programmatically manipulate them at runtime based on user settings...

Thanks!!

Tim

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Dynamic Asset Loading and code wise manipulation
« Reply #1 on: January 26, 2012, 11:17:11 pm »
Hi there.

You should be able to load content dynamically with no problem. I'll list the steps for a Sprite Collection, but the same concept applies to Sprite Animations too.

1. Find the data block of your sprite collection, by clicking on your Sprite Collection, and double clicking on the "Sprite Collection" field in the inspector. By default this is named "data".

2. Rename this to something sensible. This is the name you'll use to load the asset in game, so please ensure that it is unique.

3. Create a folder called "resources" in the same directory, or wherever you want, and drag this asset into that folder.

3. Load the sprite collection by doing the following:
Code: [Select]
sprite = GetComponent<tk2dSprite>();
sprite.collection = Resources.Load ("human", typeof(tk2dSpriteCollectionData)) as tk2dSpriteCollectionData;
sprite.spriteId = sprite.GetSpriteIdByName("nameofsprite");



If you need to create a sprite programmatically, all you need to do is:
Code: [Select]
GameObject go = new GameObject();
tk2dSprite sprite = go.AddComponent<tk2dSprite>();
sprite.SwitchCollectionAndSprite(spriteCollectionData, 0);
sprite.Build();

Hope that helps, and feel free to ask if you have any other questions.
« Last Edit: October 15, 2013, 06:33:56 pm by unikronsoftware »

auzette

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Dynamic Asset Loading and code wise manipulation
« Reply #2 on: January 27, 2012, 12:59:33 am »
Fantastic response, THANKS for the quick reply. I will dive into your example and give it a shot!

auzette

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Dynamic Asset Loading and code wise manipulation
« Reply #3 on: January 27, 2012, 06:12:41 pm »
Thanks again for the response. I went ahead and tested out your suggestions, and am able to load sprite collections dynamically and associate them with sprites at runtime and programmatically.

One odd quirk I found is that... when I use the code below, I am able to create a sprite, dynamically load the sprite collection, and see it in the game. However, when I click on the game object that i created with the sprite, everything reverts back to a different state (in this case, it turns into the built-in spider sprite collection with the sprite being the body. I assume that when the inspector attempts to view this, that is finds something it doesn't like and somehow reinitailizes the sprite to defaults?

To test this, I loaded the polygon colliders scene in the sample. I selected the "data" prefab in DemoSpriteCollection in the project and saved it into a resources directory file named "bubba".

I then added code in the GameController reload method to do this:

      // works, but selecting the game object reverts its properties to being a spider.
//      GameObject go = new GameObject();
//      tk2dSprite sprite = go.AddComponent<tk2dSprite>();
//      sprite.collection = Resources.Load ("bubba", typeof(tk2dSpriteCollectionData)) as tk2dSpriteCollectionData;
//      sprite.spriteId = sprite.GetSpriteIdByName("2dtoolkit_logo");   
      
      
      // works, but selecting the game object reverts its properties to being a spider.
      
      GameObject go = new GameObject();
      tk2dSprite sprite = go.AddComponent<tk2dSprite>();
      tk2dSpriteCollectionData collection = Resources.Load ("bubba", typeof(tk2dSpriteCollectionData)) as tk2dSpriteCollectionData;
      sprite.SwitchCollectionAndSprite(collection, 12);
      sprite.Build();
      sprite.scale = new Vector3(10, 10, 10);

I see the sprite fine in the game view. Clicking the "Game Object" in the hierarchy view will flip it back to spider mode.

Any idea what I'm doing wrong? It'll be important for us to be able to click and view properties on sprites dynamically generated in game, so I'm hoping this is just something I'm not seeing.

Thanks!


 

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Dynamic Asset Loading and code wise manipulation
« Reply #4 on: January 27, 2012, 08:09:47 pm »
When you moved the object, I am assuming you did it in the Unity Editor and not in explorer / finder? One more thing to check is that the Sprite Collection still links to the Data object, you can verify this by checking the "Sprite Collection" property.

What version of 2D Toolkit are you using, and in which version of Unity?

In any case, I've attached something to test this - it works fine on mine, and could also serve as a demo to others. Let me know how it goes...

Flopopo

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Dynamic Asset Loading and code wise manipulation
« Reply #5 on: March 21, 2013, 01:31:44 pm »
Hello,

In order to handle multiple resolutions assets, i would like to load the right ressources in a tk2dSpriteCollection at runtime.
I found this post, but i can't figure how to apply the procedure described by unikron :

Quote
1. Find the data block of your sprite collection, by clicking on your Sprite Collection, and double clicking on the "Sprite Collection" field in the inspector. By default this is named "data".

I don't understand where to find the data block i have to rename and place in the Ressources folder. It seems the 2d ToolKit interface has changed since the original post in January 26, 2012.

Note that i'm a unity newbie...

evs

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Dynamic Asset Loading and code wise manipulation
« Reply #6 on: March 21, 2013, 06:44:44 pm »
@Flopopo

Here's a screen shot that should help


cheers

evs
« Last Edit: March 21, 2013, 06:49:50 pm by evs »

Flopopo

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Dynamic Asset Loading and code wise manipulation
« Reply #7 on: March 22, 2013, 09:42:05 am »
Thanks @evs,

It runs now !  :D

I have one more question :

If i create some sprite collections, for example : SpriteCollection1x, SpriteCollection2x and SpriteCollection4x.
As long as i don't create manually in the scene editor a tk2dSprite from these sprite collections, the resources (texture & material) of these sprite collections won't be loaded when the application (scene) start ?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Dynamic Asset Loading and code wise manipulation
« Reply #8 on: March 22, 2013, 01:12:56 pm »
@Flopopo -
Yes.

leef918

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Dynamic Asset Loading and code wise manipulation
« Reply #9 on: June 24, 2013, 04:41:32 am »
Hi.
i have tried following your instructions.
but it not works for me,
my unity version:3.56
2dtoolkit:1.7
and the screen capture from running your demo unitypackage.

leef918

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Dynamic Asset Loading and code wise manipulation
« Reply #10 on: June 24, 2013, 05:07:49 am »
Hi.
i have tried following your instructions.
but it not works for me,
my unity version:3.56
2dtoolkit:1.7
and the screen capture from running your demo unitypackage.
***************************
***********************************
the version 1.92 works well for me ,tks.

spinner

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Dynamic Asset Loading and code wise manipulation
« Reply #11 on: August 18, 2013, 02:58:23 pm »
It seems the code used as response is not valid anymore with the new 2Dtoolkit API.
What's the new way to instantiate and render sprites dinamically?

Thanks.

spinner

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Dynamic Asset Loading and code wise manipulation
« Reply #12 on: August 18, 2013, 03:08:51 pm »
I found a way, not sure if it's that right though:
       
        tk2dSprite sprite;
   
   void Start () {
      tk2dSpriteCollectionData spriteCollection = Resources.Load ("Level1", typeof(tk2dSpriteCollection)) as tk2dSpriteCollectionData;
      sprite = tk2dSprite.AddComponent(gameObject,spriteCollection, "texture-1");
      sprite.transform.position = new Vector3(0,0,0);
   }


I am getitng a null reference point with my sprite Collection, So I'm not sure if I'm loading that right.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Dynamic Asset Loading and code wise manipulation
« Reply #13 on: August 18, 2013, 03:20:57 pm »
You can either tk2dSprite.AddComponent, or SetSprite( collection, "texture-1" ) after its been created.

This is not going to work:
  tk2dSpriteCollectionData spriteCollection = Resources.Load ("Level1", typeof(tk2dSpriteCollection)) as tk2dSpriteCollectionData;
It should be:
  tk2dSpriteCollectionData spriteCollection = Resources.Load ("Level1", typeof(tk2dSpriteCollectionData)) as tk2dSpriteCollectionData;

Also the data object itself must be in a resources folder for the corrected code above to work.
spriteCollection should not be null, otherwise the rest of it isn't going to work.

spinner

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Dynamic Asset Loading and code wise manipulation
« Reply #14 on: August 18, 2013, 03:26:21 pm »
Right, thanks!

I'm still getting Null reference though, The collection data name "Leve1" is found under the field dataObject on editor settings right?
(Nevermind!)

Btw, great support!


« Last Edit: August 18, 2013, 03:29:19 pm by spinner »