Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
3.0 KiB

  1. namespace VRTK
  2. {
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System;
  6. using System.Reflection;
  7. public static class VRTK_EditorUtilities
  8. {
  9. public static TooltipAttribute GetTooltipAttribute(FieldInfo fieldInfo)
  10. {
  11. return (TooltipAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(TooltipAttribute));
  12. }
  13. public static GUIContent BuildGUIContent<T>(string fieldName, string displayOverride = null)
  14. {
  15. string displayName = (displayOverride != null ? displayOverride : ObjectNames.NicifyVariableName(fieldName));
  16. FieldInfo fieldInfo = typeof(T).GetField(fieldName);
  17. TooltipAttribute tooltipAttribute = GetTooltipAttribute(fieldInfo);
  18. return (tooltipAttribute == null ? new GUIContent(displayName) : new GUIContent(displayName, tooltipAttribute.tooltip));
  19. }
  20. public static void AddHeader<T>(string fieldName, string displayOverride = null)
  21. {
  22. string displayName = (displayOverride != null ? displayOverride : ObjectNames.NicifyVariableName(fieldName));
  23. FieldInfo fieldInfo = typeof(T).GetField(fieldName);
  24. HeaderAttribute headerAttribute = (HeaderAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(HeaderAttribute));
  25. AddHeader(headerAttribute == null ? displayName : headerAttribute.header);
  26. }
  27. public static void AddHeader(string header, bool spaceBeforeHeader = true)
  28. {
  29. if (spaceBeforeHeader)
  30. {
  31. EditorGUILayout.Space();
  32. }
  33. EditorGUILayout.LabelField(header, EditorStyles.boldLabel);
  34. }
  35. public static void DrawUsingDestructiveStyle(GUIStyle styleToCopy, Action<GUIStyle> drawAction)
  36. {
  37. Color previousBackgroundColor = GUI.backgroundColor;
  38. GUIStyle destructiveButtonStyle = new GUIStyle(styleToCopy)
  39. {
  40. normal =
  41. {
  42. textColor = Color.white
  43. },
  44. active =
  45. {
  46. textColor = Color.white
  47. }
  48. };
  49. GUI.backgroundColor = Color.red;
  50. drawAction(destructiveButtonStyle);
  51. GUI.backgroundColor = previousBackgroundColor;
  52. }
  53. public static void DrawScrollableSelectableLabel(ref Vector2 scrollPosition, ref float width, string text, GUIStyle style)
  54. {
  55. using (EditorGUILayout.ScrollViewScope scrollViewScope = new EditorGUILayout.ScrollViewScope(scrollPosition))
  56. {
  57. scrollPosition = scrollViewScope.scrollPosition;
  58. float textHeight = style.CalcHeight(new GUIContent(text), width);
  59. EditorGUILayout.SelectableLabel(text, style, GUILayout.MinHeight(textHeight));
  60. if (Event.current.type == EventType.Repaint)
  61. {
  62. width = GUILayoutUtility.GetLastRect().width;
  63. }
  64. }
  65. }
  66. }
  67. }