Author Topic: NGUI rigger grid  (Read 3160 times)

isperia

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
    • View Profile
NGUI rigger grid
« on: January 09, 2014, 05:30:13 AM »
Hello

In my project i use Horizontal Scroll view for personage selection.

the code is as follows:
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using k7th;
  5. using LitJson;
  6.  
  7. public class PersonageChooseController : MonoBehaviour, NetworkCallback {
  8.  
  9.         //grid to add personage data to
  10.         public UIGrid grid;
  11.         public GameObject prefabPersonageView;
  12.         public UIScrollView scrollView;
  13.  
  14.         private bool mOnlyCanPlay;
  15.  
  16.         private bool mIsShowing;
  17.         private bool mShouldShowing;
  18.  
  19.         private List<PersonageModel> mPersonages;
  20.  
  21.         public void StartShowing(bool onlyCanPlay) {
  22.                 mOnlyCanPlay = onlyCanPlay;
  23.                 mShouldShowing = true;
  24.  
  25.                 //we need to show stuff.
  26.                 NetworkRequest request = new NetworkRequest("modules/get_personages.php", "preload");
  27.                 request.set ("session", GameEngine.session);
  28.                 request.set ("onlyCanPlay", mOnlyCanPlay?"1":"0");
  29.                 //TODO: fill in the request
  30.                 NetworkModule.getInstance().sendRequest(request, this);
  31.         }
  32.  
  33.         public void StopShowing() {
  34.                 mIsShowing = false;
  35.                 mShouldShowing = false;
  36.                 mOnlyCanPlay = false;
  37.                 mPersonages.Clear();
  38.  
  39.                 while (grid.transform.childCount > 0) {
  40.                         NGUITools.Destroy(grid.transform.GetChild(0).gameObject);
  41.                 }
  42.  
  43.                 grid.transform.DetachChildren();
  44.  
  45.                 grid.Reposition();
  46.                 grid.transform.parent.GetComponent<UIScrollView>().ResetPosition();
  47.         }
  48.        
  49.         // Use this for initialization
  50.         void Start () {
  51.                 mOnlyCanPlay = false;
  52.                 mPersonages = new List<PersonageModel>();
  53.         }      
  54.        
  55.         // Update is called once per frame
  56.         void Update () {
  57.  
  58.         }
  59.  
  60.         public void onResponse(long requestId, string tag, JsonData data, string str_data) {
  61.                 if (tag == "preload") {
  62.                         //ok, we got server response
  63.                         JsonData jsonPersonages = data["personages"];
  64.                         List<PersonageModel> personages = new List<PersonageModel>();//TODO: fill in personage list
  65.                         for (int i = 0; i < jsonPersonages.Count; i++) {
  66.                                 personages.Add(new PersonageModel(jsonPersonages[i]));
  67.                         }
  68.                         foreach (PersonageModel personage in personages) {
  69.                                 AddPersonageToGrid(personage);
  70.                         }
  71.  
  72.                         gameObject.SetActive(true);
  73.                 }
  74.         }
  75.  
  76.         private void AddPersonageToGrid(PersonageModel personage) {
  77.                 //add personage to UI grid
  78.                 GameObject portrait = NGUITools.AddChild(grid.gameObject, prefabPersonageView);
  79.                 PersonagePortraitScript portraitScript = (PersonagePortraitScript) portrait.GetComponent(typeof(PersonagePortraitScript));
  80.                 portraitScript.ApplyModel(personage);
  81.                 UIDragScrollView drag = (UIDragScrollView) portrait.GetComponent(typeof(UIDragScrollView));
  82.                 drag.scrollView = scrollView;
  83.                 grid.Reposition();
  84.                 grid.transform.parent.GetComponent<UIScrollView>().ResetPosition();
  85.         }
  86.        
  87.         public void onError(long requestId, string error) {
  88.  
  89.         }
  90.  
  91.         public void OnCancel() {
  92.                 GameEngine.one.NotifyPersonageChooser(null, true);
  93.         }
  94. }
  95.  

First time i get the list of personages it works ok. (ngui_issue_1_0.png)
But if i hit cancel button (OnCancel) that leads to StopShowing() my grid inserts new clips incorrect
It seems like that are all inserted on the 0th slot.

What should I do to make it work?


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI rigger grid
« Reply #1 on: January 09, 2014, 07:06:17 PM »
When you are adding your items, you are calling grid.Reposition() while the game object is disabled. This won't have any effect. You need to call Reposition() after the game object has been enabled, and only after all you've added all your children.

1. Add all children.
2. Enable the game object
3. Call Reposition().

isperia

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: NGUI rigger grid
« Reply #2 on: January 10, 2014, 03:33:38 AM »
Thank you a lot! I will give it a try