- Home /
Public, private and getting stuff efficiency question.
Is it cheaper to have something like a sound set to private and have it find the audio source in the assets or if used as public and set in inspector?\
I'm asking because I see things in code examples in the net that use private versions of things that would be much easier to use as public and set in inspector.
Like for example I'm working vehicle sounds and I just want to add the sound via:
public AudioSource audioSource;
... Will that suck my cpu dry or something? Just need to know in case.
My game:
Answer by Jamora · Dec 19, 2013 at 07:34 PM
The access modifier has no effect on the CPU. It only sets how visible the variable is at compile time, so you can encapsulate data in good Object-Oriented fashion.
Using public when private would suffice is not the end of the world, but it is bad (Object-Oriented) software design.
Answer by Owen-Reynolds · Dec 19, 2013 at 08:49 PM
As far as Inspector vs. hand-loaded:
A lot of old-time programmers have always had to store data themselves, set up a file format, ... and search/read/load everything themselves. They feel odd if they can't bring up the master audio list in NotePad and check/edit it (and there are some advantages.) These people are naturally going to going to use Resources.Load.
The Inspector is doing the same thing. When you drag "arf.wav" into the dogSound
variable, Unity is storing something like "var:dogSound, loc: assets/animals/arf.wav, ... " in a file like "scene3.data". When the scene loads, Unity scans that file and looks up and load the assets from Resources, same as hand-coding would do.
If you know the file format, you could write a special-case file+code. For example, I get a lot of color RGB's sent to me. So instead of setting a Color[] Cols;
in the Inspector, I read from a file with lines like: "255/127/127 pink". That way I can cut&paste into the file, or send it to someone else to edit. I doubt it runs faster -- probably slower, since there's one extra file to open.