- Home /
How to add options to this context menu?
Is it possible add options to this context menu? I want to make context option to allow me copy texture settings, is it possible?
Answer by sisus_co · Jan 27, 2020 at 05:11 PM
There is a callback for when the context menu is opening for any inspector element drawn as a SerializedProperty field.
using UnityEngine;
using UnityEditor;
[InitializeOnLoad]
public static class ContextMenuOpenListener
{
static ContextMenuOpenListener()
{
EditorApplication.contextualPropertyMenu += OnContextMenuOpening;
}
private static void OnContextMenuOpening(GenericMenu menu, SerializedProperty property)
{
Debug.Log("ContextMenu opening for property " + property.propertyPath);
}
}
Unfortunately almost none of the elements of the TextureImporterInspector are drawn using SerializedProperties, so the callback never gets sent for most of the elements :(
So I don't think that there's currently any way to achieve this. Perhaps once all the internal Editor have switched to using UIElements it could become possible to hack something together.
What you could do however is extend the texture importer's context menu with several new items for copying and pasting the different elements.
using UnityEditor;
internal static class TextureImporterContextMenuExtensions
{
private static object copied;
[MenuItem("CONTEXT/TextureImporter/Copy/Texture Type")]
private static void CopyMaxSize(MenuCommand command)
{
var textureImporter = (TextureImporter)command.context;
copied = textureImporter.textureType;
UnityEngine.Debug.Log("Copied TextureImporterType: " + copied);
}
[MenuItem("CONTEXT/TextureImporter/Paste/Texture Type")]
private static void PasteTextureType(MenuCommand command)
{
var textureImporter = (TextureImporter)command.context;
textureImporter.textureType = (TextureImporterType)copied;
UnityEngine.Debug.Log("Pasted TextureImporterType: " + copied);
}
[MenuItem("CONTEXT/TextureImporter/Paste/Texture Type", validate = true)]
private static bool CanPasteTextureType(MenuCommand command)
{
return copied is TextureImporterType;
}
}
Thanks you are totaly right, i added my cody to copy importer settings.
class TextureImporterContext$$anonymous$$enuExtensions
{
private static TextureImporter buffer;
[$$anonymous$$enuItem("CONTEXT/TextureImporter/Copy")]
public static void $$anonymous$$enuCommandCopy($$anonymous$$enuCommand command)
{
TextureImporter target = (TextureImporter) command.context;
buffer = target;
}
[$$anonymous$$enuItem("CONTEXT/TextureImporter/Paste", true)]
public static bool PasteValidateFunction()
{
return buffer != null;
}
[$$anonymous$$enuItem("CONTEXT/TextureImporter/Paste", false)]
public static void Paste($$anonymous$$enuCommand command)
{
if (buffer != null)
{
TextureImporter target = (TextureImporter) command.context;
EditorUtility.CopySerializedIfDifferent(buffer, target);
target.SaveAndReimport();
}
}
}