Author Topic: Share Time: Useful align and distribute script  (Read 4656 times)

dubbreak

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 15
    • View Profile
Share Time: Useful align and distribute script
« on: March 22, 2013, 01:05:31 AM »
Hey fellow ngui users. One of my coworkers (well subcontractor, technically) created this script and I thought I'd share.

I've attached an image of what it looks like in use. Use is pretty much how you'd expect. Select the items you want to align or distribute, then choose one of the align/distribute options. Works a charm and has saved me a lot of time.

Code:
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5.  
  6. public class AlignAndDistribute : MonoBehaviour {
  7.  
  8.         [MenuItem( "Align and Distribute/Align Left" )]
  9.         static void AlignLeft () {
  10.                 if(Selection.gameObjects.Any())
  11.                 {
  12.                         UpdateX(Selection.gameObjects.Min(go => go.transform.position.x));
  13.                 }
  14.         }
  15.        
  16.         [MenuItem( "Align and Distribute/Align Center" )]
  17.         static void AlignCenter () {
  18.                 if(Selection.gameObjects.Any())
  19.                 {
  20.                         UpdateX(Selection.gameObjects.Average(go => go.transform.position.x));
  21.                 }
  22.         }
  23.        
  24.         [MenuItem( "Align and Distribute/Align Right" )]
  25.         static void AlignRight () {
  26.                 if(Selection.gameObjects.Any())
  27.                 {
  28.                         UpdateX(Selection.gameObjects.Max(go => go.transform.position.x));
  29.                 }
  30.         }
  31.        
  32.         [MenuItem("Align and Distribute/Distribute Vertical")]
  33.         static void DistributeVertical () {
  34.                 if(Selection.gameObjects.Length >= 3)
  35.                 {
  36.                         var top = Selection.gameObjects.Max(go => go.transform.position.y);
  37.                         var bottom = Selection.gameObjects.Min(go => go.transform.position.y);
  38.                         List<GameObject> blah = Selection.gameObjects.ToList();
  39.                         blah.Sort((g1,g2) => (g1.transform.position.y >= g2.transform.position.y) ? 1 : -1);
  40.                         var i = 0f;
  41.                         var t = blah.Count*1f-1;
  42.                         foreach (var go in blah)
  43.                         {
  44.                                 var pos = go.transform.position;
  45.                                 pos.y = top*i/t + bottom*(1-i/t);
  46.                                 go.transform.position = pos;
  47.                                 i++;
  48.                         }
  49.                 }
  50.         }
  51.        
  52.         [MenuItem("Align and Distribute/Distribute Horizontal")]
  53.         static void DistributeHorizontal () {
  54.                 if(Selection.gameObjects.Length >= 3)
  55.                 {
  56.                         var top = Selection.gameObjects.Max(go => go.transform.position.x);
  57.                         var bottom = Selection.gameObjects.Min(go => go.transform.position.x);
  58.                         List<GameObject> blah = Selection.gameObjects.ToList();
  59.                         blah.Sort((g1,g2) => (g1.transform.position.x >= g2.transform.position.x) ? 1 : -1);
  60.                         var i = 0f;
  61.                         var t = blah.Count*1f-1;
  62.                         foreach (var go in blah)
  63.                         {
  64.                                 var pos = go.transform.position;
  65.                                 pos.x = top*i/t + bottom*(1-i/t);
  66.                                 go.transform.position = pos;
  67.                                 i++;
  68.                         }
  69.                 }
  70.         }
  71.        
  72.        
  73.         static void UpdateX (float val)
  74.         {
  75.                 foreach(var t in Selection.gameObjects)
  76.                 {
  77.                         var pos = t.transform.position;
  78.                         pos.x = val;
  79.                         t.transform.position = pos;
  80.                 }
  81.         }
  82.        
  83. }
  84.  
  85.  

To use, put this in a new file named AlignAndDistribute.cs and add it to your project.

If anyone else has any useful homemade tools they use with ngui I'd love to see them  ;D.

Malzbier

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 93
    • View Profile
