Author Topic: UICenterOnChild derived script - OK to include in asset store product?  (Read 4217 times)

Gregzo

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 64
    • View Profile
Hi,

NGUI free doesn't come with UICenterOnChild, so I've made my own version, UICenterOnChildManual, which centers on a specific Transform as opposed to finding the center-most transform itself.
It's very much a stripped down version of the original, adapted for compatibility and use with my pickers.
Is it OK to include it in my asset store package?
Here it is :

  1. using UnityEngine;
  2.  
  3.         /// <summary>
  4.         /// Manual version of NGUI's UICenterOnChild -stripped down and adapted for compatibility with NGUI free
  5.         /// </summary>
  6. public class UICenterOnChildManual : MonoBehaviour
  7. {
  8.         UIDraggablePanel mDrag;
  9.         GameObject mCenteredObject;
  10.         UIPanel mDragPanel;
  11.  
  12.         /// <summary>
  13.         /// Recenter the draggable list on targetTrans.
  14.         /// </summary>
  15.  
  16.         public void CenterOnChild( Transform targetTrans )
  17.         {
  18.                 if (mDrag == null)
  19.                 {
  20.                         mDrag = NGUITools.FindInParents<UIDraggablePanel>(gameObject);
  21.                        
  22.                         mDragPanel = gameObject.GetComponent ( typeof ( UIPanel ) ) as UIPanel;
  23.  
  24.                         if (mDrag == null)
  25.                         {
  26.                                 Debug.LogWarning(GetType() + " requires " + typeof(UIDraggablePanel) + " on a parent object in order to work", this);
  27.                                 enabled = false;
  28.                                 return;
  29.                         }
  30.                 }
  31.                 if (mDragPanel == null) return;
  32.  
  33.                 // Calculate the panel's center in world coordinates
  34.                 Vector4 clip = mDragPanel.clipRange;
  35.                 Transform dt = mDragPanel.cachedTransform;
  36.                 Vector3 center = dt.localPosition;
  37.                 center.x += clip.x;
  38.                 center.y += clip.y;
  39.                 center = dt.parent.TransformPoint(center);
  40.  
  41.                 // Offset this value by the momentum
  42.                 mDrag.currentMomentum = Vector3.zero;
  43.  
  44.                 // Figure out the difference between the chosen child and the panel's center in local coordinates
  45.                 Vector3 cp = dt.InverseTransformPoint(targetTrans.position);
  46.                 Vector3 cc = dt.InverseTransformPoint(center);
  47.                 Vector3 offset = cp - cc;
  48.  
  49.                 // Offset shouldn't occur if blocked by a zeroed-out scale
  50.                 if (mDrag.scale.x == 0f) offset.x = 0f;
  51.                 if (mDrag.scale.y == 0f) offset.y = 0f;
  52.                 if (mDrag.scale.z == 0f) offset.z = 0f;
  53.  
  54.                 // Spring the panel to this calculated position
  55.                 SpringPanel.Begin(mDrag.gameObject, dt.localPosition - offset, 8f);
  56.         }
  57. }

Cheers,

Gregzo

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Of course. :)

But I recommend not including NGUI files at all. Asset Store overseers are used to packages saying "requires XYZ" and having compile errors unless that package is imported.

Likewise customers prefer to be able to import your package into their existing project (which likely already has NGUI), and including the free version in there is going to make it more difficult for them as they have to manually uncheck a bunch of files from getting imported.

Gregzo

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 64
    • View Profile
Thanks Aren. I will do as you suggest.

Please pace yourself on the new Unity GUI, I'd like to sell a few pickers before it is released!

Many thanks for your understanding,

Gregzo