Hello everyone, I have a question optimatization. I have a script to player/ camera controller and I want set value to verriable from global Scriptable Object. Like this: But what with optimatization with this type setting varialbes.When does Unity set varialbe as like this: private InputAccess m_inputAccess => GeneralScriptableObject.Instance.InputAccess; it call one time on start or in each time when some function which is using this varialbe. I think about beacuse I can also set variable in Start/ Awake function.
it doesn't set anything. You are writing a property with a getter that runs that expression every time you call it. For example this is equivalent to your second property: Code (CSharp): private InputAction m_mouseDeltaInput { get { return m_inputAccess.MouseDelta.reference.action; } } Which is also more or less equivalent to a getter method like this: Code (CSharp): InputAction GetMouseDeltaInput() { return m_inputAccess.MouseDelta.reference.action; }
So better will be something like this? Code (CSharp): private InputAccess m_inputAccess; private InputAction m_mouseDeltaInput; private PlayerStats m_playerStats; private float m_mouseSensitivity; private void Start() { Cursor.lockState = CursorLockMode.Locked; m_playerStats = GeneralScriptableObject.Instance.PlayerStats; m_inputAccess = GeneralScriptableObject.Instance.InputAccess; m_mouseDeltaInput = m_inputAccess.MouseDelta.reference.action; m_mouseSensitivity = m_playerStats.MouseSensitivity; }
No. That's not to say that you're wrong, exactly - that code does the thing you think it does. But, you're optimizing for a nanosecond during execution (if anything*), while making your script more annoying to read, write, and maintain. And if any of those values could possibly ever change at runtime, you'll be accessing stale data. "Premature optimization" is the term for the thing you're doing right now. Optimize for performance after you've profiled and found a performance bottleneck, not before. Computers don't think in ways that are intuitive for humans, and optimizing the performance of these tiny tiny things is an absolute waste of time. * You may not be saving any time at all, because it's very possible that those properties are exactly as fast as accessing the value itself.