Author Topic: AnchorPoint performance  (Read 1528 times)

yelmam

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
AnchorPoint performance
« on: November 13, 2015, 08:31:19 AM »
Hi,

Just found out a this method in AnchorPoint class in UIRect.cs

      public Vector3[] GetSides (Transform relativeTo)
      {
         if (target != null)
         {
            if (rect != null) return rect.GetSides(relativeTo);
#if UNITY_4_3 || UNITY_4_5 || UNITY_4_6
            if (target.camera != null) return target.camera.GetSides(relativeTo);
#else
            if (target.GetComponent<Camera>() != null) return target.GetComponent<Camera>().GetSides(relativeTo);
#endif
         }
         return null;
      }

This method cause allocations on every frame and also take couple of milliseconds to execute.
There are two problems here as far as I can tell:
1. Using GetComponent every frame
2. Using GetComponent twice rather that just caching the result

It would be great if this piece of code could be optimized for future releases.
BTW I am using NGUI 3.9.0

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: AnchorPoint performance
« Reply #1 on: November 18, 2015, 03:59:33 PM »
The allocations only happen in the Unity Editor. It's a well known issue where there is an allocation/delay that happens when you try to GetComponent that doesn't exist. This doesn't happen at run-time.