97 lines
2.7 KiB
C#
97 lines
2.7 KiB
C#
using UnityEngine;
|
|
using UnityEditor;
|
|
using UnityEngine.UI;
|
|
using System.Collections;
|
|
|
|
public class LevelEditor : EditorWindow
|
|
{
|
|
string myString = "Hello World";
|
|
bool groupEnabled;
|
|
bool myBool = true;
|
|
float myFloat = 1.23f;
|
|
public int pixelsToUnits = 8;
|
|
|
|
|
|
// 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));
|
|
|
|
}
|
|
|
|
public static LevelEditor GetEditor(){
|
|
return (LevelEditor)EditorWindow.GetWindow(typeof(LevelEditor), false);
|
|
}
|
|
|
|
[MenuItem ("LevelEditor/Aling position %g")]
|
|
public static void AlignStatic(){
|
|
var editor = GetEditor();
|
|
editor.AlignPositionToGrid();
|
|
}
|
|
|
|
float AlingToGrid(float value, float gridStep){
|
|
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){
|
|
return new Vector3 (
|
|
AlingToGrid(value.x, gridStep),
|
|
AlingToGrid(value.y, gridStep),
|
|
value.z);
|
|
//AlingToGrid(value.z, gridStep));
|
|
}
|
|
|
|
void AlignPositionToGrid(){
|
|
float gridStep = 1.0f / pixelsToUnits;
|
|
foreach(var obj in Selection.objects){
|
|
GameObject go = obj as GameObject;
|
|
if(go != null){
|
|
go.transform.position = AlingToGrid(go.transform.position, gridStep);
|
|
}
|
|
}
|
|
}
|
|
|
|
void OnGUI()
|
|
{
|
|
pixelsToUnits = EditorGUILayout.IntField("PixelsToUnits", pixelsToUnits);
|
|
|
|
GUILayout.Label ("Blocks alignment", EditorStyles.boldLabel);
|
|
if (GUILayout.Button ("Aling position to grid")) {
|
|
AlignPositionToGrid();
|
|
}
|
|
|
|
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));
|
|
}
|
|
}
|
|
}
|
|
|
|
if (GUILayout.Button("Add physics extension")) {
|
|
var rb = FindObjectsOfType<Rigidbody2D>();
|
|
foreach (var body in rb) {
|
|
var ex = body.GetComponent<Physics2DEx>();
|
|
if (ex == null) {
|
|
ex = body.gameObject.AddComponent<Physics2DEx>();
|
|
Debug.Log("Add PhysicsEx to " + body.name);
|
|
}
|
|
}
|
|
//rb[0].worldCenterOfMass
|
|
// rb[0].GetPointVelocity()
|
|
}
|
|
|
|
/*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 ();*/
|
|
}
|
|
} |