Author Topic: (Solved) Making table elements pixel perfect  (Read 1542 times)

Euthyphro

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 11
    • View Profile
(Solved) Making table elements pixel perfect
« on: August 08, 2014, 05:05:56 PM »
I'm trying to make my UILabel's pixel perfect however I'm running into problems with positioning the gameobject to a rounded number under a UITable.

My problem is 1) the UITable itself keeps repositioning after and 2) the table elements keep repositioning to a non-pixel perfect position. In my code below I go through all the children gameobjects to make sure they are set to the proper positioning.

  1. uiTable.repositionNow = true;
  2. //get all children and set to pixel perfect position.
  3. Transform[] allChildrenInContainer = GetComponentsInChildren<Transform>();
  4. foreach (Transform child in allChildrenInContainer) {
  5.         child.localPosition = new Vector3(Mathf.RoundToInt(child.localPosition.x),
  6.                                                    Mathf.RoundToInt(child.localPosition.y)+0.5f,
  7.                                                    Mathf.RoundToInt(child.localPosition.z));
  8.  
  9.        //NGUITools.MakePixelPerfect(child); same problem as above when trying this.
  10. }
  11.  

Any suggestions?

*EDIT* Solved it myself, hopefully this helps someone else. The solution is to call the "onRepositoin" invoker and it will invoke the method you create when the table is reposition. In my case, I created RepositionPixelPerfect() to be invoked once repositionNow does occur. This method is then invoked after and in my case I wrote this method to set each element to pixel perfect positioning.

Solution:

  1. void YourMethodForAddingTableElements()
  2. {
  3.     uiTable.onReposition += RepositionPixelPerfect; //onReposition wil invoke RepositionPixelPerfect method when UITable is repositioned.
  4.     uiTable.repositionNow = true;
  5. }
  6.  
  7. void RepositionPixelPerfect()
  8. {
  9.     Transform[] allChildrenInContainer  = GetComponentsInChildren<Transform>();
  10.     foreach (Transform child in allChildrenInContainer) {
  11.          NGUITools.MakePixelPerfect(child);
  12.     }
  13. }
  14.  
  15.  
« Last Edit: August 08, 2014, 05:34:25 PM by Euthyphro »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Making fonts pixel perfect
« Reply #1 on: August 08, 2014, 05:25:05 PM »
The table simply uses the bounds of the widgets inside. You mentioned making sure they're pixel perfect, but you can actually force it by selecting the table and using ALT+SHIFT+P. Also make sure the game view uses dimensions that are dividable by two, or it won't look crisp.

Also note that setting the "repositionNow" bool delays the actual repositioning logic until later. You need to call the Reposition() function for it to be immediate.