Guest User

TemplateEditor

a guest
May 12th, 2021
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.55 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Tilemaps;
  5. using UnityEditor;
  6. using UnityEngine.UI;
  7. using UnityEditor.Callbacks;
  8.  
  9. namespace BaseProject
  10. {
  11.     [CustomEditor(typeof(Template))]
  12.     public class TemplateEditor : Editor
  13.     {
  14.         //[SerializeField] RawImage imageIcon;
  15.  
  16.         public SerializedProperty _up;
  17.         public SerializedProperty _right;
  18.         public SerializedProperty _down;
  19.         public SerializedProperty _left;
  20.  
  21.         public SerializedProperty _size;
  22.  
  23.         public SerializedProperty _foreground;
  24.         public SerializedProperty _terrain;
  25.         public SerializedProperty _background;
  26.         public SerializedProperty _foregroundRotation;
  27.         public SerializedProperty _terrainRotation;
  28.         public SerializedProperty _backgroundRotation;
  29.  
  30.         public SerializedProperty _master;
  31.         public SerializedProperty _colliders;
  32.         public SerializedProperty _prefabs;
  33.  
  34.         public static bool foregroundCategory;
  35.         public static bool terrainCategory;
  36.         public static bool backgroundCategory;
  37.  
  38.         public static bool foregroundOpen;
  39.         public static bool terrainOpen;
  40.         public static bool backgroundOpen;
  41.         public static bool foregroundRotationOpen;
  42.         public static bool terrainRotationOpen;
  43.         public static bool backgroundRotationOpen;
  44.  
  45.         public static bool collidersOpen;
  46.         public static bool prefabsOpen;
  47.  
  48.         public void OnEnable()
  49.         {
  50.             _up = serializedObject.FindProperty("up");
  51.             _right = serializedObject.FindProperty("right");
  52.             _down = serializedObject.FindProperty("down");
  53.             _left = serializedObject.FindProperty("left");
  54.  
  55.             _size = serializedObject.FindProperty("size");
  56.  
  57.             _foreground = serializedObject.FindProperty("foreground");
  58.             _terrain = serializedObject.FindProperty("terrain");
  59.             _background = serializedObject.FindProperty("background");
  60.             _foregroundRotation = serializedObject.FindProperty("foregroundRotation");
  61.             _terrainRotation = serializedObject.FindProperty("terrainRotation");
  62.             _backgroundRotation = serializedObject.FindProperty("backgroundRotation");
  63.  
  64.             _master = serializedObject.FindProperty("master");
  65.             _colliders = serializedObject.FindProperty("colliders");
  66.             _prefabs = serializedObject.FindProperty("prefabs");
  67.         }
  68.  
  69.         public override void OnInspectorGUI()
  70.         {
  71.             /*GUI.enabled = false;
  72.             MonoScript script = MonoScript.FromMonoBehaviour(target as MonoBehaviour);
  73.             script = EditorGUILayout.ObjectField(script, typeof(MonoScript), false) as MonoScript;
  74.             GUI.enabled = true;*/
  75.  
  76.             Tools.current = UnityEditor.Tool.View;
  77.             Template template = target as Template;
  78.  
  79.             GUIStyle loadButton = new GUIStyle(GUI.skin.button);
  80.             loadButton.fontSize = 18;
  81.             loadButton.fontStyle = FontStyle.Bold;
  82.             loadButton.margin = new RectOffset(0, 0, 6, 6);
  83.             if (GUILayout.Button(new GUIContent("Load Template", "Load this template"), loadButton))
  84.                 LoadTemplate();
  85.  
  86.             if (GUILayout.Button(new GUIContent("Preview Template", "Preview the template in a new window")))
  87.                 PreviewTemplate();
  88.  
  89.             template.master = EditorGUILayout.ObjectField(template.master, typeof(TemplateMaster), false) as TemplateMaster;
  90.             template.size = EditorGUILayout.Vector2IntField(new GUIContent("Template Size"), template.size);
  91.  
  92.             EditorGUILayout.LabelField("Template Connections");
  93.  
  94.             int spacer = 40;
  95.             GUILayout.BeginVertical();
  96.  
  97.             GUILayout.BeginHorizontal();
  98.             GUILayout.Space(spacer);
  99.             template.up = EditorGUILayout.ToggleLeft("Up", template.up);
  100.             GUILayout.EndHorizontal();
  101.  
  102.             GUILayout.BeginHorizontal();
  103.             template.left = EditorGUILayout.ToggleLeft("Left", template.left, GUILayout.Width(60));
  104.             template.right = EditorGUILayout.ToggleLeft("Right", template.right, GUILayout.Width(60));
  105.             GUILayout.EndHorizontal();
  106.  
  107.             GUILayout.BeginHorizontal();
  108.             GUILayout.Space(spacer);
  109.             template.down = EditorGUILayout.ToggleLeft("Down", template.down);
  110.             GUILayout.EndHorizontal();
  111.  
  112.             GUILayout.EndVertical();
  113.  
  114.             foregroundCategory = EditorGUILayout.Foldout(foregroundCategory, "Foreground");
  115.             if (foregroundCategory)
  116.             {
  117.                 EditorGUI.indentLevel++;
  118.                 foregroundOpen = EditorGUILayout.Foldout(foregroundOpen, "Tiles");
  119.                 if (foregroundOpen)
  120.                 {
  121.                     EditorGUI.indentLevel++;
  122.                     for (int i = 0; i < template.foreground.Length; i++)
  123.                     {
  124.                         template.foreground[i] = EditorGUILayout.ObjectField("Tile " + i, template.foreground[i], typeof(TileBase), true) as TileBase;
  125.                     }
  126.                     EditorGUI.indentLevel--;
  127.                 }
  128.  
  129.                 EditorGUILayout.LabelField("TODO: Foreground Rotation");
  130.  
  131.                 //TODO: Matrix4x4
  132.  
  133.                 /*foregroundRotationOpen = EditorGUILayout.Foldout(foregroundRotationOpen, "Foreground Rotation");
  134.                 if (foregroundRotationOpen)
  135.                 {
  136.                     //EditorGUI.BeginChangeCheck();
  137.                     //EditorGUILayout.PropertyField(_foreground);
  138.                     //if (EditorGUI.EndChangeCheck())
  139.                     //    serializedObject.ApplyModifiedProperties();
  140.  
  141.                     for (int i = 0; i < template.foregroundRotation.Length; i++)
  142.                     {
  143.                         EditorGUI.BeginChangeCheck();
  144.                         EditorGUILayout.PropertyField(_foreground);
  145.                         if (EditorGUI.EndChangeCheck())
  146.                             serializedObject.ApplyModifiedProperties();
  147.                         //template.foregroundRotation[i] = EditorGUILayout.Vector4Field("Matrix4x4 " + i, template.foregroundRotation[i]);
  148.                         //template.foregroundRotation[i] = EditorGUILayout.ObjectField("Tile " + i.ToString(), template.foregroundRotation[i], typeof(Matrix4x4), true) as Matrix4x4;
  149.                     }
  150.                 }*/
  151.                 EditorGUI.indentLevel--;
  152.             }
  153.  
  154.             terrainCategory = EditorGUILayout.Foldout(terrainCategory, "Terrain");
  155.             if (terrainCategory)
  156.             {
  157.                 EditorGUI.indentLevel++;
  158.                 terrainOpen = EditorGUILayout.Foldout(terrainOpen, "Tiles");
  159.                 if (terrainOpen)
  160.                 {
  161.                     EditorGUI.indentLevel++;
  162.                     for (int i = 0; i < template.foreground.Length; i++)
  163.                     {
  164.                         template.terrain[i] = EditorGUILayout.ObjectField("Tile " + i, template.terrain[i], typeof(TileBase), true) as TileBase;
  165.                     }
  166.                     EditorGUI.indentLevel--;
  167.                 }
  168.  
  169.                 EditorGUILayout.LabelField("TODO: Terrain Rotation");
  170.                 EditorGUI.indentLevel--;
  171.             }
  172.  
  173.             backgroundCategory = EditorGUILayout.Foldout(backgroundCategory, "Background");
  174.             if (backgroundCategory)
  175.             {
  176.                 EditorGUI.indentLevel++;
  177.                 backgroundOpen = EditorGUILayout.Foldout(backgroundOpen, "Tiles");
  178.                 if (backgroundOpen)
  179.                 {
  180.                     EditorGUI.indentLevel++;
  181.                     for (int i = 0; i < template.foreground.Length; i++)
  182.                     {
  183.                         template.background[i] = EditorGUILayout.ObjectField("Tile " + i, template.background[i], typeof(TileBase), true) as TileBase;
  184.                     }
  185.                     EditorGUI.indentLevel--;
  186.                 }
  187.                 EditorGUILayout.LabelField("TODO: Background Rotation");
  188.                 EditorGUI.indentLevel--;
  189.             }
  190.  
  191.             EditorGUILayout.LabelField("TODO: TemplateColliderData");
  192.             EditorGUILayout.LabelField("TODO: TemplatePrefabData");
  193.  
  194.             //TODO: TemplateColliderData
  195.             //TODO: TemplatePrefabData
  196.         }
  197.  
  198.         public void LoadTemplate()
  199.         {
  200.  
  201.         }
  202.  
  203.         public void PreviewTemplate()
  204.         {
  205.  
  206.         }
  207.  
  208.         /*[DidReloadScripts]
  209.         static void GizmoIconUtility()
  210.         {
  211.             EditorApplication.projectWindowItemOnGUI = ItemOnGUI;
  212.         }
  213.  
  214.         static void ItemOnGUI(string guid, Rect rect)
  215.         {
  216.             string assetPath = AssetDatabase.GUIDToAssetPath(guid);
  217.  
  218.             Template obj = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Template)) as Template;
  219.  
  220.             if (obj != null)
  221.             {
  222.                 rect.width = rect.height;
  223.  
  224.                 if (obj.GetInstanceID() == 1)
  225.                 {
  226.                     GUI.DrawTexture(rect, (Texture2D)AssetDatabase.LoadAssetAtPath("Assets/Base/BaseProject/Textures/Editor/Template Icon.png", typeof(Texture2D)));
  227.                 }
  228.                 else if (obj.GetInstanceID() == 2)
  229.                 {
  230.                     GUI.DrawTexture(rect, (Texture2D)AssetDatabase.LoadAssetAtPath("Assets/Gizmos/MyScriptableObject_id-2.png", typeof(Texture2D)));
  231.                 }
  232.             }
  233.         }*/
  234.     }
  235.  
  236.     [CustomPropertyDrawer(typeof(Matrix4x4))]
  237.     public class MatrixPropertyDrawer : PropertyDrawer
  238.     {
  239.         const float CELL_HEIGHT = 16;
  240.  
  241.         Rect position;
  242.         SerializedProperty property;
  243.         GUIContent label;
  244.  
  245.         // Draw the property inside the given rect
  246.         public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent lab)
  247.         {
  248.             position = pos;
  249.             property = prop;
  250.             label = lab;
  251.  
  252.             // Using BeginProperty / EndProperty on the parent property means that
  253.             // prefab override logic works on the entire property.
  254.             EditorGUI.BeginProperty(position, label, property);
  255.  
  256.             // Draw label
  257.             position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
  258.  
  259.             // Don't make child fields be indented
  260.             var indent = EditorGUI.indentLevel;
  261.             EditorGUI.indentLevel = 0;
  262.  
  263.             Matrix4x4 matrix = Matrix4x4.identity;
  264.  
  265.             for (int r = 0; r < 4; r++)
  266.             {
  267.                 for (int c = 0; c < 4; c++)
  268.                 {
  269.                     DrawCell(c, r);
  270.                     matrix[r, c] = property.FindPropertyRelative("e" + r + c).floatValue;
  271.                 }
  272.             }
  273.  
  274.             pos.y += 5 * CELL_HEIGHT;
  275.  
  276.             Vector3 translation = matrix.GetColumn(3);
  277.  
  278.             Quaternion rotation = Quaternion.LookRotation(
  279.                 matrix.GetColumn(2),
  280.                 matrix.GetColumn(1)
  281.             );
  282.  
  283.             Vector3 scale = new Vector3(
  284.                 matrix.GetColumn(0).magnitude,
  285.                 matrix.GetColumn(1).magnitude,
  286.                 matrix.GetColumn(2).magnitude
  287.             );
  288.  
  289.             bool wasEnabled = GUI.enabled;
  290.             GUI.enabled = false;
  291.             position = EditorGUI.PrefixLabel(pos, GUIUtility.GetControlID(FocusType.Passive), new GUIContent("Translation"));
  292.             translation = EditorGUI.Vector3Field(position, "", translation);
  293.             pos.y += CELL_HEIGHT;
  294.  
  295.             position = EditorGUI.PrefixLabel(pos, GUIUtility.GetControlID(FocusType.Passive), new GUIContent("Rotation (Euler)"));
  296.             rotation.eulerAngles = EditorGUI.Vector3Field(position, "", rotation.eulerAngles);
  297.             pos.y += CELL_HEIGHT;
  298.  
  299.             position = EditorGUI.PrefixLabel(pos, GUIUtility.GetControlID(FocusType.Passive), new GUIContent("Rotation (Quaternion)"));
  300.             rotation = Vector4ToQuaternion(EditorGUI.Vector4Field(position, "", QuaternionToVector4(rotation)));
  301.             pos.y += CELL_HEIGHT;
  302.  
  303.             position = EditorGUI.PrefixLabel(pos, GUIUtility.GetControlID(FocusType.Passive), new GUIContent("Scale"));
  304.             scale = EditorGUI.Vector3Field(position, "", scale);
  305.             pos.y += CELL_HEIGHT;
  306.             GUI.enabled = wasEnabled;
  307.  
  308.  
  309.             if (GUI.Button(pos, "From Camera"))
  310.             {
  311.                 matrix = Camera.main.projectionMatrix;
  312.                 for (int r = 0; r < 4; r++)
  313.                 {
  314.                     for (int c = 0; c < 4; c++)
  315.                     {
  316.                         property.FindPropertyRelative("e" + r + c).floatValue = matrix[r, c];
  317.                     }
  318.                 }
  319.  
  320.             }
  321.  
  322.             // Set indent back to what it was
  323.             EditorGUI.indentLevel = indent;
  324.  
  325.             EditorGUI.EndProperty();
  326.         }
  327.  
  328.         void DrawCell(int column, int row)
  329.         {
  330.             Vector2 cellPos = position.position;
  331.             cellPos.x += position.width * column / 4;
  332.             cellPos.y += CELL_HEIGHT * row;
  333.             EditorGUI.PropertyField(
  334.                 new Rect(cellPos, new Vector2(position.width / 4, CELL_HEIGHT)),
  335.                 property.FindPropertyRelative("e" + row + column),
  336.                 GUIContent.none
  337.             );
  338.         }
  339.  
  340.         static Vector4 QuaternionToVector4(Quaternion rot)
  341.         {
  342.             return new Vector4(rot.x, rot.y, rot.z, rot.w);
  343.         }
  344.  
  345.         static Quaternion Vector4ToQuaternion(Vector4 v)
  346.         {
  347.             return new Quaternion(v.x, v.y, v.z, v.w);
  348.         }
  349.  
  350.     }
  351. }
Advertisement
Add Comment
Please, Sign In to add comment