Author Topic: Any tutorials on how to add stuff programmatically? eg. Add items to scroll grid  (Read 10003 times)

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Pretty much everything I'm finding regards utilising NGUI, at least for those starting with it, revolves around edit-time UI design. For things like dynamic UI generation I'm not finding much. Are there any tutorials on basic activities, like populating a draggable list programmatically?

After some prodding and poking, I got that example working, but it's the sort of bread-and-butter example I'd expect there to be documentation/tutorials on. NGUI is very well catered for design-time tutorials via YouTube vids and the like which is what convinced me to buy it; is this not the case with the programmable side, or have I just not found the place that teaches this?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Many people tend to overthink this for some reason.

Create a prefab out of what a single item within your draggable list should look like then instantiate this prefab as many times as you have items, modifying the instantiated object's data as needed.

Same as any other game object in Unity.

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
I see what you're saying, and that's an option. A tutorial that shows this would be a Good Thing. But also creating stuff from code from scratch is sometimes a good, or even necessary, approach at times, or even just a preferred way of working depending on one's background. There are several aspects to NGUI in code that I'm just sort of stumbling over at the moment. eg. To change a sprite's size, I use its transform. Logical enough, but I was expecting a setSize method or something. To have the sprite not be a gameObject and yet work like a game Object in some aspects is a little odd.

I'm sure it all makes sense when learnt, but making it easier to learn can't be a bad thing! ;) Walking through even really simple stuff can help wonders in getting a feel for how the underlying system is structured, and don't forget that Unity and the like are attracting more and more people to development without robust coding experience.

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Even then, "Just add it as a prefab," is proving non-trivial.

I take my sprite that's presently in the grid and drag it to the assets to create a prefab. I then have to instantiate that and add it to the grid. So I try adding a UISprite, and I try referencing the UIGrid as a GameObject, and various things until I stumble upon accessing the sprite as a GameObject and not a Sprite.
And then a bit of trial and error later, I get:

  1.                 GameObject sp;
  2.  
  3.                 sp = NGUITools.AddChild(gameObject, Instantiate (prefab) as GameObject);
  4.                 sp.GetComponent<UISprite>().name = "Sprite 1";
  5.                 sp.GetComponent<UISprite>().spriteName = "Emoticon - Angry";
  6.  
...which adds the new item to the list and changes it to the desired icon, only it also adds a second item in the middle of the screen. And lots of trial and error later with various combinations of instantiates and assignings, that's still the case - I cannot get a prefab into the grid without also getting a second listItem(clone) to appear. So what's trivial and doesn't need explaining to you is not necessarily piddly easy and obvious to some of us thickos. ;)

Edit: Persisting with this, I've found the solution. The AddChild function instantiates the object (as the documentation states), but wasn't apparent in your reply and didn't sink in on first investigation. But an hour of my time would have been saved if there was a simple tutorial, even just a tiny piece of reference code like:

Quote
public class AddItemsToGrid : MonoBehaviour {
   // Script to add items to a grid. Attach this script to your grid
   public GameObject prefab;        // prefab of sprite to clone
   
   void Start () {
      GameObject sp;     // handle for cloned sprite
      sp = NGUITools.AddChild(gameObject, prefab);      // clone sprite, add as child to grid object
      sp.GetComponent<UISprite>().name = "Sprite 1";     // use sp handle to modify properties such as name...
      sp.GetComponent<UISprite>().spriteName = "Emoticon - Angry";     //.....and sprite graphic
   }
}
« Last Edit: December 30, 2013, 03:39:41 PM by Shifty Geezer »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
NGUITools.AddChild does the instantiation for you. Don't call Instantiate(prefab) yourself.
  1. GameObject go = NGUITools.AddChild(gameObject, prefab);
  2. UISprite sp = go.GetComponent<UISprite>();
  3. //sp.atlas = yourAtlas;
  4. sp.spriteName = "Emoticon - Angry";
If you need examples of doing this 100% programmatically, look inside UICreateWidgetWizard. Everything inside is created using code -- sliders, progress bars, drop-down lists, all of it.

John.Bergman

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 30
    • View Profile
IF you are adding items to a grid, it was mentioned in a similar thread that you need to call UIGrid.Reposition(); I added it to my code and have not really noticed a difference in behavior. 

I did notice that it appears that the behavior of the scrollbar has changed in 3.0.8.f3 though, and after adding all of my items, the panel is now centered, rather than at the top like it was in the previous release(s).