Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save craigmjohnston/419e7c18797eebb47d57fe7a11f68e18 to your computer and use it in GitHub Desktop.
Save craigmjohnston/419e7c18797eebb47d57fe7a11f68e18 to your computer and use it in GitHub Desktop.
Example of how to have a context menu item in Unity that can only be used on folders.
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
public static class UnityDirectoryContextMenuExample
{
[MenuItem("Assets/My Context Item")]
public static void DoSomething()
{
Debug.Log("Doing something.");
}
[MenuItem("Assets/My Context Item", true)]
public static bool DoSomethingValidator()
{
var assetObject = Selection.GetFiltered<DefaultAsset>(SelectionMode.Assets).FirstOrDefault();
if (assetObject == null)
{
return false;
}
return AssetIsDirectory(assetObject);
}
public static bool AssetIsDirectory(Object assetObject)
{
string path = AssetDatabase.GetAssetPath(assetObject);
return File.GetAttributes(path).HasFlag(FileAttributes.Directory);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment