Author Topic: iOS Scrolling Emulation  (Read 5212 times)

mixd

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
iOS Scrolling Emulation
« on: April 19, 2012, 10:38:28 PM »
This is the only (slightly hacky) way I could come up with to simulate iOS scrolling, WITHOUT changing the core NGUI library. There is currently one flaw which I am unable to solve due to the access level of the underlying momentum.

Known issues: (This would be GREAT if an update gave better access to allow mods like this, or simply built it in)
1) iOS dampens "momentum" down to half of whatever it was at the point of impact. This is currently not possible without modifying the core NGUI library.

Instructions:
1) Add this component to any game object that has a UIDraggablePanel attached to it, and it will magically make your draggable lists more like iOS.

Code:
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class UIDraggablePanelDamper : MonoBehaviour {
  5.  
  6.         public float dampeningFactor = 0.5f;
  7.        
  8.         UIPanel mPanel;
  9.         UIDraggablePanel draggablePanel;
  10.         Vector3 originalScale;
  11.  
  12.         void Start () {
  13.                 draggablePanel = GetComponent<UIDraggablePanel>();
  14.                 mPanel = GetComponent<UIPanel>();
  15.                 originalScale = draggablePanel.scale;
  16.         }
  17.  
  18.         void Update () {
  19.                 Vector3 constraint = mPanel.CalculateConstrainOffset(draggablePanel.bounds.min, draggablePanel.bounds.max);
  20.                 bool outOfBounds = constraint.magnitude > 0.001f;
  21.                 draggablePanel.scale = outOfBounds ?  originalScale * dampeningFactor : originalScale;
  22.         }
  23. }
  24.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: iOS Scrolling Emulation
« Reply #1 on: April 20, 2012, 07:11:19 AM »
Tell me which values you need exposed on the drag script, and I can do that. NGUI library is not a black box for a reason. ;)

mixd

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: iOS Scrolling Emulation
« Reply #2 on: April 20, 2012, 02:15:53 PM »
NGUI is great! The dragging library is one of the RARE things I've had to modify in order to get the behavior I want.

This is pretty much all I need added to have full control over the scrolling behavior without having to mod your core library.

Added in UIDraggablePanel.cs:

  1. public Vector3 momentum
  2.         {
  3.                 get
  4.                 {
  5.                         return mMomentum;
  6.                 }
  7.                
  8.                 set
  9.                 {
  10.                         mMomentum = value;
  11.                 }
  12.         }
  13.  


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: iOS Scrolling Emulation
« Reply #3 on: April 20, 2012, 02:22:31 PM »
Sure thing, I've added it to my copy so you'll see it in the next release. I called it 'currentMomentum' though.

mixd

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: iOS Scrolling Emulation
« Reply #4 on: April 20, 2012, 02:26:24 PM »
Code for iOS behavior now becomes:

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class UIDraggablePanelDamper : MonoBehaviour {
  5.  
  6.         public float scrollDampeningFactor = 0.5f;
  7.         public float momentumDampingFactor = 0.5f;
  8.        
  9.         UIPanel mPanel;
  10.         UIDraggablePanel draggablePanel;
  11.         Vector3 originalScale;
  12.         bool momentumSet = false;
  13.  
  14.         void Start () {
  15.                 draggablePanel = GetComponent<UIDraggablePanel>();
  16.                 mPanel = GetComponent<UIPanel>();
  17.                 originalScale = draggablePanel.scale;
  18.         }
  19.  
  20.         void Update () {
  21.                 Vector3 constraint = mPanel.CalculateConstrainOffset(draggablePanel.bounds.min, draggablePanel.bounds.max);
  22.                 bool outOfBounds = constraint.magnitude > 0.001f;
  23.                 draggablePanel.scale = outOfBounds ?  originalScale * scrollDampeningFactor : originalScale;
  24.                 if(draggablePanel.currentMomentum == Vector3.zero) {
  25.                         momentumSet = false;
  26.                 }
  27.                 else if(outOfBounds && !momentumSet) {
  28.                         draggablePanel.currentMomentum = draggablePanel.currentMomentum * momentumDampingFactor;
  29.                         momentumSet = true;
  30.                 }
  31.         }
  32. }
  33.  
  34.