How to add options to this context menu?
Asked Answered
M

1

0

Is it possible add options to this context menu? I want to make context option to allow me copy texture settings, is it possible?
151905-screenshot-1.png

Moll answered 9/1, 2024 at 20:21 Comment(0)
D
0

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 :frowning:

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.

151920-texture-importer-context-menu.png

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;
	}
}
Deckard answered 9/1, 2024 at 20:20 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.