[HideInInspector] Functionality for Methods

Ran into an issue recently where a public method that needs to be public (it is called externally by another package that does not care about implementation details) was being used incorrectly in UnityEvent properties (calling abstract class’s Set instead of specific implementation’s SetState which provides additional functionality) and I was asked if it was possible to hide Set from the dropdown.

The only way to do this is to mark the base method as [Obsolete] due to how Unity populates the dropdown for method references. But this would hide Set in the dropdown for all implementation subclasses, some of which have a valid reason to call Set via a reference set in the inspector.

Creating a new Set method that hides the base implementation (the method was not virtual for a reason) and then marking that as [Obsolete] doesn’t work, because the base class still has a non-obsolete version (implementing this feature probably wouldn’t fix my specific use-case, as the new method and base method are still different methods as far as Reflection goes).

Checking for the attribute should just be an additional check inside the UnityEventDrawer class where the check for Obsolete already occurs.

Hey,
a quick solution I came up with is to flag your method with the SpecialName Attribute like so:

[SpecialName]
public void MyHiddenMethod()
{...}

You can see in your screenshot of the UnityEventDrawer code that methods with IsSpecialName flag get excluded.

However I have not tested this solution any further for side effects. Would be awesome if you could comment here in case you find problems with it.

In general I would also appreciate an official dedicated attribute for hiding methods from the inspector

Cheers

1 Like