/// <summary> /// Adds the specified object at the specified menu item path and returns all menu items created in order to end up at the specified menu path. /// </summary> /// <param name="tree">The tree.</param> /// <param name="menuPath">The menu path.</param> /// <param name="instance">The object instance.</param> /// <param name="forceShowOdinSerializedMembers">Set this to true if you want Odin serialzied members such as dictionaries and generics to be shown as well.</param> /// <returns>Returns all menu items created in order to add the menu item at the specified menu item path.</returns> public static IEnumerable <OdinMenuItem> AddObjectAtPath(this OdinMenuTree tree, string menuPath, object instance, bool forceShowOdinSerializedMembers = false) { string name; SplitMenuPath(menuPath, out menuPath, out name); if (forceShowOdinSerializedMembers && !(instance as UnityEngine.Object)) { return(tree.AddMenuItemAtPath(menuPath, new OdinMenuItem(tree, name, new SerializedValueWrapper(instance)))); } else { return(tree.AddMenuItemAtPath(menuPath, new OdinMenuItem(tree, name, instance))); } }
/// <summary> /// Adds all assets at the specified path. Each asset found gets its own menu item inside the specified menu item path. /// </summary> /// <param name="tree">The tree.</param> /// <param name="menuPath">The menu item path.</param> /// <param name="assetFolderPath">The asset folder path.</param> /// <param name="type">The type.</param> /// <param name="includeSubDirectories">Whether to search for assets in subdirectories as well.</param> /// <param name="flattenSubDirectories">If true, sub-directories in the assetFolderPath will no longer get its own sub-menu item at the specified menu item path.</param> /// <returns>Returns all menu items created in order to add the menu item at the specified menu item path.</returns> public static IEnumerable <OdinMenuItem> AddAllAssetsAtPath(this OdinMenuTree tree, string menuPath, string assetFolderPath, Type type, bool includeSubDirectories = false, bool flattenSubDirectories = false) { assetFolderPath = (assetFolderPath ?? "").TrimEnd('/') + "/"; if (!assetFolderPath.ToLower().StartsWith("assets/")) { assetFolderPath = "Assets/" + assetFolderPath; } assetFolderPath = assetFolderPath.TrimEnd('/') + "/"; var assets = AssetDatabase.GetAllAssetPaths() .Where(x => { if (includeSubDirectories) { return(x.StartsWith(assetFolderPath, StringComparison.InvariantCultureIgnoreCase)); } return(string.Compare(PathUtilities.GetDirectoryName(x).Trim('/'), assetFolderPath.Trim('/'), true) == 0); }); menuPath = menuPath ?? ""; menuPath = menuPath.TrimStart('/'); HashSet <OdinMenuItem> result = new HashSet <OdinMenuItem>(); foreach (var assetPath in assets) { UnityEngine.Object obj = AssetDatabase.LoadAssetAtPath(assetPath, type); if (obj == null) { continue; } var name = System.IO.Path.GetFileNameWithoutExtension(assetPath); var path = menuPath; if (flattenSubDirectories == false) { var subPath = (PathUtilities.GetDirectoryName(assetPath).TrimEnd('/') + "/"); subPath = subPath.Substring(assetFolderPath.Length); if (subPath.Length != 0) { path = path.Trim('/') + "/" + subPath; } } path = path.Trim('/') + "/" + name; string menu; SplitMenuPath(path, out path, out menu); tree.AddMenuItemAtPath(result, path, new OdinMenuItem(tree, menu, obj)); } return(result); }
/// <summary> /// Adds the asset at the specified menu item path and returns all menu items created in order to end up at the specified menu path. /// </summary> /// <param name="tree">The tree.</param> /// <param name="menuItemPath">The menu item path.</param> /// <param name="assetPath">The asset path.</param> /// <param name="type">The type.</param> /// <returns>Returns all menu items created in order to add the menu item at the specified menu item path.</returns> public static IEnumerable <OdinMenuItem> AddAssetAtPath(this OdinMenuTree tree, string menuItemPath, string assetPath, Type type) { if (!assetPath.StartsWith("assets/", StringComparison.InvariantCultureIgnoreCase)) { assetPath = "Assets/" + assetPath; } var obj = AssetDatabase.LoadAssetAtPath(assetPath, type); string name; SplitMenuPath(menuItemPath, out menuItemPath, out name); return(tree.AddMenuItemAtPath(menuItemPath, new OdinMenuItem(tree, name, obj))); }
/// <summary> /// Adds all asset instances from the specified path and type into a single <see cref="OdinMenuItem"/> at the specified menu item path, and returns all menu items created in order to add the menuItem at the specified path.. /// </summary> /// <param name="tree">The tree.</param> /// <param name="menuPath">The menu item path.</param> /// <param name="assetFolderPath">The asset folder path.</param> /// <param name="type">The type of objects.</param> /// <param name="includeSubDirectories">Whether to search for assets in subdirectories as well.</param> /// <returns>Returns all menu items created in order to add the menu item at the specified menu item path.</returns> public static IEnumerable <OdinMenuItem> AddAllAssetsAtPathCombined(this OdinMenuTree tree, string menuPath, string assetFolderPath, Type type, bool includeSubDirectories = false) { assetFolderPath = (assetFolderPath ?? "").TrimEnd('/') + "/"; if (!assetFolderPath.ToLower().StartsWith("assets/")) { assetFolderPath = "Assets/" + assetFolderPath; } assetFolderPath = assetFolderPath.TrimEnd('/') + "/"; var assets = AssetDatabase.GetAllAssetPaths() .Where(x => { if (includeSubDirectories) { return(x.StartsWith(assetFolderPath, StringComparison.InvariantCultureIgnoreCase)); } return(string.Compare(PathUtilities.GetDirectoryName(x).Trim('/'), assetFolderPath.Trim('/'), true) == 0); }) .Select(x => { UnityEngine.Object tmp = null; return((Func <object>)(() => { if (tmp == null) { tmp = AssetDatabase.LoadAssetAtPath(x, type); } return tmp; })); }) .ToList(); string path, menu; SplitMenuPath(menuPath, out path, out menu); return(tree.AddMenuItemAtPath(path, new OdinMenuItem(tree, menu, assets))); }