Author Topic: Table clipping not working for sprites?  (Read 6510 times)

EToreo

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 88
    • View Profile
Table clipping not working for sprites?
« on: December 30, 2012, 10:44:01 PM »
I am trying to (dynamically) create a list of items (trains) the player can buy.  I have set up a table (with 2 rows) and I load the data in from an XML file.  For each item, I instantiate 2 new Prefabs (cells for the table) and add them to the table object.  See code:

  1.       foreach(TrainTemplate pTrainTemplate in mTemplates)
  2.       {
  3.          // Col 1 - Picture
  4.          GameObject pCol1Fake = Instantiate(Resources.Load("Prefabs/GUI/TrainTemplateTable/Col1"), Vector3.zero, Quaternion.identity) as GameObject;
  5.          GameObject pCol1 = NGUITools.AddChild(mTableGameObject, pCol1Fake);
  6.          Destroy(pCol1Fake);
  7.          pCol1.name = Convert.ToString(i++) + " - Col 1 - " + pTrainTemplate.Name;
  8.          UISprite pSprite = pCol1.transform.FindChild("Sprite").GetComponent<UISprite>();
  9.          pSprite.spriteName = pTrainTemplate.Icon;
  10.          pSprite.MakePixelPerfect();
  11.          
  12.          
  13.          // Col 2 - Text
  14.          GameObject pCol2Fake = Instantiate(Resources.Load("Prefabs/GUI/TrainTemplateTable/Col2"), Vector3.zero, Quaternion.identity) as GameObject;
  15.          GameObject pCol2 = NGUITools.AddChild(mTableGameObject, pCol2Fake);
  16.          Destroy(pCol2Fake);
  17.          pCol2.name = Convert.ToString(i++) + " - Col 2 - " + pTrainTemplate.Name;
  18.          
  19.          UILabel pLable = pCol2.transform.FindChild("Label").GetComponent<UILabel>();
  20.          DebugUtils.Assert(pLable != null);
  21.          pLable.text = "Name: " + pTrainTemplate.Name + "\nCost: $" + pTrainTemplate.Cost + "\nMax Speed: " + pTrainTemplate.CarSpeed[0];
  22.       }
  23.  

I am running into the problem that the sprites do not clip while the text does.  Any suggestions?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Table clipping not working for sprites?
« Reply #1 on: December 31, 2012, 06:47:40 AM »
Make sure the shader used by your sprite's atlas is Unlit/Transparent Colored, or clipping won't work.

Also never use Instantiate. Use NGUITools.AddChild, or your layers won't be correct.

EToreo

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 88
    • View Profile
Re: Table clipping not working for sprites?
« Reply #2 on: December 31, 2012, 01:20:39 PM »
Thanks for getting back to me.  The Atlas's material has the "Unlit/Transparent Colored" shader on it.  That leaves the Instantiate call.

How can I Resorces.Load a Prefab and then use NGUITools.AddChild on the result?  I fooled around with it and I can't seem to be able to do it without calling Instantiated on the loaded resource first.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Table clipping not working for sprites?
« Reply #3 on: December 31, 2012, 05:23:51 PM »
NGUITools.AddChild(parentObject, Resources.Load(yourPrefab));

EToreo

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 88
    • View Profile
Re: Table clipping not working for sprites?
« Reply #4 on: December 31, 2012, 05:33:56 PM »
I tried that and while it cleaned up my code (and some copy constructors, I'm sure), I still get the same sprite behavior.  :-\

I am sure all the textures are from the same Atlas and that the Atlas has the correct shared on it.

What else can I try?

P.S.  The Prefab being loaded is a GameObject with a UISprite under it.  That means that the top level GameObject in the prefab has a UIBasePanel script on it.  (I didn't put it there, I think NGUI did when I created the sprite widget - disabling that script causes the sprite not to draw at all.)

  1.    void Start ()
  2.    {
  3.       Transform pTableTransform = gameObject.transform.FindChild("SubPanel/Table");
  4.       DebugUtils.Assert(pTableTransform != null);
  5.       mTableGameObject = pTableTransform.gameObject;
  6.      
  7.       mTemplates = TrainTemplateLoader.loadAll();
  8.       int i = 0;
  9.       foreach(TrainTemplate pTrainTemplate in mTemplates)
  10.       {
  11.          // Col 1 - Picture
  12.          GameObject pCol1 = NGUITools.AddChild(mTableGameObject, Resources.Load("Prefabs/GUI/TrainTemplateTable/Col1") as GameObject);
  13.          pCol1.name = Convert.ToString(i++) + " - Col 1 - " + pTrainTemplate.Name;
  14.          UISprite pSprite = pCol1.transform.FindChild("Sprite").GetComponent<UISprite>();
  15.          pSprite.spriteName = pTrainTemplate.Icon;
  16.          pSprite.MakePixelPerfect();
  17.          
  18.          
  19.          // Col 2 - Text
  20.          GameObject pCol2 = NGUITools.AddChild(mTableGameObject, Resources.Load("Prefabs/GUI/TrainTemplateTable/Col2") as GameObject);
  21.          pCol2.name = Convert.ToString(i++) + " - Col 2 - " + pTrainTemplate.Name;
  22.          
  23.          UILabel pLable = pCol2.transform.FindChild("Label").GetComponent<UILabel>();
  24.          DebugUtils.Assert(pLable != null);
  25.          pLable.text = "Name: " + pTrainTemplate.Name + "\nCost: $" + pTrainTemplate.Cost + "\nMax Speed: " + pTrainTemplate.CarSpeed[0];
  26.       }
  27.    }
« Last Edit: December 31, 2012, 05:40:15 PM by EToreo »

EToreo

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 88
    • View Profile
Re: Table clipping not working for sprites?
« Reply #5 on: December 31, 2012, 07:07:11 PM »
Well then, upgrading to the newest version of NGUI fixed my problem.  I was using FREE 2.0.0.7 or something close to it and I upgraded to FULL 2.2.6c.  Sorry to bother you about it.
« Last Edit: December 31, 2012, 07:13:14 PM by EToreo »