dimanche 19 juillet 2020

Restricting access to various methods of a game manager object - Unity

In a game I'm working on in Unity I have various managers like AudioManager etc. I'd like to allow various levels of access to the actual AudioManager object in the scene, so that some objects could maybe only play sounds, some could play sounds and also change mixer settings etc.

My first thought was that I could achieve this by inheritance. IAudioManager could inherit from IAudioManagerBase. This way whether I refer to the actual audio manager instance (which implements IAudioManager) as an object of type IAudioManager or IAudioManagerBase will determine what methods I'll be able to call.

Is this a reasonable approach? My whole goal is just to have the compiler tell me "you're calling a method you're not allowed to" and nothing else. Using inheritance for this purpose seems a bit weird to me.

Just for completeness, because I don't know if it matters - I'll be getting a reference to the various managers by using a service locator object - for example, it could return the actual AudioManager instance.

Aucun commentaire:

Enregistrer un commentaire