marijnz / unity-toolbar-extender

Extend the Unity Toolbar with your own Editor UI code.
MIT License
1.66k stars 168 forks source link

Alternative approach #4

Closed OndrejPetrzilka closed 5 years ago

OndrejPetrzilka commented 5 years ago

Hey, I was thinking about different approach:

  1. Create wrapper class deriving from Toolbar
  2. Override OldOnGUI(), provide callback
  3. Replace instance of the Toolbar in Editor

Sounds impossible, but it actually isn't.

  1. To derive from Toolbar, you'll have to do it in IL, because it's internal. See attached IL file. You can ignore GameObjectEditor that's something else :) I've also attached mockup of classes, which I compile and then disassemble into IL UnityIL.Editor.il.txt Mockup.cs.txt

  2. There's internal ToolbarWrapper and static class ToolbarExtension which exposes callbacks and other useful methods (see mockups)

  3. Create wrapper, copy all serialized properties, replace it MainView.children[0] is always Toolbar, just replace it There's also Toolbar.get (field surprisingly), which is probably a good idea to replace ToolbarExtension.cs.txt

IL compilation command: c:\Windows\Microsoft.NET\Framework\v2.0.50727\ilasm UnityIL.Editor.il /dll /OUTPUT=%~dp0UnityIL.Editor.dll

Simple case works, see screenshot below. I've encountered some issues when switching layout (not sure why). Done on Unity 2018.2.10f1

toolbarext

OndrejPetrzilka commented 5 years ago

This approach is complicated and has issues, there's better approach #5

marijnz commented 5 years ago

Plus points for creativity :)