Firstly OnMouseOver()
is called in every frame you probably want to use OnMouseEnter()
instead.
Unity's scripting is not pure OO feel free to violate laws whenever you feel like it needs to be. E.g. every Unity message can be public, protected, private
etc. so you could just make the access modifier public and trigger it yourself.
Also you can use reflection to call private methods since you only want to test the UpdateHoverState()
method's inner logic this might be better. However reflection is vulnerable to refactors (you need the name of the method in the form of a string). For accessing private methods via a cool extension method using reflection see this answer.
And you can also use preprocessor symbols in order to make a method public when testing or to provide additional information about the class. Unity preprocessors
#if TESTING
// public wrapper method when testing
public void UpdateHoverStateTestHelper(bool hovered)
{
UpdateHoverState(hovered);
}
#endif
void UpdateHoverState(bool hovered)
{
// ... modify the state ...
}
#if TESTING || UNITY_EDITOR
// Now we will know the method name even when it gets renamed (using refactoring tools)
static readonly string UpdateHoverStateMethod = nameof(UpdateHoverState);
#endif
void UpdateHoverState(bool hovered)
{
// ... modify the state ...
}
#if TESTING || UNITY_EDITOR
// nameof is accessed at compile time so the string can be const too
const string UpdateHoverStateMethod = nameof(UpdateHoverState);
#endif
void UpdateHoverState(bool hovered)
{
// ... modify the state ...
}
The cool thing about these is that whenever you are building for your target platform these codes will be stripped (assuming you unset the TESTING
preprocessor)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…