Guest User

Untitled

a guest
Jan 22nd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3.  
  4. [CustomPropertyDrawer(typeof(Matrix4x4))]
  5. public class MatrixPropertyDrawer : PropertyDrawer
  6. {
  7. const float CELL_HEIGHT = 16;
  8.  
  9. Rect position;
  10. SerializedProperty property;
  11. GUIContent label;
  12.  
  13. // Draw the property inside the given rect
  14. public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent lab)
  15. {
  16. position = pos;
  17. property = prop;
  18. label = lab;
  19.  
  20. // Using BeginProperty / EndProperty on the parent property means that
  21. // prefab override logic works on the entire property.
  22. EditorGUI.BeginProperty(position, label, property);
  23.  
  24. // Draw label
  25. position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
  26.  
  27. // Don't make child fields be indented
  28. var indent = EditorGUI.indentLevel;
  29. EditorGUI.indentLevel = 0;
  30.  
  31. Matrix4x4 matrix = Matrix4x4.identity;
  32.  
  33. for (int r = 0; r < 4; r++)
  34. {
  35. for (int c = 0; c < 4; c++)
  36. {
  37. DrawCell(c, r);
  38. matrix[r, c] = property.FindPropertyRelative("e" + r + c).floatValue;
  39. }
  40. }
  41.  
  42. pos.y += 5 * CELL_HEIGHT;
  43.  
  44. Vector3 translation = matrix.GetColumn(3);
  45.  
  46. Quaternion rotation = Quaternion.LookRotation(
  47. matrix.GetColumn(2),
  48. matrix.GetColumn(1)
  49. );
  50.  
  51. Vector3 scale = new Vector3(
  52. matrix.GetColumn(0).magnitude,
  53. matrix.GetColumn(1).magnitude,
  54. matrix.GetColumn(2).magnitude
  55. );
  56.  
  57. bool wasEnabled = GUI.enabled;
  58. GUI.enabled = false;
  59. position = EditorGUI.PrefixLabel(pos, GUIUtility.GetControlID(FocusType.Passive), new GUIContent("Translation"));
  60. translation = EditorGUI.Vector3Field(position, "", translation);
  61. pos.y += CELL_HEIGHT;
  62.  
  63. position = EditorGUI.PrefixLabel(pos, GUIUtility.GetControlID(FocusType.Passive), new GUIContent("Rotation (Euler)"));
  64. rotation.eulerAngles = EditorGUI.Vector3Field(position, "", rotation.eulerAngles);
  65. pos.y += CELL_HEIGHT;
  66.  
  67. position = EditorGUI.PrefixLabel(pos, GUIUtility.GetControlID(FocusType.Passive), new GUIContent("Rotation (Quaternion)"));
  68. rotation = Vector4ToQuaternion(EditorGUI.Vector4Field(position, "", QuaternionToVector4(rotation)));
  69. pos.y += CELL_HEIGHT;
  70.  
  71. position = EditorGUI.PrefixLabel(pos, GUIUtility.GetControlID(FocusType.Passive), new GUIContent("Scale"));
  72. scale = EditorGUI.Vector3Field(position, "", scale);
  73. pos.y += CELL_HEIGHT;
  74. GUI.enabled = wasEnabled;
  75.  
  76.  
  77. if (GUI.Button(pos, "From Camera"))
  78. {
  79. matrix = Camera.main.projectionMatrix;
  80. for (int r = 0; r < 4; r++)
  81. {
  82. for (int c = 0; c < 4; c++)
  83. {
  84. property.FindPropertyRelative("e" + r + c).floatValue = matrix[r, c];
  85. }
  86. }
  87.  
  88. }
  89.  
  90. // Set indent back to what it was
  91. EditorGUI.indentLevel = indent;
  92.  
  93. EditorGUI.EndProperty();
  94. }
  95.  
  96. void DrawCell(int column, int row)
  97. {
  98. Vector2 cellPos = position.position;
  99. cellPos.x += position.width * column / 4;
  100. cellPos.y += CELL_HEIGHT * row;
  101. EditorGUI.PropertyField(
  102. new Rect(cellPos, new Vector2(position.width / 4, CELL_HEIGHT)),
  103. property.FindPropertyRelative("e" + row + column),
  104. GUIContent.none
  105. );
  106. }
  107.  
  108. static Vector4 QuaternionToVector4(Quaternion rot)
  109. {
  110. return new Vector4(rot.x, rot.y, rot.z, rot.w);
  111. }
  112.  
  113. static Quaternion Vector4ToQuaternion(Vector4 v)
  114. {
  115. return new Quaternion(v.x, v.y, v.z, v.w);
  116. }
  117.  
  118. }
Add Comment
Please, Sign In to add comment