OdinMenuItem.DrawMenuItem does not work in isolation (reversion)

Issue #571 closed
Former user created an issue

Previously (Odin 2.0.7 and lower or so), we have been drawing a shallow 2-level Odin menu tree by hand, by using OdinMenuItem.DrawMenuItem(int indent). Historically, this has worked, but since ~2.1.0+, throws a NullReferenceException.

I don’t have the old Odin source on me, but running with the 2.1.3 source, the issue happens at this line (OdinMenuItem.cs, line 812):

        this.wasMouseDownEvent = eType == EventType.MouseDown && this.rect.Contains(e.mousePosition);

The issue is that e is null. Currently, e is OdinMenuTree.CurrentEvent, and that is now only set inside the OdinMenuTree.DrawMenuTree function.

While it makes sense for the menu items to require the menu tree to be drawn, this behaviour is a reversion from old versions of Unity.

For completeness sake:

  1. NullReferenceException when using OdinMenuItem without OdinMenuTree
  2. Create a shallow menu tree and draw it like this:

    --

  3. Issue appears in 2018.4 and 2019.1 (and probably every version of Unity ever)
  4. Issue appears in 2.1.3, did not appear in older versions (2.0.7 and similar)
  5. No
  6. Windows 10

Possible fix looks like:

Comments (6)

  1. Bjarke Elias

    Hey there, sorry about the breaking change.

    There’s actually a ton of things that don’t work on if you don’t call draw on the tree itself.

    We could also solve this by providing a way of specifying a custom CreateOdinMenuTreeItem function to the tree, which would be used as the default creation method. That way the root menu item of the tree would also be of your custom type. Then you could call odinMenuTree.Draw(); and DrawOdinMenuItems would also be invoked for the first layer of menu items.

    Would that be a suitable solution to your use case?

  2. Bjarke Elias

    I added in the feature, it’s a good one to have regardless, but it turns out that there’s a lot of things we have to do when drawing the first layer. It was not as simple as just drawing the root menu item unfortunately. However, looking at you code maybe I can assist you in reaching similar results without using GUI layout. Or if you really just want to move on with your work, you can try setting OdinMenuTree.CurrentEvent through reflection.

    Feel free to reach out to me on discord if you need any assistance, I'm curious to what it is you use case exactly is.

  3. Former user Account Deleted reporter

    It’s actually a pretty small thing, and in retrospect I’m not sure why an Odin Menu Tree is in use at all. We’ve got a project wizard that looks like this:

    whereas, with default OdinMenuTreeStyling, it’d look like this:

    I think, in my case, that it probably shouldn’t be an OdinMenuTree at all – pretty sure I can handle constructing a list of clickable things :P

  4. Log in to comment