Author Topic: Centring items in a UIDraggablePanel...  (Read 2456 times)

afroman

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
Centring items in a UIDraggablePanel...
« on: November 07, 2013, 10:26:04 AM »
I'm trying to centre an item at the end of a UITable list within a UIDraggablePanel. What I'm trying to do is that when someone clicks an item near the end and only part of it is shown, I want the whole item to be displayed instead of it being selected and only half shown.
I stumbled across an old forum http://www.tasharen.com/forum/index.php?topic=180.0 and found the answer, however the math is slightly wrong. If I select the first or last item in the list it moves the entire draggable panel out of the panel clipping until I click it again. I tried to fix it with no luck. Any suggestions? (the code is below)

using UnityEngine;
using System.Collections;
[RequireComponent(typeof(UIDraggablePanel))]
public class UIDraggablePanelSelectedObjectFollower : MonoBehaviour {

    UIDraggablePanel draggablePanel;
    Transform mTrans;
    GameObject mGO;
    GameObject lastFollowed;
   
    void Awake() {
        mGO = gameObject;
        mTrans = transform;
        draggablePanel = GetComponent<UIDraggablePanel>();   
    }
   
    void Update() {
        if (enabled && mGO.active && UICamera.selectedObject != null && UICamera.selectedObject != lastFollowed) {
            if (!UICamera.selectedObject.transform.IsChildOf(draggablePanel.transform)) return;
           
            var draggableBounds = draggablePanel.bounds;
           
            var widgetBounds = NGUIMath.CalculateRelativeWidgetBounds(draggablePanel.transform, UICamera.selectedObject.transform);

                UIPanel mPanel = GetComponent<UIPanel>();
               
            Vector3 constraint = mPanel.CalculateConstrainOffset(widgetBounds.min, widgetBounds.max);
            if (constraint.sqrMagnitude > 0.000001f) {
                SpringPanel.Begin(mPanel.gameObject, mTrans.localPosition + constraint, 13f);
                lastFollowed = UICamera.selectedObject;
            }
        }
    }
}

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Centring items in a UIDraggablePanel...
« Reply #1 on: November 08, 2013, 05:36:58 AM »
Did you see the Skyrim UI example in the Useful Stuff sticky?

afroman

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
Re: Centring items in a UIDraggablePanel...
« Reply #2 on: November 10, 2013, 12:10:49 AM »
I just did, sorry I'm new to the forums :$