Tasharen Entertainment Forum
Support => NGUI 3 Support => Topic started by: nekokiller on March 11, 2014, 10:53:47 AM
-
hi, ngui team:
You should use the Vector3.sqrMagnitude instead of Vector3.magnitude.
Our program with ngui , the performance is not good on the android/ios device . ( our case, we got fps = 40 ~ 45 )
We change the ngui source code , use sqrMagnitude instead of magnitude , then the performance is become good a lot ~ ( fps up to 50 ~ 55 )
-
sqrMagnitude and magnitude are quite different. You can't just replace everything. NGUI doesn't use .magnitude at run-time in a lot of places. Scroll view mainly, and the event position checks which occur only when there is an active touch / mouse event. So I don't know what it is you changed, but my guess is that you broke something and the increased performance was just a side-effect.
-
thanks for reply.
we are not just replace the 'magnitude', of couse we have to modify relative codes to make the logic right.
Vector3.sqrMagnitude : Returns the squared length of this vector
If you just want to check the distance is bigger or smaller than a constant value, not to get a real distance , you really really should use sqrMagnitude instead of magnitude for the performance issue.
The reason for using sqrMagnitude instead of magnitude is because magnitude has to do a square root operation, which is pretty expensive, much more so than a multiplication.
example :
UICamera.cs : void ProcessTouch( ... )
...
float mag = currentTouch.totalDelta.magnitude;
...
if ( currentTouch.delta.magnitude != 0f )
...
example :
LookAtTarget.cs : LateUpdate() :
...
float mag = dir.magnitude ;
if(mag > 0.001f )
...
change to currentTouch.delta.sqrMagnitude is not better ? of course the sqrMagnitude is better than magnitude.
other examples in
UIDragObject.cs, UIWidget.cs, UIScrollView.cs
-
Interesting... I'll remove .magnitude usage from UICamera. Any other places?
-
UIDraggableCamera.cs : ConstrainToBounds( ) : if ( offset.magnitude > 0f )
UIScrollView.cs : RestrictWithinBounds( ) : if (constraint.magnitude > 1f )
UIPanel.cs : ConstrainTargetToBounds()
SpringPosition.csd : Update( ) : use sqrMagnitude can do the same result, just have to modify some codes.
-
Got them, thanks.