public void Dispose() { value = null; type = null; secondaryLabel = ""; parent = null; preview = null; previewFetched = false; isGroup = false; valueType = MenuItemValueType.Disregard; for (int n = children.Count - 1; n >= 0; n--) { children[n].Dispose(); } children.Clear(); var dispose = this; Pool.Dispose(ref dispose); }
public PopupMenuItem AddChild(string itemLabel, string itemSecondaryLabel, [CanBeNull] object itemValue, [CanBeNull] Type itemType, MenuItemValueType setValueType = MenuItemValueType.Undefined) { var addItem = Item(itemValue, itemType, itemLabel, itemSecondaryLabel, this, setValueType); children.Add(addItem); return(addItem); }
private static bool TryGetPreviewUsingStringValue([NotNull] string stringValue, [CanBeNull] Type type, MenuItemValueType valueType, ref Texture preview) { switch (valueType) { case MenuItemValueType.Undefined: if (TryGetPreviewUsingStringValue(stringValue, type, MenuItemValueType.AssetPath, ref preview)) { return(true); } if (TryGetPreviewUsingStringValue(stringValue, type, MenuItemValueType.AssetGuid, ref preview)) { return(true); } return(TryGetPreviewUsingStringValue(stringValue, type, MenuItemValueType.HierarchyPath, ref preview)); case MenuItemValueType.AssetPath: preview = AssetDatabase.GetCachedIcon(stringValue); return(preview != null); case MenuItemValueType.AssetGuid: var guid = AssetDatabase.GUIDToAssetPath(stringValue); if (!string.IsNullOrEmpty(guid)) { preview = AssetDatabase.GetCachedIcon(guid); return(preview != null); } return(false); case MenuItemValueType.HierarchyPath: if (type != null && type.IsComponent()) { var component = HierarchyUtility.FindComponentByHierarchyPath(stringValue, type); preview = AssetPreview.GetAssetPreview(component); return(preview != null); } var gameObject = HierarchyUtility.FindByHierarchyPath(stringValue); if (gameObject != null) { preview = AssetPreview.GetAssetPreview(gameObject); return(preview != null); } return(false); } throw new NotImplementedException("TryGetPreviewUsingStringValue does not support MenuItemValueType " + valueType); }
/// <summary> Create PopupMenuItem, with preview icon generated based on type or value only if/when needed. </summary> /// <param name="value"> The value represented by the menu item. This may be null. </param> /// <param name="type"> Type represented by the menu item. This can be used when generating preview icon for the item. </param> /// <param name="label"> The label for the item. </param> /// <param name="secondaryLabel"> The secondary label for the item (currently shown as a tooltip). </param> /// <param name="parent"> Parent PopupMenuItem which contains the created item. This may be null. </param> /// <param name="valueType"> Describes the type of value. Used when generating a preview for the menu item using the value if/when the menu item is shown. </param> /// <returns> A PopupMenuItem. </returns> public static PopupMenuItem Item([CanBeNull] object value, [CanBeNull] Type type, string label, string secondaryLabel, [CanBeNull] PopupMenuItem parent, MenuItemValueType valueType = MenuItemValueType.Undefined) { PopupMenuItem item; if (!Pool.TryGet(out item)) { item = new PopupMenuItem(); } item.isGroup = false; item.parent = parent; item.children.Clear(); item.label = label; item.type = type; item.value = value; item.valueType = valueType; if (secondaryLabel == null) { if (type != null) { item.secondaryLabel = type.Namespace + "\n" + type.Assembly.GetName().Name; } else { item.secondaryLabel = ""; } } else { item.secondaryLabel = secondaryLabel; } return(item); }
private static bool TryGetPreviewUsingValue([CanBeNull] object value, [CanBeNull] Type type, MenuItemValueType valueType, ref Texture preview) { if (value == null) { return(false); } switch (valueType) { case MenuItemValueType.Undefined: if (TryGetPreviewUsingValue(value as Object, type, MenuItemValueType.UnityObject, ref preview)) { return(true); } string valueString = value as string; return(!string.IsNullOrEmpty(valueString) && TryGetPreviewUsingStringValue(value as string, type, MenuItemValueType.Undefined, ref preview)); case MenuItemValueType.UnityObject: var obj = value as Object; if (obj != null) { preview = AssetPreview.GetAssetPreview(obj); return(preview != null); } return(false); case MenuItemValueType.Disregard: return(false); default: valueString = value as string; return(!string.IsNullOrEmpty(valueString) && TryGetPreviewUsingStringValue((string)value, type, valueType, ref preview)); } }
private static void GetPreviewUsingValueOrType([CanBeNull] object value, [CanBeNull] Type type, MenuItemValueType valueType, ref Texture preview) { #if UNITY_EDITOR if (TryGetPreviewUsingValue(value, type, valueType, ref preview)) { return; } #endif if (type != null) { #if UNITY_EDITOR preview = AssetPreview.GetMiniTypeThumbnail(type); if (preview != null) { return; } #endif var typeNamespace = type.Namespace; if (typeNamespace != null) { int i = typeNamespace.IndexOf('.'); if (i != -1) { typeNamespace = typeNamespace.Substring(0, i); } switch (typeNamespace) { case "System": preview = InspectorUtility.Preferences.graphics.DotNetFileIcon; #if DEV_MODE || PROFILE_POWER_INSPECTOR Profiler.EndSample(); #endif return; case "UnityEngine": case "UnityEngineInternal": preview = InspectorUtility.Preferences.graphics.UnityFileIcon; #if DEV_MODE || PROFILE_POWER_INSPECTOR Profiler.EndSample(); #endif return; case "UnityEditor": case "UnityEditorInternal": preview = InspectorUtility.Preferences.graphics.UnityEditorFileIcon; #if DEV_MODE || PROFILE_POWER_INSPECTOR Profiler.EndSample(); #endif return; } } preview = InspectorUtility.Preferences.graphics.CSharpScriptIcon; } }
public static void BuildPopupMenuItemWithLabel(ref List <PopupMenuItem> rootItems, ref Dictionary <string, PopupMenuItem> groupsByLabel, ref Dictionary <string, PopupMenuItem> itemsByLabel, [NotNull] object value, [CanBeNull] Type type, string fullMenuName, string tooltip, MenuItemValueType valueType) { #if DEV_MODE || PROFILE_POWER_INSPECTOR Profiler.BeginSample("BuildPopupMenuItemWithLabel"); #endif int split = fullMenuName.LastIndexOf('/'); PopupMenuItem item; if (split != -1) { var groupLabels = fullMenuName.Substring(0, split); var itemLabel = fullMenuName.Substring(split + 1); var group = GetOrCreateGroup(ref rootItems, ref groupsByLabel, groupLabels, InspectorUtility.Preferences.graphics.PrefabIcon); item = group.AddChild(itemLabel, tooltip, value, type, valueType); } else { item = PopupMenuItem.Item(value, type, fullMenuName, tooltip, null, valueType); rootItems.Add(item); } if (!itemsByLabel.ContainsKey(fullMenuName)) { itemsByLabel.Add(fullMenuName, item); } #if DEV_MODE else { Debug.LogError("Menu already contained item by name \"" + fullMenuName + "\""); } #endif #if DEV_MODE || PROFILE_POWER_INSPECTOR Profiler.EndSample(); #endif }
public static void BuildPopupMenuItemWithLabel(ref List <PopupMenuItem> rootItems, ref Dictionary <string, PopupMenuItem> groupsByLabel, ref Dictionary <string, PopupMenuItem> itemsByLabel, [NotNull] object value, [NotNull] Type type, string fullMenuName, MenuItemValueType valueType) { #if DEV_MODE || PROFILE_POWER_INSPECTOR Profiler.BeginSample("BuildPopupMenuItemWithLabel"); #endif BuildPopupMenuItemWithLabel(ref rootItems, ref groupsByLabel, ref itemsByLabel, value, type, fullMenuName, GetTooltip(type), valueType); #if DEV_MODE || PROFILE_POWER_INSPECTOR Profiler.EndSample(); #endif }
public static void BuildPopupMenuItemWithLabel([NotNull]ref List<PopupMenuItem> rootItems, [NotNull]ref Dictionary<string, PopupMenuItem> groupsByLabel, [NotNull]ref Dictionary<string, PopupMenuItem> itemsByLabel, [NotNull]object value, [CanBeNull]Type type, [NotNullOrEmpty]string fullMenuName, [NotNull]string tooltip, MenuItemValueType valueType) { #if DEV_MODE || PROFILE_POWER_INSPECTOR Profiler.BeginSample("BuildPopupMenuItemWithLabel"); #endif #if DEV_MODE && PI_ASSERTATIONS Debug.Assert(rootItems != null); Debug.Assert(groupsByLabel != null); Debug.Assert(itemsByLabel != null); Debug.Assert(value != null); Debug.Assert(!string.IsNullOrEmpty(fullMenuName)); Debug.Assert(!fullMenuName.EndsWith("/")); Debug.Assert(!fullMenuName.StartsWith("/")); Debug.Assert(tooltip != null); #endif int split = fullMenuName.LastIndexOf('/'); PopupMenuItem item; if(split != -1) { if(split == fullMenuName.Length - 1) { Debug.LogWarning("BuildPopupMenuItemWithLabel called with menu path that ended with \"/\". Menu items with an empty name are not supported."); #if DEV_MODE || PROFILE_POWER_INSPECTOR Profiler.EndSample(); #endif return; } var groupLabel = fullMenuName.Substring(0, split); var itemLabel = fullMenuName.Substring(split + 1); var group = GetOrCreateGroup(ref rootItems, ref groupsByLabel, groupLabel, null); item = group.AddChild(itemLabel, tooltip, value, type, valueType); } else { item = PopupMenuItem.Item(value, type, fullMenuName, tooltip, null, valueType); rootItems.Add(item); } if(!itemsByLabel.ContainsKey(fullMenuName)) { itemsByLabel.Add(fullMenuName, item); } #if DEV_MODE else { Debug.LogError("Menu already contained item by name \"" + fullMenuName + "\""); } #endif #if DEV_MODE || PROFILE_POWER_INSPECTOR Profiler.EndSample(); #endif }