- Home /
All interfaces in one Script
I just learned about Interfaces, is there a downside to putting all Interfaces in a single "Interfaces" script file? I'm having a hard time finding info about that, I've read that putting each Interface in it's own script is better, but no reason was provided.
Answer by Glurth · Oct 05, 2018 at 04:39 PM
Unity requires that each class be stored in a file with the same name as the class. I honestly don't know if this is required for Interfaces too, but for consistency alone, keeping them in separate files is useful. [Edit: Comments below clarify- this is NOT required for Interfaces.]
The other reason is for developing larger projects, and reusable code. If for example, you need ONE of those interfaces for a future project, you could include JUST that one interface file code in your project, without bogging it down with all the interfaces you DONT need. Same thing applies if you start developing libraries for your projects (DLLs)
Lastly, I've found separate files makes stuff easier to find. (Particularly, before I even open up visual studio.)
There is not requirement. So you could define your interfaces whereever you like. Even $$anonymous$$onoBehaviours can be defined in another file but only when it's only used as base class which is never directly attached to an object.
Though i also would recommend to place the interface in it's own file mainly to make it easier to find. Interfaces are an abstraction and usually have a wider usage Only in some rare cases you may pair the interface with some other classes.
The only na$$anonymous$$g requirement I know of is that each $$anonymous$$onoBehavior that you intend to attach to objects is defined in a file by the same name.