Skip to content

Instantly share code, notes, and snippets.

@flycarl
Created February 24, 2017 08:01
Show Gist options
  • Save flycarl/6b1318f99c3bc9bce5e8cc038974b61c to your computer and use it in GitHub Desktop.
Save flycarl/6b1318f99c3bc9bce5e8cc038974b61c to your computer and use it in GitHub Desktop.
Unity Editor Script to make items in line
using System.IO;
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
public class MakeItemsLine : EditorWindow
{
private Vector3 m_startPos;
private float m_unitLength;
[MenuItem("GameEditor/make items line")]
public static void ShowWindow()
{
EditorWindow.GetWindow(typeof(MakeItemsLine));
}
void OnGUI()
{
EditorGUILayout.BeginVertical();
m_startPos = EditorGUILayout.Vector3Field("start pos", m_startPos);
m_unitLength = EditorGUILayout.FloatField("unit length ", m_unitLength);
EditorGUILayout.Space();
if (GUILayout.Button("make selections along - Line", GUILayout.Height(20)))
{
makeLine(Selection.gameObjects);
}
if (GUILayout.Button("make selections along | Line", GUILayout.Height(20)))
{
makeVLine(Selection.gameObjects);
}
if (GUILayout.Button("set Start Position", GUILayout.Height(20)))
{
setStartPos(Selection.gameObjects);
}
if (GUILayout.Button("set Unit Length", GUILayout.Height(20)))
{
setUnitLength(Selection.gameObjects);
}
EditorGUILayout.EndVertical();
}
void makeLine(GameObject[] gameObjcects)
{
int count = gameObjcects.Length;
Vector3 pos = Vector3.zero;
for (int i = 0; i < count; i++)
{
GameObject obj = gameObjcects[i];
pos.x = m_unitLength * i;
obj.transform.localPosition = pos + m_startPos;
}
}
void makeVLine(GameObject[] gameObjcects)
{
int count = gameObjcects.Length;
Vector3 pos = Vector3.zero;
for (int i = 0; i < count; i++)
{
GameObject obj = gameObjcects[i];
pos.y = m_unitLength * i;
obj.transform.localPosition = pos + m_startPos;
}
}
void setStartPos(GameObject[] gameObjcects)
{
int count = gameObjcects.Length;
Vector3 minXPos = Vector3.zero;
if (count > 0)
minXPos = gameObjcects[0].transform.localPosition;
for (int i = 0; i < count; i++)
{
GameObject obj = gameObjcects[i];
if(obj.transform.localPosition.x < minXPos.x)
minXPos = gameObjcects[i].transform.localPosition;
}
m_startPos = minXPos;
}
void setUnitLength(GameObject[] gameObjcects)
{
int count = gameObjcects.Length;
if (count == 2)
{
m_unitLength = Mathf.Abs(gameObjcects[1].transform.localPosition.x - gameObjcects[0].transform.localPosition.x);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment