Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Tilemaps;
- using UnityEditor;
- using UnityEngine.UI;
- using UnityEditor.Callbacks;
- namespace BaseProject
- {
- [CustomEditor(typeof(Template))]
- public class TemplateEditor : Editor
- {
- //[SerializeField] RawImage imageIcon;
- public SerializedProperty _up;
- public SerializedProperty _right;
- public SerializedProperty _down;
- public SerializedProperty _left;
- public SerializedProperty _size;
- public SerializedProperty _foreground;
- public SerializedProperty _terrain;
- public SerializedProperty _background;
- public SerializedProperty _foregroundRotation;
- public SerializedProperty _terrainRotation;
- public SerializedProperty _backgroundRotation;
- public SerializedProperty _master;
- public SerializedProperty _colliders;
- public SerializedProperty _prefabs;
- public static bool foregroundCategory;
- public static bool terrainCategory;
- public static bool backgroundCategory;
- public static bool foregroundOpen;
- public static bool terrainOpen;
- public static bool backgroundOpen;
- public static bool foregroundRotationOpen;
- public static bool terrainRotationOpen;
- public static bool backgroundRotationOpen;
- public static bool collidersOpen;
- public static bool prefabsOpen;
- public void OnEnable()
- {
- _up = serializedObject.FindProperty("up");
- _right = serializedObject.FindProperty("right");
- _down = serializedObject.FindProperty("down");
- _left = serializedObject.FindProperty("left");
- _size = serializedObject.FindProperty("size");
- _foreground = serializedObject.FindProperty("foreground");
- _terrain = serializedObject.FindProperty("terrain");
- _background = serializedObject.FindProperty("background");
- _foregroundRotation = serializedObject.FindProperty("foregroundRotation");
- _terrainRotation = serializedObject.FindProperty("terrainRotation");
- _backgroundRotation = serializedObject.FindProperty("backgroundRotation");
- _master = serializedObject.FindProperty("master");
- _colliders = serializedObject.FindProperty("colliders");
- _prefabs = serializedObject.FindProperty("prefabs");
- }
- public override void OnInspectorGUI()
- {
- /*GUI.enabled = false;
- MonoScript script = MonoScript.FromMonoBehaviour(target as MonoBehaviour);
- script = EditorGUILayout.ObjectField(script, typeof(MonoScript), false) as MonoScript;
- GUI.enabled = true;*/
- Tools.current = UnityEditor.Tool.View;
- Template template = target as Template;
- GUIStyle loadButton = new GUIStyle(GUI.skin.button);
- loadButton.fontSize = 18;
- loadButton.fontStyle = FontStyle.Bold;
- loadButton.margin = new RectOffset(0, 0, 6, 6);
- if (GUILayout.Button(new GUIContent("Load Template", "Load this template"), loadButton))
- LoadTemplate();
- if (GUILayout.Button(new GUIContent("Preview Template", "Preview the template in a new window")))
- PreviewTemplate();
- template.master = EditorGUILayout.ObjectField(template.master, typeof(TemplateMaster), false) as TemplateMaster;
- template.size = EditorGUILayout.Vector2IntField(new GUIContent("Template Size"), template.size);
- EditorGUILayout.LabelField("Template Connections");
- int spacer = 40;
- GUILayout.BeginVertical();
- GUILayout.BeginHorizontal();
- GUILayout.Space(spacer);
- template.up = EditorGUILayout.ToggleLeft("Up", template.up);
- GUILayout.EndHorizontal();
- GUILayout.BeginHorizontal();
- template.left = EditorGUILayout.ToggleLeft("Left", template.left, GUILayout.Width(60));
- template.right = EditorGUILayout.ToggleLeft("Right", template.right, GUILayout.Width(60));
- GUILayout.EndHorizontal();
- GUILayout.BeginHorizontal();
- GUILayout.Space(spacer);
- template.down = EditorGUILayout.ToggleLeft("Down", template.down);
- GUILayout.EndHorizontal();
- GUILayout.EndVertical();
- foregroundCategory = EditorGUILayout.Foldout(foregroundCategory, "Foreground");
- if (foregroundCategory)
- {
- EditorGUI.indentLevel++;
- foregroundOpen = EditorGUILayout.Foldout(foregroundOpen, "Tiles");
- if (foregroundOpen)
- {
- EditorGUI.indentLevel++;
- for (int i = 0; i < template.foreground.Length; i++)
- {
- template.foreground[i] = EditorGUILayout.ObjectField("Tile " + i, template.foreground[i], typeof(TileBase), true) as TileBase;
- }
- EditorGUI.indentLevel--;
- }
- EditorGUILayout.LabelField("TODO: Foreground Rotation");
- //TODO: Matrix4x4
- /*foregroundRotationOpen = EditorGUILayout.Foldout(foregroundRotationOpen, "Foreground Rotation");
- if (foregroundRotationOpen)
- {
- //EditorGUI.BeginChangeCheck();
- //EditorGUILayout.PropertyField(_foreground);
- //if (EditorGUI.EndChangeCheck())
- // serializedObject.ApplyModifiedProperties();
- for (int i = 0; i < template.foregroundRotation.Length; i++)
- {
- EditorGUI.BeginChangeCheck();
- EditorGUILayout.PropertyField(_foreground);
- if (EditorGUI.EndChangeCheck())
- serializedObject.ApplyModifiedProperties();
- //template.foregroundRotation[i] = EditorGUILayout.Vector4Field("Matrix4x4 " + i, template.foregroundRotation[i]);
- //template.foregroundRotation[i] = EditorGUILayout.ObjectField("Tile " + i.ToString(), template.foregroundRotation[i], typeof(Matrix4x4), true) as Matrix4x4;
- }
- }*/
- EditorGUI.indentLevel--;
- }
- terrainCategory = EditorGUILayout.Foldout(terrainCategory, "Terrain");
- if (terrainCategory)
- {
- EditorGUI.indentLevel++;
- terrainOpen = EditorGUILayout.Foldout(terrainOpen, "Tiles");
- if (terrainOpen)
- {
- EditorGUI.indentLevel++;
- for (int i = 0; i < template.foreground.Length; i++)
- {
- template.terrain[i] = EditorGUILayout.ObjectField("Tile " + i, template.terrain[i], typeof(TileBase), true) as TileBase;
- }
- EditorGUI.indentLevel--;
- }
- EditorGUILayout.LabelField("TODO: Terrain Rotation");
- EditorGUI.indentLevel--;
- }
- backgroundCategory = EditorGUILayout.Foldout(backgroundCategory, "Background");
- if (backgroundCategory)
- {
- EditorGUI.indentLevel++;
- backgroundOpen = EditorGUILayout.Foldout(backgroundOpen, "Tiles");
- if (backgroundOpen)
- {
- EditorGUI.indentLevel++;
- for (int i = 0; i < template.foreground.Length; i++)
- {
- template.background[i] = EditorGUILayout.ObjectField("Tile " + i, template.background[i], typeof(TileBase), true) as TileBase;
- }
- EditorGUI.indentLevel--;
- }
- EditorGUILayout.LabelField("TODO: Background Rotation");
- EditorGUI.indentLevel--;
- }
- EditorGUILayout.LabelField("TODO: TemplateColliderData");
- EditorGUILayout.LabelField("TODO: TemplatePrefabData");
- //TODO: TemplateColliderData
- //TODO: TemplatePrefabData
- }
- public void LoadTemplate()
- {
- }
- public void PreviewTemplate()
- {
- }
- /*[DidReloadScripts]
- static void GizmoIconUtility()
- {
- EditorApplication.projectWindowItemOnGUI = ItemOnGUI;
- }
- static void ItemOnGUI(string guid, Rect rect)
- {
- string assetPath = AssetDatabase.GUIDToAssetPath(guid);
- Template obj = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Template)) as Template;
- if (obj != null)
- {
- rect.width = rect.height;
- if (obj.GetInstanceID() == 1)
- {
- GUI.DrawTexture(rect, (Texture2D)AssetDatabase.LoadAssetAtPath("Assets/Base/BaseProject/Textures/Editor/Template Icon.png", typeof(Texture2D)));
- }
- else if (obj.GetInstanceID() == 2)
- {
- GUI.DrawTexture(rect, (Texture2D)AssetDatabase.LoadAssetAtPath("Assets/Gizmos/MyScriptableObject_id-2.png", typeof(Texture2D)));
- }
- }
- }*/
- }
- [CustomPropertyDrawer(typeof(Matrix4x4))]
- public class MatrixPropertyDrawer : PropertyDrawer
- {
- const float CELL_HEIGHT = 16;
- Rect position;
- SerializedProperty property;
- GUIContent label;
- // Draw the property inside the given rect
- public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent lab)
- {
- position = pos;
- property = prop;
- label = lab;
- // Using BeginProperty / EndProperty on the parent property means that
- // prefab override logic works on the entire property.
- EditorGUI.BeginProperty(position, label, property);
- // Draw label
- position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
- // Don't make child fields be indented
- var indent = EditorGUI.indentLevel;
- EditorGUI.indentLevel = 0;
- Matrix4x4 matrix = Matrix4x4.identity;
- for (int r = 0; r < 4; r++)
- {
- for (int c = 0; c < 4; c++)
- {
- DrawCell(c, r);
- matrix[r, c] = property.FindPropertyRelative("e" + r + c).floatValue;
- }
- }
- pos.y += 5 * CELL_HEIGHT;
- Vector3 translation = matrix.GetColumn(3);
- Quaternion rotation = Quaternion.LookRotation(
- matrix.GetColumn(2),
- matrix.GetColumn(1)
- );
- Vector3 scale = new Vector3(
- matrix.GetColumn(0).magnitude,
- matrix.GetColumn(1).magnitude,
- matrix.GetColumn(2).magnitude
- );
- bool wasEnabled = GUI.enabled;
- GUI.enabled = false;
- position = EditorGUI.PrefixLabel(pos, GUIUtility.GetControlID(FocusType.Passive), new GUIContent("Translation"));
- translation = EditorGUI.Vector3Field(position, "", translation);
- pos.y += CELL_HEIGHT;
- position = EditorGUI.PrefixLabel(pos, GUIUtility.GetControlID(FocusType.Passive), new GUIContent("Rotation (Euler)"));
- rotation.eulerAngles = EditorGUI.Vector3Field(position, "", rotation.eulerAngles);
- pos.y += CELL_HEIGHT;
- position = EditorGUI.PrefixLabel(pos, GUIUtility.GetControlID(FocusType.Passive), new GUIContent("Rotation (Quaternion)"));
- rotation = Vector4ToQuaternion(EditorGUI.Vector4Field(position, "", QuaternionToVector4(rotation)));
- pos.y += CELL_HEIGHT;
- position = EditorGUI.PrefixLabel(pos, GUIUtility.GetControlID(FocusType.Passive), new GUIContent("Scale"));
- scale = EditorGUI.Vector3Field(position, "", scale);
- pos.y += CELL_HEIGHT;
- GUI.enabled = wasEnabled;
- if (GUI.Button(pos, "From Camera"))
- {
- matrix = Camera.main.projectionMatrix;
- for (int r = 0; r < 4; r++)
- {
- for (int c = 0; c < 4; c++)
- {
- property.FindPropertyRelative("e" + r + c).floatValue = matrix[r, c];
- }
- }
- }
- // Set indent back to what it was
- EditorGUI.indentLevel = indent;
- EditorGUI.EndProperty();
- }
- void DrawCell(int column, int row)
- {
- Vector2 cellPos = position.position;
- cellPos.x += position.width * column / 4;
- cellPos.y += CELL_HEIGHT * row;
- EditorGUI.PropertyField(
- new Rect(cellPos, new Vector2(position.width / 4, CELL_HEIGHT)),
- property.FindPropertyRelative("e" + row + column),
- GUIContent.none
- );
- }
- static Vector4 QuaternionToVector4(Quaternion rot)
- {
- return new Vector4(rot.x, rot.y, rot.z, rot.w);
- }
- static Quaternion Vector4ToQuaternion(Vector4 v)
- {
- return new Quaternion(v.x, v.y, v.z, v.w);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment