2
\$\begingroup\$

Why are classes created by the game developer in Unity public (by default) when you could just as easily make them internal? For example: I go Assets->Create->C# Script. Via the default script template, I have just created a public class that derives from MonoBehaviour. But technically shouldn't this class only have to be internal? If so, I really want to delete that public keyword, because having the word public is a slight distraction to me.

Do outside DLLs call functions within the developer-created DLL? I don't think so, right? Because the classes in other DLLs wouldn't know what funcitons are in there beforehand anyway.

I don't know much about how these DLLs interact with each other, but I get the feeling outside DLLs don't particularly care about the one DLL assembly the game developer himself generates.

Take a look at my picture - it seems like Unity just jams all developer-created scripts in one assembly. Yes, the assembly does reference MonoBehaviour, (so I assume other classes in all the other dlls are public, not internal) but no classes outside of this one need be concerned with the details inside this assembly.

Unity just jams all developer-created scripts in here.

It doesn't matter that much. Yes, I could go on living my life peacefully and making my classes public within Unity. But is there any potential drawback, and isn't it considered better practice, to make them internal? If there's nothing wrong with making my classes internal, then I'm going to edit the default template.

\$\endgroup\$
2
\$\begingroup\$

internal types are only visible within the assembly they are defined in. public types are visible outside that assembly.

The choice to have the user types generated by Unity's tools default to public is probably (but see below) simply a matter of convenience: while you are correct that in most cases these types could be internal, it doesn't hurt anything to make them public, and it means Unity can assume less about what the developer is going to do. It's a safe bet, basically, since it's the least restrictive option.

Also, Unity relies on reflection to accomplish some of its work. C#'s reflection APIs often take flags that specify whether to search for private types or public ones, and by default generally only search for public types. It's possible Unity's internal code is relying on that default and only expecting to be able to extract public types from the generated assemblies.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.