Author Topic: ScrollView increase scale of object in center  (Read 6564 times)

maga

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 2
    • View Profile
ScrollView increase scale of object in center
« on: July 05, 2013, 02:30:42 AM »
"Example 7 - Scroll View (Panel)" this scene of NGUI has really nice scrollview component, but just wondering if possible to scale a bit center object of scrollview while dragging or centering, using any other of NGUI components?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: ScrollView increase scale of object in center
« Reply #1 on: July 05, 2013, 08:20:54 AM »
You'd have to scale it yourself using custom logic. Don't forget to remove the "widgets are static" flag from the draggable panel or scaling won't work.

maga

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 2
    • View Profile
Re: ScrollView increase scale of object in center
« Reply #2 on: July 05, 2013, 04:59:59 PM »
probably not the best way to achieve this effect, but at least it working correctly :)

here is the code:

My Hierarchy:
DraggableArea (UIPanel, UIDraggablePanel)
->Grid (UIGrid, UICenterOnChild, UIScaleOnCenter [script code below])
->->Item_1 (UIDragPanelContents)
->->->Background (UISprite)
->->Item_2 (UIDragPanelContents)
->->->Background (UISprite)
->->...
->->->...
->->Item_n (UIDragPanelContents)
->->->Background (UISprite)

  1. using UnityEngine;
  2.  
  3. public class UIScaleOnCenter : MonoBehaviour {
  4.        
  5.         /// <summary>
  6.         /// Scale applies to all involved childs, dependent on distance from center object,
  7.         /// the higher distance the lower scale modifier is applied.
  8.         /// </summary>
  9.        
  10.         public Vector3 scaleIncreaseBy = Vector3.zero;
  11.        
  12.         /// <summary>
  13.         /// Variable describe how much items around centered object will take effect of scale.
  14.         /// </summary>
  15.        
  16.         public int childsInvolved = 2;
  17.         private float maxOffset;
  18.        
  19.         private UISprite [] items;
  20.         private Vector3 [] startingScale;
  21.        
  22.         private bool scrollViewIsHorizontal;
  23.        
  24.         UIDraggablePanel mDrag;
  25.        
  26.         void Start() {
  27.                 if(mDrag == null) {
  28.                         mDrag = NGUITools.FindInParents<UIDraggablePanel>(gameObject);
  29.                        
  30.                         if(mDrag == null) {
  31.                                 Debug.LogWarning(GetType() + " requires " + typeof(UIDraggablePanel) + " on a parent object in order to work", this);
  32.                                 Destroy(this);
  33.                         }
  34.                        
  35.                 }
  36.                
  37.                 UIGrid grid = GetComponent<UIGrid>();
  38.                
  39.                 if(grid == null) {
  40.                         Debug.LogWarning(GetType() + " requires " + typeof(UIGrid) + " object in order to work", this);
  41.                         Destroy(this);
  42.                 }
  43.                
  44.                 if(grid.arrangement == UIGrid.Arrangement.Horizontal) {
  45.                         maxOffset = grid.cellWidth * childsInvolved;
  46.                         scrollViewIsHorizontal = true;
  47.                 } else {
  48.                         scrollViewIsHorizontal = false;
  49.                         maxOffset = grid.cellHeight * childsInvolved;
  50.                 }
  51.                
  52.                 Transform trans = transform;
  53.                 int childCount = trans.childCount;
  54.                
  55.                 items = new UISprite[childCount];
  56.                 startingScale = new Vector3[childCount];
  57.                
  58.                 for(int i = 0; i < childCount; i++) {
  59.                         Transform child = trans.GetChild(i);
  60.                         items[i] = child.GetComponentInChildren<UISprite>();
  61.                         startingScale[i] = child.localScale;
  62.                 }
  63.         }
  64.        
  65.         void Update() {
  66.  
  67.                 if (mDrag.panel == null) return;
  68.                
  69.                 Transform dt = mDrag.panel.cachedTransform;
  70.                 Vector3 center = dt.localPosition;
  71.  
  72.                 Transform trans = transform;
  73.                
  74.                 for(int i = 0, imax = items.Length; i < imax; i++) {
  75.                         float distance = (scrollViewIsHorizontal == true) ? Mathf.Abs(center.x + items[i].transform.parent.localPosition.x):
  76.                                                                                                                                 Mathf.Abs(center.y + items[i].transform.parent.localPosition.y);
  77.                        
  78.                         float modifierStrength = 1.0f - distance / maxOffset;
  79.                        
  80.                         if(modifierStrength > 0.0f) {
  81.                                 items[i].depth = Mathf.RoundToInt(modifierStrength * 10.0f);
  82.                                 items[i].transform.parent.localScale = new Vector3(startingScale[i].x + scaleIncreaseBy.x * modifierStrength,
  83.                                                                                                                                    startingScale[i].y + scaleIncreaseBy.y * modifierStrength,
  84.                                                                                                                                    startingScale[i].z + scaleIncreaseBy.z * modifierStrength);
  85.                         } else {
  86.                                 items[i].depth = 0;
  87.                                 items[i].transform.parent.localScale = new Vector3(startingScale[i].x, startingScale[i].y, startingScale[i].z);
  88.                         }
  89.                 }
  90.         }
  91. }
  92.  

unitymoo

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 8
    • View Profile
Re: ScrollView increase scale of object in center
« Reply #3 on: October 28, 2016, 09:58:43 AM »
Hi,

I tried implementing this code in my project and I'm getting this error:
"The type or namespace name "UIDraggable Panel" could not be found. Are you missing an assembly reference?"

I've searched for this error online and other projects with this error are nothing to do with Unity / NGUI.

Any tips for how to make this code work?

It would be great if NGUI would include a code example with scroll view that scales center object.

Thanks!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: ScrollView increase scale of object in center
« Reply #4 on: October 30, 2016, 12:26:02 PM »
This code is ancient. UIDraggablePanel is NGUI 2 stuff. NGUI 3 calls it UIScrollView.