Hello Guest

Author Topic: How to packageAtlas at Runtime by tk2dSpriteCollectionData.CreateFromTexture  (Read 5025 times)

deepsunm

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 1
    • View Profile
How to packageAtlas at Runtime by tk2dSpriteCollectionData.CreateFromTexture ?
I can not have right rect[] and anchors[] I think.
I used Texture2D.PackTextures to package multi texture into tk2dSpriteCollectionData, it seems loaded but regions and anchors are wrong.
What should I do to make them right?

I am using tk2dcamera
Code attached.

private static tk2dSpriteCollectionSize sz2 = tk2dSpriteCollectionSize.ForTk2dCamera ();
public static tk2dSpriteCollectionData packCoreIngredientCollectionData (ArrayList ingre)
   {
      int s = ingre.Count;
      string[] ids = new string;
      Texture2D[] atlas = new Texture2D;
      Texture2D packed = new Texture2D (512, 1024);
      IEnumerator ie = ingre.GetEnumerator ();
      int i = 0;
      while (ie.MoveNext()) {
         string id = ie.Current.ToString ();
         ids = id;
         string path = "INGREDIENT/" + id;
         Texture2D t = Resources.Load (path) as Texture2D;
 
         atlas = t;
         i++;
      }
      Rect[] rects = packed.PackTextures (atlas, 0 );
      Vector2[] anchors = new Vector2;
      int k = 0;
      foreach (Rect r in rects) {
         float xx = r.width/2;
         Debug.Log(xx);
         float yy = r.x + xx;
         Debug.Log(yy);
         //anchors [k] = new Vector2 (r.x + r.width / 2, r.y + r.height / 2);
         anchors [k] = new Vector2 (r.width / 2, r.height / 2);
         Debug.Log(r);
         Debug.Log(anchors[k]);
         k++;
      }
      
      tk2dSpriteCollectionData data = tk2dSpriteCollectionData.CreateFromTexture (packed,  sz2, ids, rects, anchors);
       return data;
   }



unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
I can't remember what the rects from PackTextures returns, but the tk2d one expects coordinates from top left. i.e. exactly what you'd see in Photoshop. Is the y coordinate flipped when you use packtextures? Print out some values, look at the texture in photoshop (or the inspector... UnityEditor.Selection.activeObject = packed; ) - it should be obvious what is going wrong.