62 lines
1.8 KiB
C#
62 lines
1.8 KiB
C#
using UnityEngine;
|
|
using UnityEditor;
|
|
using System.Collections;
|
|
|
|
public class LevelEditor : EditorWindow
|
|
{
|
|
string myString = "Hello World";
|
|
bool groupEnabled;
|
|
bool myBool = true;
|
|
float myFloat = 1.23f;
|
|
|
|
// Add menu item named "My Window" to the Window menu
|
|
[MenuItem("Window/LevelEditor")]
|
|
public static void ShowWindow()
|
|
{
|
|
//Show existing window instance. If one doesn't exist, make one.
|
|
EditorWindow.GetWindow(typeof(LevelEditor));
|
|
}
|
|
|
|
float AlingToGrid(float value, float gridStep = 0.5f){
|
|
float div = value / gridStep;
|
|
int cellId = Mathf.FloorToInt (div);
|
|
float frac = div - (float)cellId;
|
|
return (float)cellId * gridStep + (Mathf.Abs(frac) > 0.5f ? gridStep : 0.0f);
|
|
}
|
|
|
|
Vector3 AlingToGrid(Vector3 value, float gridStep = 0.5f){
|
|
return new Vector3 (
|
|
AlingToGrid(value.x, gridStep),
|
|
AlingToGrid(value.y, gridStep),
|
|
AlingToGrid(value.z, gridStep));
|
|
}
|
|
|
|
void OnGUI()
|
|
{
|
|
GUILayout.Label ("Blocks alignment", EditorStyles.boldLabel);
|
|
if (GUILayout.Button ("Aling position to grid")) {
|
|
foreach(var obj in Selection.objects){
|
|
GameObject go = obj as GameObject;
|
|
if(go != null){
|
|
go.transform.position = AlingToGrid(go.transform.position);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (GUILayout.Button ("Aling rotation to grid")) {
|
|
foreach(var obj in Selection.objects){
|
|
GameObject go = obj as GameObject;
|
|
if(go != null){
|
|
go.transform.rotation = Quaternion.Euler(AlingToGrid(go.transform.rotation.eulerAngles, 45.0f));
|
|
}
|
|
}
|
|
}
|
|
|
|
/*myString = EditorGUILayout.TextField ("Text Field", myString);
|
|
|
|
groupEnabled = EditorGUILayout.BeginToggleGroup ("Optional Settings", groupEnabled);
|
|
myBool = EditorGUILayout.Toggle ("Toggle", myBool);
|
|
myFloat = EditorGUILayout.Slider ("Slider", myFloat, -3, 3);
|
|
EditorGUILayout.EndToggleGroup ();*/
|
|
}
|
|
} |