Unity Object Spacing Script – UPDATE(Fixed Broken Script)

This has saved me so much time lining up GUI elements for my TD game. It takes a user selection then allows them to define their order and the spacing between them. I used them for the level selection screen and am finding use for this in many of my projects.

Feel free to use this wherever you see fit.

Usage:

*Place in Editor folder.
*Select object you wish to space.
*Goto Helper/Space Objects
*Select Axis
*Enter float value to define the gap between objects.
*Hit Apply!


using UnityEngine;
using UnityEditor;
using System.Collections;

public class SetSpacingOnSelection : EditorWindow 
{
    
    public enum Axis
    {
        x,
        y, 
        z,
    }
    public Axis selectedAxis; 
    public float spaceAmount; 
    public static GameObject[] selectedObjects;
    
    [MenuItem("Helper/Space Objects")]
    
     static void Init()
    {
        SetSpacingOnSelection window =
                (SetSpacingOnSelection)EditorWindow.GetWindow
                 (typeof(SetSpacingOnSelection));
        selectedObjects = Selection.gameObjects;
        
    }
    
    void GetSelection()
    {
    }
    void OnSelectionChange()
    {
        selectedObjects = Selection.gameObjects;
    }
    void OnGUI()
    {
        if(selectedObjects != null)
        {
                    GUILayout.Label("Space Items", EditorStyles.boldLabel);
        GUIContent axisLabel  = new GUIContent("Axis: ");
        selectedAxis = (Axis)EditorGUILayout.EnumPopup(axisLabel,selectedAxis);
        GUIContent spaceLabel = new GUIContent("Amount: ");
        spaceAmount = EditorGUILayout.FloatField(spaceLabel, spaceAmount); 
    

        for(int x = 0; x < selectedObjects.Length; x++)
            {
                    EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField(selectedObjects[x].name);
                if(x >0)
                {
                                    if(GUILayout.Button ("UP"))
                {
                    GameObject temp = selectedObjects[x];
                    selectedObjects[x] = selectedObjects[x-1];
                    selectedObjects[x-1] = temp;
                }
                }

        if(x < selectedObjects.Length -1)
            {
                if(GUILayout.Button ("DOWN"))
                {
                    GameObject temp = selectedObjects[x];
                    selectedObjects[x] = selectedObjects[x+1];
                    selectedObjects[x+1] = temp;
                }
            }
                            EditorGUILayout.EndHorizontal();
    
        }
        
        
        GameObject[] objects = selectedObjects;
        
        if(GUILayout.Button("Apply"))
        { 
            for(int i = 1; i < objects.Length; i++)
            {
                Vector3 newPos = objects[i].transform.position; 
                switch(selectedAxis)
                {
                case Axis.x:
                    newPos.x = objects[i-1].transform.position.x + spaceAmount;
                    break;
                case Axis.y: 
                    newPos.y = objects[i-1].transform.position.y + spaceAmount; 
                    break; 
                case Axis.z:
                    newPos.z = objects[i-1].transform.position.z + spaceAmount;
                    break; 
                }
                objects[i].transform.position = newPos; 
            }
        }
        }
    }
    
}

Created with this script:
Level Select

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s