Author Topic: Smooth scaling of UIDragPanelContents issue  (Read 4275 times)

troglodescu

  • Guest
Smooth scaling of UIDragPanelContents issue
« on: May 10, 2013, 05:03:45 AM »
I want to make a scroll list that will allways center on the closest item. This is already done using UICenterOnChild.
The only problem is that I want the centeredObject to be scaled up a bit, for this I use the simple code

  1. // Determine the closest child
  2.         for (int i = 0, imax = parentForChildren.childCount; i < imax; ++i)
  3.         {
  4.             Transform t = parentForChildren.GetChild(i);
  5.             float sqrDist = Vector3.SqrMagnitude(t.position - offsetCenter);
  6.  
  7.             if (sqrDist < min)
  8.             {
  9.                 min = sqrDist;
  10.                 closest = t;
  11.             }
  12.            
  13.             TweenScale.Begin(t.gameObject, 0.3F, Vector3.one);
  14.         }
  15.         centeredObject = closest.gameObject;
  16.  
  17.  
  18.        TweenScale.Begin(centeredObject, 0.3F, Vector3.one * scaleFactor);

This works very well in the scene view, I can clearly see how the scroll items are being properly scaled smoothly but the sprites update their scales only when the TweenScale finishes and the scale transition is not smooth at all, in fact it is done in one frame.

Are there any options I need to tick on the UIPanel to force it to update the UISprites explicitly or something ?

Any help is greatly appreciated, thank you :)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Smooth scaling of UIDragPanelContents issue
« Reply #1 on: May 11, 2013, 12:44:27 AM »
Your panel is likely marked as "static", so it doesn't watch for transform changes.

troglodescu

  • Guest
Re: Smooth scaling of UIDragPanelContents issue
« Reply #2 on: May 13, 2013, 09:03:40 AM »
Thanks alot, that was it :)

rekus

  • Guest
Re: Smooth scaling of UIDragPanelContents issue
« Reply #3 on: May 24, 2013, 07:56:49 PM »
I want to make a scroll list that will allways center on the closest item. This is already done using UICenterOnChild.
The only problem is that I want the centeredObject to be scaled up a bit, for this I use the simple code

  1. // Determine the closest child
  2.         for (int i = 0, imax = parentForChildren.childCount; i < imax; ++i)
  3.         {
  4.             Transform t = parentForChildren.GetChild(i);
  5.             float sqrDist = Vector3.SqrMagnitude(t.position - offsetCenter);
  6.  
  7.             if (sqrDist < min)
  8.             {
  9.                 min = sqrDist;
  10.                 closest = t;
  11.             }
  12.            
  13.             TweenScale.Begin(t.gameObject, 0.3F, Vector3.one);
  14.         }
  15.         centeredObject = closest.gameObject;
  16.  
  17.  
  18.        TweenScale.Begin(centeredObject, 0.3F, Vector3.one * scaleFactor);

This works very well in the scene view, I can clearly see how the scroll items are being properly scaled smoothly but the sprites update their scales only when the TweenScale finishes and the scale transition is not smooth at all, in fact it is done in one frame.

Are there any options I need to tick on the UIPanel to force it to update the UISprites explicitly or something ?

Any help is greatly appreciated, thank you :)

Hi troglodescu I'm trying to do the same thing you did there but I get this error message

"error CS0200: Property or indexer `UICenterOnChild.centeredObject' cannot be assigned to (it is read only)"  :-\

I'm totally lost here on C# Thanks!




troglodescu

  • Guest
Re: Smooth scaling of UIDragPanelContents issue
« Reply #4 on: May 29, 2013, 04:00:31 PM »
Hi rekus,

Sorry for the delay. The thing is, I made a different script called UICenteredOnChildImproved.cs

Here is the code. You can use my version, maybe it's more suited for you as well.

In my version if you want to set the centered object just use the function CenterOn(Transform).

