Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using Unity.Mathematics;
  6. using UnityEngine;
  7. using Unity.Physics.Authoring;
  8. #if UNITY_EDITOR
  9. using UnityEditor;
  10. #endif
  11.  
  12. [Serializable]
  13. public struct PhysicsCategoryMask
  14. {
  15. public uint Value;
  16.  
  17. public static implicit operator uint(PhysicsCategoryMask mask) => mask.Value;
  18. }
  19.  
  20. #if UNITY_EDITOR
  21. [CustomPropertyDrawer(typeof(PhysicsCategoryMask))]
  22. public class PhysicsCategoryMaskDrawer : PropertyDrawer
  23. {
  24. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  25. {
  26. var assets = GetAssets<PhysicsCategoryNames>();
  27. var asset = assets.First();
  28. var names = asset.CategoryNames.Where(name => !string.IsNullOrEmpty(name)).ToArray();
  29.  
  30. label = EditorGUI.BeginProperty(position, label, property);
  31. var controlPosition = EditorGUI.PrefixLabel(position, label);
  32.  
  33. EditorGUI.indentLevel = 0;
  34.  
  35. var valueProperty = property.FindPropertyRelative(nameof(PhysicsCategoryMask.Value));
  36. if (valueProperty == null)
  37. throw new InvalidOperationException();
  38.  
  39. var value = valueProperty.intValue;
  40.  
  41. // MaskField wants 'Everything' represented as -1,
  42. if (IsEverythingSet(value, names.Length))
  43. {
  44. value = ~0;
  45. }
  46.  
  47. EditorGUI.BeginChangeCheck();
  48. var maskField = EditorGUI.MaskField(controlPosition, GUIContent.none, value, names);
  49. if (EditorGUI.EndChangeCheck())
  50. {
  51. if (maskField == -1)
  52. {
  53. // Calculate the cumulative value.
  54. var everything = 0;
  55. for (int i = 0, count = names.Length; i < count; ++i)
  56. {
  57. everything |= 1 << i;
  58. }
  59.  
  60. maskField = everything;
  61. }
  62.  
  63. valueProperty.intValue = maskField;
  64. }
  65.  
  66. EditorGUI.EndProperty();
  67. }
  68.  
  69. public static List<T> GetAssets<T>() where T : ScriptableObject
  70. {
  71. return AssetDatabase.FindAssets($"t:{typeof(T).Name}")
  72. .Select(AssetDatabase.GUIDToAssetPath)
  73. .Select(AssetDatabase.LoadAssetAtPath<T>)
  74. .Where(c => c != null).ToList();
  75. }
  76.  
  77. public static bool IsEverythingSet(int maskValue, int length)
  78. {
  79. var value = 0;
  80. var everything = 0;
  81. for (int i = 1, count = length; i < count; ++i)
  82. {
  83. var isSelected = (maskValue & (1 << i)) != 0;
  84. value |= isSelected ? 1 << i : 0;
  85. everything |= 1 << i;
  86. }
  87.  
  88. return value == everything;
  89. }
  90.  
  91. }
  92. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement