Author Topic: How to dynamically add items to Scroll View  (Read 5252 times)

Itchigon

  • Guest
How to dynamically add items to Scroll View
« on: February 25, 2013, 11:22:24 AM »
I am having a bit of trouble adding items to a scroll view dynamically.

Say the user has a deck of cards, holding 5 cards in his deck out of a possible 30 in the entire game. How would I add the 5 cards to the Scroll View? The cards are made from a prefab using two planes (Front and Back).

I have set up my Scroll View based on this video I found: https://www.youtube.com/watch?v=AjzJrbiTJgE up to the point where he creates the RootGrid and ItemPref GameObjects. I am assuming the RootGrid should be my UIGrid and the ItemPref should be my Card prefab.

The Card class looks like this:

  1. public class Card : MonoBehaviour {
  2.        
  3.         //Holds all textures for every card so when instantiated can set individual card texture itself
  4.         public Texture[] texArray;
  5.         public Texture backTexture;
  6.         public GameObject face;
  7.         public GameObject back;
  8.  
  9.     //Holds each cards GameObject for use when moving it
  10.     public GameObject cardObject;
  11.  
  12.         //Variables to hold values
  13.         public int topValue;
  14.         public int bottomValue;
  15.         public int leftValue;
  16.         public int rightValue;
  17.     public int id;
  18.     public int deckIdentifier;
  19.        
  20.         //Called when a Card instance is made, sets the texture of the card to the id
  21.         void Start()
  22.         {
  23.         //Set the face of the card to the texture in array based on the cards id (Set when instantiating)
  24.                 face.renderer.material.mainTexture = texArray[id];
  25.  
  26.         //Set the back of the card to backTexture (Same for every card)
  27.                 back.renderer.material.mainTexture = backTexture;
  28.         }
  29.        
  30.         int GetTopValue()
  31.         {
  32.                 return topValue;
  33.         }
  34.        
  35.         int GetBottomValue()
  36.         {
  37.                 return bottomValue;
  38.         }
  39.        
  40.         int GetRightValue()
  41.         {
  42.                 return rightValue;
  43.         }
  44.        
  45.         int GetLeftValue()
  46.         {
  47.                 return leftValue;
  48.         }
  49. }

I tried the following code and found that the grid was not in its correct place on the panel, instead way off the screen.

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class ScrollView : MonoBehaviour {
  5.  
  6.     public GameObject RootGrid;
  7.     public GameObject Card;
  8.     public GameObject panel;
  9.  
  10.         // Use this for initialization
  11.         void Start () {
  12.  
  13.                 for (int i = 0; i < Player.playerDeck.getLength(); i++)
  14.                 {
  15.                         Card = Player.playerDeck.getCard(i).gameObject;
  16.  
  17.                         NGUITools.AddChild(RootGrid, Card);
  18.  
  19.                         Card.transform.parent = RootGrid.transform;
  20.                         RootGrid.GetComponent<UIGrid>().Reposition();
  21.  
  22.                 }
  23.  
  24.         }
  25. }
  26.  



I feel like I'm doing this completely wrong but because I've been following a Russian video I don't fully understand if this is the correct way, any help would be appreciated.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to dynamically add items to Scroll View
« Reply #1 on: February 25, 2013, 06:10:04 PM »
renderer.material.maintexture? Wuh? NGUI doesn't work with renderers. At all. You either change UISprite.spriteName, or UITexture.mainTexture.

Itchigon

  • Guest
Re: How to dynamically add items to Scroll View
« Reply #2 on: February 26, 2013, 06:32:12 AM »
renderer.material.maintexture? Wuh? NGUI doesn't work with renderers. At all. You either change UISprite.spriteName, or UITexture.mainTexture.

So I have a Card prefab which uses two planes (front and back) and is connected to an empty GameObject to show the differing sides of a card. The front side has a texture array to hold all of the images of each individual card and the card sets its own texture when instantiated. How would I change my Card class to use UISprite instead?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to dynamically add items to Scroll View
« Reply #3 on: February 26, 2013, 07:12:18 AM »
You use a UITexture.