Try using this script instead of NGUI-s UICenteredOnChild:

  1. using UnityEngine;
  2.  
  3. /// <summary>
  4. /// Ever wanted to be able to auto-center on an object within a draggable panel?
  5. /// Attach this script to the container that has the objects to center on as its children.
  6. /// </summary>
  7. [AddComponentMenu("NGUI/Interaction/Center On Child")]
  8. public class UICenterOnChildImproved : MonoBehaviour
  9. {
  10.     #region Fields Arranged Nicely :)
  11.  
  12.     public UIDraggablePanel draggablePanel;
  13.  
  14.     public Transform parentForChildren;
  15.  
  16.     public System.EventHandler onCenteredOnChild;
  17.  
  18.     public GameObject centeredObject;
  19.  
  20.     public float scaleFactor = 1.2F;
  21.  
  22.     public bool scaleSelected = false;
  23.  
  24.     UIPanel panel;
  25.  
  26.     #endregion Fields Arranged Nicely :)
  27.  
  28.     #region Properties
  29.  
  30.     public UIPanel Panel
  31.     {
  32.         get
  33.         {
  34.             if (!panel)
  35.             {
  36.                 panel = draggablePanel.GetComponent<UIPanel>();
  37.             }
  38.             return panel;
  39.         }
  40.     }
  41.  
  42.     #endregion Properties
  43.  
  44.     #region Methods
  45.  
  46.     public void CenterOn(Transform closest)
  47.     {
  48.         Vector4 clip = Panel.clipRange;
  49.         Transform dt = Panel.cachedTransform;
  50.         Vector3 center = dt.localPosition;
  51.         center.x += clip.x;
  52.         center.y += clip.y;
  53.         center = dt.parent.TransformPoint(center);
  54.  
  55.         centeredObject = closest.gameObject;
  56.         if (scaleSelected)
  57.         {
  58.  
  59.             TweenScale.Begin(centeredObject, 0.3F, Vector3.one * scaleFactor);
  60.         }
  61.  
  62.         onCenteredOnChild.InvokeSafe(this, null);
  63.  
  64.         // Figure out the difference between the chosen child and the panel's center in local coordinates
  65.         Vector3 cp = dt.InverseTransformPoint(closest.position);
  66.         Vector3 cc = dt.InverseTransformPoint(center);
  67.         Vector3 offset = cp - cc;
  68.  
  69.         // Offset shouldn't occur if blocked by a zeroed-out scale
  70.         if (draggablePanel.scale.x == 0f) offset.x = 0f;
  71.         if (draggablePanel.scale.y == 0f) offset.y = 0f;
  72.         if (draggablePanel.scale.z == 0f) offset.z = 0f;
  73.  
  74.         // Spring the panel to this calculated position
  75.         SpringPanel.Begin(draggablePanel.gameObject, dt.localPosition - offset, 8f);
  76.     }
  77.     /// <summary>
  78.     /// Recenter the draggable list on the center-most child.
  79.     /// </summary>
  80.     public void Recenter()
  81.     {
  82.         Vector4 clip = Panel.clipRange;
  83.         Transform dt = Panel.cachedTransform;
  84.         Vector3 center = dt.localPosition;
  85.         center.x += clip.x;
  86.         center.y += clip.y;
  87.         center = dt.parent.TransformPoint(center);
  88.  
  89.         // Offset this value by the momentum
  90.         Vector3 offsetCenter = center - draggablePanel.currentMomentum * (draggablePanel.momentumAmount * 0.1f);
  91.         draggablePanel.currentMomentum = Vector3.zero;
  92.  
  93.         float min = float.MaxValue;
  94.         Transform closest = null;
  95.  
  96.         // Determine the closest child
  97.         for (int i = 0, imax = parentForChildren.childCount; i < imax; ++i)
  98.         {
  99.             Transform t = parentForChildren.GetChild(i);
  100.             float sqrDist = Vector3.SqrMagnitude(t.position - offsetCenter);
  101.  
  102.             if (sqrDist < min)
  103.             {
  104.                 min = sqrDist;
  105.                 closest = t;
  106.             }
  107.             if (scaleSelected)
  108.             {
  109.                 TweenScale.Begin(t.gameObject, 0.3F, Vector3.one);
  110.             }
  111.         }
  112.  
  113.         CenterOn(closest);
  114.     }
  115.  
  116.     void Awake()
  117.     {
  118.         draggablePanel.onDragFinished = OnDragFinished;
  119.     }
  120.     void OnDragFinished()
  121.     {
  122.         if (enabled) Recenter();
  123.     }
  124.     /// <summary>
  125.     /// Game object that the draggable panel is currently centered on.
  126.     /// </summary>
  127.     void OnEnable()
  128.     {
  129.         Recenter();
  130.     }
  131.  
  132.     #endregion Methods
  133. }

rekus

  • Guest
Re: Smooth scaling of UIDragPanelContents issue
« Reply #5 on: May 30, 2013, 08:11:55 PM »
 :D Thank you very much!