Author Topic: Sprite.inner /.outer replacement?  (Read 2116 times)

Sigh@iCreate

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Sprite.inner /.outer replacement?
« on: October 21, 2013, 09:30:36 AM »
Hi guys, dont know if I can get much help on this, but I am a little stuck and I cant figure this out.

I got code quite similar to this a while back from these forums and it is used to create a new atlas for use on ngui buttons.

Prior to updating, the code below worked fine, but since updating I am getting errors.
I have pasted the code and the error areas are commented.

  1. private void CreateAtlas()
  2.         {
  3.                 atlasTexture = new Texture2D(1, 1);
  4.                 spriteCoordInfo = atlasTexture.PackTextures(textureArray, 0);
  5.                
  6.                 atlasMat = new Material(Shader.Find("Unlit/Transparent Colored"));
  7.                 atlasMat.SetTexture("_MainTex", atlasTexture);         
  8.                
  9.                 myAtlas.GetComponent<UIAtlas>();
  10.                 myAtlas.spriteMaterial = atlasMat;
  11.                 myAtlas.coordinates = UIAtlas.Coordinates.TexCoords;      <--- Error 1
  12.                
  13.                 myAtlas.spriteList.Clear();
  14.                
  15.                 for(int i  = 0; i < spriteCoordInfo.Length; i++)
  16.                 {
  17.                         Rect coordinate = spriteCoordInfo[i];
  18.                         UIAtlas.Sprite sprite = new UIAtlas.Sprite();   ****           
  19.                         sprite.name = names[i];
  20.                         sprite.inner = coordinate;              <---- error 2
  21.                         sprite.outer = coordinate;             <---- error 3
  22.                         sprite.paddingBottom = 0;
  23.                         sprite.paddingTop = 0;
  24.                         sprite.paddingLeft = 0;
  25.                         sprite.paddingRight = 0;
  26.                         myAtlas.spriteList.Add(sprite);                
  27.                 }
  28. }

I can remove the lines listed, and then change this line
  1.  UIAtlas.Sprite sprite = new UIAtlas.Sprite();  ****   
and replace it with
  1. UISpriteData sprite = new UISpriteData();
   but when I then assign the new sprites to my buttons I am getting new errors which are:
aabb.IsValid()
!IsFinite(outDistanceForSort)
UnityEditor.DockArea:OnGUI()
!IsFinite(outDistanceAlongView)
UnityEditor.DockArea:OnGUI()
!IsFinite(outDistanceForSort)
!IsFinite(outDistanceAlongView)
These errors just repeat themselves over and over.

I am fairly new to Unity and NGUI, so I apologise if this wastes anyones time, I will continue to try and fix this myself too!

Thanks in advance for any time and advice given.

Sigh

edit: I should note, that I believe the errors to be caused by the fact that the sprites are being added without their inner/outer coordinates? as a result, when rendering the sprite, the coordinates are (id imagine) 0, 0, 0, 0 ?

I did find a SetRect within spritedata, but that takes 4 int values? is this the replacement? if so, does this mean I need to get pixel coordinates from my packtextures line? rather than texcoords? Thanks again
« Last Edit: October 21, 2013, 09:49:01 AM by Sigh@iCreate »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Sprite.inner /.outer replacement?
« Reply #1 on: October 21, 2013, 01:48:20 PM »
Well, first of all... there are no more atlas coordinates. It's always in pixels now. TexCoords option was removed.

Second, the UIAtlas.Sprite is not used anymore (which reminds me, I should make it private now...)

Use UISpriteData instead. It contains much more understandable values for X, Y, width, height, border, and padding.

Sigh@iCreate

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: Sprite.inner /.outer replacement?
« Reply #2 on: October 22, 2013, 03:16:52 AM »
Hey ArenMook, thank you very much for getting back to me so quick.

I looked through your code and saw the changes which you mentioned, but having never used pixel coordinates for textures up until now, so that is mainly what I was asking about. (edit at bottom of my post).

I have converted the texcoords returned by packtextures to pixelcoords and I have it working now.

Thanks again for your help and time!