Re: Share Time: Useful align and distribute script
« Reply #1 on: March 22, 2013, 10:01:39 AM »
I have wrote a script that is helping me a lot with different / dynamic buttons and panels.
The script called UpdateCollider.cs updates the size of a box collider according to the size of a UISlicedSprite.
Just add it to a button and it will grab the sprite in his children called "Background" (like NGUI usually calls the background sprite), or you can change the target by just replacing the targetTransform variable.
The if the script is insert in your project it can be found under Component/NGUI/Helper/ColliderUpdater .

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. [ExecuteInEditMode]
  5. [AddComponentMenu("NGUI/Helper/ColliderUpdater")]
  6. public class UpdateCollider : MonoBehaviour
  7. {
  8.         public Transform targetTransform;
  9.         private BoxCollider cachedCollider;
  10.         public Vector2 sizeoffset = new Vector2 (1, 1);
  11.         public float depth;
  12.         /// <summary>
  13.         /// Getting Stuff Cached
  14.         /// </summary>
  15.         void Start ()
  16.         {
  17.                 if (targetTransform == null) {
  18.                         targetTransform = transform.FindChild ("Background");
  19.                 }
  20.                 if (this.GetComponent<BoxCollider> () != null) {
  21.                         cachedCollider = this.GetComponent<BoxCollider> ();
  22.                 }
  23.         }
  24.         /// <summary>
  25.         /// Update the possition of the collider according to the sice of the target UISlicedSprite.
  26.         /// </summary>
  27.         void Update ()
  28.         {
  29.                
  30.                
  31.                 if (targetTransform != null) {
  32.                         cachedCollider.size = new Vector3 (sizeoffset.x * targetTransform.localScale.x, sizeoffset.y * targetTransform.localScale.y, targetTransform.localScale.z);
  33.                         cachedCollider.center = targetTransform.localPosition;
  34.                         if (targetTransform.GetComponent<UISlicedSprite> () != null) {
  35.                                 switch (targetTransform.GetComponent<UISlicedSprite> ().pivot) {
  36.                                 case UIWidget.Pivot.Bottom:
  37.                                         cachedCollider.center += new Vector3 (0, sizeoffset.y * targetTransform.localScale.y / -2, depth);
  38.                                         break;
  39.                                 case UIWidget.Pivot.BottomLeft:
  40.                                         cachedCollider.center += new Vector3 (+sizeoffset.x * targetTransform.localScale.x / 2, sizeoffset.y * targetTransform.localScale.y / 2, depth);
  41.                                         break;
  42.                                 case UIWidget.Pivot.BottomRight:
  43.                                         cachedCollider.center += new Vector3 (sizeoffset.x * targetTransform.localScale.x / -2, sizeoffset.y * targetTransform.localScale.y / 2, depth);
  44.                                         break;
  45.                                 case UIWidget.Pivot.Top:
  46.                                         cachedCollider.center += new Vector3 (0, sizeoffset.y * targetTransform.localScale.y / -2, 0);
  47.                                         break;
  48.                                 case UIWidget.Pivot.TopLeft:
  49.                                         cachedCollider.center += new Vector3 (sizeoffset.x * targetTransform.localScale.x / 2, sizeoffset.y * targetTransform.localScale.y / -2, depth);
  50.                                         break;
  51.                                 case UIWidget.Pivot.TopRight:
  52.                                         cachedCollider.center += new Vector3 (sizeoffset.x * targetTransform.localScale.x / -2, sizeoffset.y * targetTransform.localScale.y / -2, depth);
  53.                                         break;
  54.                                 case UIWidget.Pivot.Left:
  55.                                         cachedCollider.center += new Vector3 (sizeoffset.x * targetTransform.localScale.x / 2, 0, depth);
  56.                                         break;
  57.                                 case UIWidget.Pivot.Right:
  58.                                         cachedCollider.center += new Vector3 (sizeoffset.x * targetTransform.localScale.x / -2, 0, depth);
  59.                                         break;
  60.                                 case UIWidget.Pivot.Center:
  61.                                         cachedCollider.center += new Vector3 (cachedCollider.center.x, cachedCollider.center.y, depth);
  62.                                         break;
  63.                                 }
  64.                         }
  65.                 }
  66.         }
  67. }
  68.  

dubbreak

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 15
    • View Profile
Re: Share Time: Useful align and distribute script
« Reply #2 on: March 22, 2013, 11:09:00 AM »
Awesome, thanks for sharing.