Search Unity

"Hiding" transform variable from public use

Discussion in 'Scripting' started by joshcamas, Jan 22, 2019.

  1. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,278
    Hello friends :)

    Since I heard that transform is slow to access, I have a property that caches the value. (I call it Transform).
    However, it'd be rad if I could hide the "transform" variable from public use, so then it's obvious you need to use "Transform". I'm assuming this is impossible, since "transform" is in a sealed class.

    I've tried doing something like this, but it doesn't seem to privatize it:
    Code (CSharp):
    1.  
    2.         private new Transform transform
    3.         {
    4.             get
    5.             {
    6.                 return base.transform;
    7.             }
    8.         }
    Any thoughts?

    Along those lines, why isn't transform cached internally?
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
    Have you profiled the performance difference, between using "transform" and your cached variable? I'm asking, because as far as I remember, accessing the "transform" property wasn't a performance issue in any project for me.
     
  3. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,442
    in some unite videos (although it was maybe few years ago) it was mentioned (that caching helps)
     
    40detectives likes this.
  4. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
    Yep, I know that's mentioned everywhere. I just never saw any real benefit by caching transforms in my projects, thus I was asking for numbers. Maybe it's outdated information or my projects don't access transform that often that it affects performance.
     
  5. MNNoxMortem

    MNNoxMortem

    Joined:
    Sep 11, 2016
    Posts:
    723
    @Peter77 that is why you always should profile before optimization. There is a huge difference between accessing a few transforms once or hundred thousands of times per frame.
     
    Last edited: Jan 22, 2019
    joshcamas likes this.
  6. 40detectives

    40detectives

    Joined:
    Apr 9, 2016
    Posts:
    74
    At 44:15 he speaks of GetLocalPositionAndRotation() and SetLocalPositionAndRotation().
    Is SetPositionAndRotation() the actual response to exposing SetLocalPositionAndRotation() Unity did in the recent versions? (I've just checked and in 5.4 that function did not exist)

    The one that we have now exposed is in world coordinates vs the local ones. Which were one of the optimisation tricks they did (local positions and rotations vs world position and rotation as much as they can).