Author Topic: Cannot load my custom atlas  (Read 4084 times)

scheissmann

  • Guest
Cannot load my custom atlas
« on: April 01, 2013, 01:25:44 AM »
Hello,

I'm attempting to create a slicedsprite at runtime. I have already created an atlas along with a few sprites in the editor, so there's already an "inventory" atlas which I placed (total 3 files, inventory.mat/.prefab/.png) under Assets/Resources/ (also tried Assets/Resources/inventory/). My code to add new sprite is like this:

  1.        
  2. void Start () {
  3.                 UISlicedSprite sp1 = NGUITools.AddWidget<UISlicedSprite>(GameObject.Find("Panel"));
  4.                 GameObject atlas = Instantiate( Resources.Load("inventory")) as GameObject;
  5.                 sp1.atlas = atlas.GetComponent<UIAtlas>();
  6.                 sp1.spriteName = "item_A_Tr";
  7.         }

I also tried without the instantiate:
GameObject atlas = Resources.Load("inventory") as GameObject;

It seems like no matter what the resources.load always returns null. All the examples I found online are using pre-existing atlas packages like scifi atlas or fantasy atlas, but here I'm using a custom atlas, not sure if I'm doing everything right?

Thanks!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Cannot load my custom atlas
« Reply #1 on: April 01, 2013, 02:22:51 AM »
Create a script that references an atlas in a public variable and attach it to some object that's present in the scene. Make this script accessible via a singleton (or just FindObjectOfType on it), and use its public atlas reference field.

This way you don't need to Resources.Load anything.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Cannot load my custom atlas
« Reply #2 on: April 01, 2013, 09:10:58 AM »
Also make sure that your path is correct and that the spelling/capitals are correct.

  1.    
  2. void Start () {
  3.         UISlicedSprite sp1 = NGUITools.AddWidget<UISlicedSprite>(GameObject.Find("Panel")); //this will likely give you problems since you may have more than a single "Panel" object.
  4.  
  5.  
  6.         //slight rework
  7.         GameObject atlasPrefab = Resources.Load("inventory") as GameObject;
  8.         UIAtlas atlas = Instantiate(atlasPrefab).GetComponent<UIAtlas>();
  9.         sp1.atlas = atlas;
  10.         sp1.spriteName = "item_A_Tr";
  11.     }
  12.  

I think Resources.Load gives you an Object, not a GameObject, so you can't shorthand it inside the Instantiate method.

scheissmann

  • Guest
Re: Cannot load my custom atlas
« Reply #3 on: April 01, 2013, 11:37:57 AM »
Thank you everyone! I think for now I'll go with creating an object with a public variable that points to its atlas.

I'm still curious though about why my resource loading fails and gets "null". The path and file names are all correct as far as I can tell. I'll get some screenshot of it when I get home.