Author Topic: UIScrollView  (Read 127051 times)

davitosan

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: UIScrollView
« Reply #60 on: June 03, 2014, 03:46:52 PM »
Hello,

I've implemented the UIScrollView using Example 7 Scroll View (Panel) as a reference. Works as I need, thank you very much.

In your example you are emulating a shop where a user can select and purchase items.

My question is, is it be possible to maintain the scroll functionality as is but limit the area that would purchase the item? Adding a buy now button below the gold price per item to where clicking the button and only the buttons executes some method. But dragging anywhere including the button scrolls the view.

If case my question/description isn't clear enough. The only thing that I can think of that is similar is Farm Heroes (mobile) Leaderboard. I cannot find any video of anyone actually doing this though.

davitosan

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: UIScrollView
« Reply #61 on: June 03, 2014, 04:03:23 PM »
Hello,

I've implemented the UIScrollView using Example 7 Scroll View (Panel) as a reference. Works as I need, thank you very much.

In your example you are emulating a shop where a user can select and purchase items.

My question is, is it be possible to maintain the scroll functionality as is but limit the area that would purchase the item? Adding a buy now button below the gold price per item to where clicking the button and only the buttons executes some method. But dragging anywhere including the button scrolls the view.

If case my question/description isn't clear enough. The only thing that I can think of that is similar is Farm Heroes (mobile) Leaderboard. I cannot find any video of anyone actually doing this though.

I figured it out. I guess I just needed to write it out. Here is my solution

Panel
- Scroll View
- - Grid
- - - Item
- - - - Button
- - - Item
- - - - Button

The button contains a collider and a UIForward Events script. I forward the Events to the Item (Buttons Parent).

PoN

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 4
  • Posts: 111
    • View Profile
Re: UIScrollView
« Reply #62 on: June 05, 2014, 09:59:50 PM »
Hi Aren, I would use endless scroll view with my data , which in List or Array, so i init each Item in endless scroll view in the UpdateItem(..) method, then when my last item data inited i would like to stop(lock) scroll possibility and when i scrolling to back that start(unlock) scroll possibility until first item data of List or Array data, it's like endless scroll view with a bounds . How can i do that ? can you help me ?

thanks
Worked on Doc&DogAge Of Fury 3D. Actually working on WarMach.

balzaque

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: UIScrollView
« Reply #63 on: June 05, 2014, 11:01:36 PM »
Hi, I have a scrollview that I need to dynamically change the content size and only show the scrollbars when the content gets big enough, I managed to do that by dynamically changing the height of a child widget.

When it grows bigger the scrollbars show without a problem but when i shrink the widget back the scrollbars stays there till I make a new action or click on something.

  1. container.height = 500;
  2. scrollView.ResetPosition();
  3. scrollBar.value = 0;
  4.  

This is how I'm currently trying to achieve this, not sure what I'm doing wrong. Without reseting both the scrollview and the scrollbar I was getting wrong positions since the scrollview was trying to keep aligned with my first button instead of going back to the start position.

Thanks! =]

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: UIScrollView
« Reply #64 on: June 06, 2014, 02:51:27 AM »
@PoN: If an endless scroll view has bounds, then it's not an endless scroll view anymore. It's just a regular scroll view.

@balzaque: You need to do what UITable does when repositioning its content within a scroll view. Namely calling UIPanel's ConstraintTargetToBounds, then updating the scroll bars. Check UITable's Reposition() function, line 229:
  1.                 if (keepWithinPanel && mPanel != null)
  2.                 {
  3.                         mPanel.ConstrainTargetToBounds(myTrans, true);
  4.                         UIScrollView sv = mPanel.GetComponent<UIScrollView>();
  5.                         if (sv != null) sv.UpdateScrollbars(true);
  6.                 }

balzaque

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: UIScrollView
« Reply #65 on: June 06, 2014, 08:55:44 AM »
Thanks, this is better then the way I was doing it.

I was also setting it at the same time I was starting a tween, had to make sure to call that after the tween had finished.

AbsurdInteractive

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
Re: UIScrollView
« Reply #66 on: June 06, 2014, 07:31:06 PM »
I the latest update my ScrollView is fine in the editor when not in play mode. Once I enter play mode though, on the Awake method something is happening that causes my ScrollView buttons to move downwards slightly then move back up to their correct position. It seems to be awake and not enable because when I deactivate and then activate the UIRoot, it does not do that again. Do you have any suggestions to correct this issue?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: UIScrollView
« Reply #67 on: June 06, 2014, 10:06:45 PM »
Awake() should be used for initializing script's local values. It should never be used for modification of other scripts or their values.

