Author Topic: ScrollView Clipping  (Read 3273 times)

whilesocold

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
ScrollView Clipping
« on: May 27, 2015, 10:22:31 AM »
I want to make ScrollView object through code and everything works fine except clipping.
Also i tried to add ScrollView and other components to objects in editor without any code. In this case everything works fine and clipping too.
What am i doing wrong?

NGUI 3.7.9, Unity 5.0.2f

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class ScrollViewHelper : MonoBehaviour {
  5.         public GameObject gridNode;
  6.         public GameObject scrollviewNode;
  7.         public GameObject backgroundNode;
  8.  
  9.         [HideInInspector]
  10.         public GameObject[] items;
  11.  
  12.         [HideInInspector]
  13.         public UIGrid grid;
  14.  
  15.         [HideInInspector]
  16.         public UIPanel panel;
  17.  
  18.         [HideInInspector]
  19.         public UICenterOnChild centerOnChild;
  20.  
  21.         [HideInInspector]
  22.         public UIScrollView scrollView;
  23.  
  24.         void Awake() {
  25.                 if (!scrollviewNode) {
  26.                         scrollviewNode = transform.FindChild("scrollview").gameObject;
  27.                 }
  28.                 if (!scrollviewNode) {
  29.                         Debug.LogError("ScrollView was not found!");
  30.                 }
  31.                 if (!gridNode) {
  32.                         gridNode = scrollviewNode.transform.FindChild("grid").gameObject;
  33.                 }
  34.                 if (!gridNode) {
  35.                         Debug.LogError("Grid was not found!");
  36.                 }
  37.                 if (!backgroundNode) {
  38.                         backgroundNode = transform.FindChild("background").gameObject;
  39.                 }
  40.                 if (!backgroundNode) {
  41.                         Debug.LogError("Background was not found!");
  42.                 }
  43.                
  44.                 UISprite backgroundSprite = backgroundNode.GetComponent<UISprite>();
  45.  
  46.                 Rigidbody rigidbody = scrollviewNode.AddComponent<Rigidbody>();
  47.                 rigidbody.useGravity = false;
  48.                 rigidbody.isKinematic = true;
  49.  
  50.                 panel = scrollviewNode.AddComponent<UIPanel>();
  51.                 panel.renderQueue = UIPanel.RenderQueue.Automatic;
  52.                 panel.cullWhileDragging = true;
  53.                 panel.softBorderPadding = true;
  54.                 panel.widgetsAreStatic = false;
  55.                 panel.sortingOrder = 0;
  56.                 panel.generateNormals = false;
  57.                 panel.anchorOffset = false;
  58.                 panel.depth = backgroundSprite.depth + 1;
  59.                 panel.clipSoftness = new Vector2(10, 0);
  60.                 panel.clipping = UIDrawCall.Clipping.SoftClip;
  61.                 panel.SetRect(0, 0, backgroundSprite.width, backgroundSprite.height);
  62.  
  63.                 scrollView = scrollviewNode.AddComponent<UIScrollView>();
  64.                 scrollView.contentPivot = UIWidget.Pivot.Left;
  65.                 scrollView.movement = UIScrollView.Movement.Horizontal;
  66.                 scrollView.disableDragIfFits = true;
  67.                 scrollView.scrollWheelFactor = -2;
  68.  
  69.                 int itemsCount = gridNode.transform.childCount;
  70.                 Bounds itemBounds;
  71.  
  72.                 items = new GameObject[itemsCount];
  73.                 for (int i = 0; i < itemsCount; i++) {
  74.                         GameObject container = gridNode.transform.GetChild(i).gameObject;
  75.                         GameObject item = container.transform.GetChild(0).gameObject;
  76.  
  77.                         UISprite sprite = item.GetComponent<UISprite>();
  78.                         sprite.pivot = UIWidget.Pivot.Left;
  79.                         sprite.depth = (panel.depth + 1);
  80.                        
  81.                         itemBounds = sprite.CalculateBounds();
  82.  
  83.                         BoxCollider boxCollider = container.AddComponent<BoxCollider>();
  84.                         boxCollider.isTrigger = true;
  85.                         boxCollider.size = new Vector3(itemBounds.max.x, itemBounds.min.y * 2, 0);
  86.                        
  87.                         container.AddComponent<UICenterOnClick>();
  88.                         container.AddComponent<UIDragScrollView>();
  89.  
  90.                         items[i] = item;
  91.                 }
  92.  
  93.                 grid = gridNode.AddComponent<UIGrid>();
  94.                 grid.cellWidth = itemBounds.max.x;
  95.                 grid.cellHeight = -itemBounds.min.y * 2;
  96.                 grid.pivot = UIWidget.Pivot.Left;
  97.                 grid.arrangement = UIGrid.Arrangement.Horizontal;
  98.                 grid.hideInactive = true;
  99.                 grid.keepWithinPanel = true;
  100.                 grid.Reposition();
  101.  
  102.                 Vector3 position = scrollviewNode.transform.position;
  103.                 position.x = 0;
  104.                 scrollviewNode.transform.position = position;
  105.         }
  106.  
  107.         void Update() {
  108.         }
  109. }
  110.  
« Last Edit: May 28, 2015, 06:37:49 AM by whilesocold »

ArenMook

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