AbsurdInteractive

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
Re: UIScrollView
« Reply #68 on: June 07, 2014, 12:18:27 AM »
Awake() should be used for initializing script's local values. It should never be used for modification of other scripts or their values.

Sorry, I should have been more specific. I don't call any Awake code or do anything special in my script that touches the UI. I just meant since, I've updated to 3.6.1, and 3.6.2, the UIScrollView I setup started to do the behavior I mentioned. I thought it might have to do with changes that were made in the Awake or Start of the NGUI UIScrollview script in v3.6.1.

Is there any code I can call to force the UIScrollview not to move when my scene starts?
« Last Edit: June 07, 2014, 12:37:26 AM by AbsurdInteractive »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: UIScrollView
« Reply #69 on: June 07, 2014, 09:33:36 PM »
Alright, then I'm not quite sure I understand what's happening. Make sure to right-click on the scroll view and choose "Reset Clipping Position" so that the content is positioned properly. Also -- choose the desired Content Origin setting.

AbsurdInteractive

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
Re: UIScrollView
« Reply #70 on: June 10, 2014, 02:59:05 AM »
The issue I'm having looks like it involves UICenterOnChild. The issue seems to be that the center child is not exactly in the center of my scroll view, and it seems when it executes for the first time in Play Mode this causes the content of my ScrollView to snap to the center causing the movement. I know I can manually try to drag the content to the center in the editor, but this seems like it could be a lot of guess and check work with all the different scenes I have with ScrollViews and CenterOnChild. Would it be possible to have UICenterOnChild be able to Execute the script in the Editor when not in Play Mode (similar to how UIGrid's right click Execute Script works), so that we can just execute the script and then save the scene to have everything automatically aligned?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: UIScrollView
« Reply #71 on: June 10, 2014, 03:11:14 AM »
Sure thing. Just add "[ContextMenu("Execute")]" above the UICenterOnChild's Recenter() function.

Edit: You will also need to change the UICenterOnChild's CenterOn function to this:
  1.         void CenterOn (Transform target, Vector3 panelCenter)
  2.         {
  3.                 if (target != null && mScrollView != null && mScrollView.panel != null)
  4.                 {
  5.                         Transform panelTrans = mScrollView.panel.cachedTransform;
  6.                         mCenteredObject = target.gameObject;
  7.  
  8.                         // Figure out the difference between the chosen child and the panel's center in local coordinates
  9.                         Vector3 cp = panelTrans.InverseTransformPoint(target.position);
  10.                         Vector3 cc = panelTrans.InverseTransformPoint(panelCenter);
  11.                         Vector3 localOffset = cp - cc;
  12.  
  13.                         // Offset shouldn't occur if blocked
  14.                         if (!mScrollView.canMoveHorizontally) localOffset.x = 0f;
  15.                         if (!mScrollView.canMoveVertically) localOffset.y = 0f;
  16.                         localOffset.z = 0f;
  17.  
  18.                         // Spring the panel to this calculated position
  19. #if UNITY_EDITOR
  20.                         if (!Application.isPlaying)
  21.                         {
  22.                                 panelTrans.localPosition = panelTrans.localPosition - localOffset;
  23.  
  24.                                 Vector4 co = mScrollView.panel.clipOffset;
  25.                                 co.x += localOffset.x;
  26.                                 co.y += localOffset.y;
  27.                                 mScrollView.panel.clipOffset = co;
  28.                         }
  29.                         else
  30. #endif
  31.                         SpringPanel.Begin(mScrollView.panel.cachedGameObject,
  32.                                 panelTrans.localPosition - localOffset, springStrength).onFinished = onFinished;
  33.                 }
  34.                 else mCenteredObject = null;
  35.         }
« Last Edit: June 10, 2014, 03:21:07 AM by ArenMook »

justkevin

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 8
    • View Profile
Re: UIScrollView
« Reply #72 on: June 12, 2014, 03:11:32 PM »
I'm trying to reset the scroll view to the upper left corner after populating it. Calling ResetPosition() before and after I add elements doesn't seem to work.

As a test, I added ResetPosition to the Update() function, and that got it to reset correctly, but obviously I don't want to call it there.

Here's the class that does the layout:

  1. using UnityEngine;
  2. using System.Collections.Generic;
  3.  
  4. public class ResearchWindow : MonoBehaviour {
  5.     // Contains all our tech tree data:
  6.     public TechTreeManager techTree;
  7.  
  8.     // Each row of this will be a tech lineage:
  9.     public GameObject techLineageTable;
  10.     // The prefab we use for each tech node displayed in the tree:
  11.     public GameObject techButtonPrefab;
  12.  
  13.     // Various UI labels:
  14.     public UILabel researchPoints;
  15.     public UILabel techLabel;
  16.     public UILabel techDescription;
  17.     public UILabel techCost;
  18.  
  19.     private TechNode _activeNode;
  20.  
  21.     /**
  22.      * Set the active node details:
  23.      **/
  24.     public TechNode ActiveNode
  25.     {
  26.         get
  27.         {
  28.             return _activeNode;
  29.         }
  30.         set
  31.         {
  32.             _activeNode = value;
  33.             if (value != null)
  34.             {
  35.                 if (techLabel != null)
  36.                 {
  37.                     techLabel.text = _activeNode.name;
  38.                     techDescription.text = _activeNode.description;
  39.                     techCost.text = "Cost: " + _activeNode.researchPoints + " RP";
  40.                 }
  41.             }
  42.            
  43.         }
  44.     }
  45.  
  46.         void Start () {
  47.         DrawTechTree();
  48.         researchPoints.text = "You have " + GameManager.Instance.player.researchPoints + " Research Points";
  49.         }
  50.        
  51.         // Update is called once per frame
  52.         void Update () {
  53.         // This actually DOES reset the position how we want, but we don't want it here:
  54.         //UIScrollView scrollView = GetComponentInChildren<UIScrollView>();
  55.         //scrollView.ResetPosition();
  56.        
  57.         }
  58.  
  59.     /**
  60.      * Adds the individual lineage tables to the overall tech tree table, and the individual
  61.      * node buttons to each of those linage tables.
  62.      **/
  63.     private void DrawTechTree()
  64.     {
  65.         // Get the scrollview:
  66.         UIScrollView scrollView = GetComponentInChildren<UIScrollView>();
  67.         scrollView.ResetPosition();
  68.         // Get a list of all lineages:
  69.         List<string> lineageIds = techTree.GetLineageIds();
  70.         foreach (string id in lineageIds)
  71.         {
  72.             TechLineage lineage = techTree.GetLineage(id);
  73.             // Create a UITable for this lineage row:
  74.             GameObject lineageRow = NGUITools.AddChild(techLineageTable);
  75.             UITable rowTable = lineageRow.AddComponent<UITable>();
  76.             // For each node in the linage, add a button for it:
  77.             foreach (TechNode node in lineage.nodes)
  78.             {
  79.                 GameObject nodeButton = NGUITools.AddChild(lineageRow, techButtonPrefab);
  80.                 TechNodeButton techNodeButton = nodeButton.GetComponent<TechNodeButton>();
  81.                 techNodeButton.Node = node;
  82.                 techNodeButton.researchWindow = this;
  83.             }
  84.             rowTable.Reposition();
  85.         }
  86.         // This does nothing:
  87.         scrollView.ResetPosition();
  88.  
  89.     }
  90. }
  91.  
  92.  

Any suggestions what I might be doing wrong?


RDeluxe

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 18
    • View Profile
Re: UIScrollView
« Reply #73 on: June 12, 2014, 05:52:08 PM »
I'm trying to reset the scroll view to the upper left corner after populating it. Calling ResetPosition() before and after I add elements doesn't seem to work.

As a test, I added ResetPosition to the Update() function, and that got it to reset correctly, but obviously I don't want to call it there.

[...]


I have the exact same problem with a scrollview containing a UIGrid. When the resolution of the screen changes, I resize my grid, and then i call ResetPosition(). It's working well if I go from a 4/3 resolution to a 16/10, but in the opposite case I have a gap between the top of my grid and the top of my scrollview.
If I put ResetPosition() in the Update() method, it's working perfectly. But, to quote justkevin, "I don't want to do that"

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: UIScrollView
« Reply #74 on: June 13, 2014, 06:20:58 AM »
If you want it to execute once, and you know it works in the Update, then what I'd do is add a flag to your script, such as "bool mReset = false;" that I would set to 'true' in that script's OnEnable() function. In the Update I'd check to see if the flag is 'true', and if so -- ResetPosition().

Same can likely be done by using a co-routine, eliminating the need for 'mReset'.