Author Topic: Help migrating inventory system made with UnityGUI to NGUI [SOLVED]  (Read 4643 times)

vexe

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 153
    • View Profile
Hello, I'm completely new to NGUI, I have watched the tutorials, videos and whatnot but I'm still lacking skills.

I have an advanced inventory system that I've been working on for the past 2 months or so here's an old demo http://www.youtube.com/watch?v=J-RxdtzI6Hs

My understanding is that I should first create the buttons/slots and then instantiate them somehow.
But there's a lot of things that I don't know how to achieve, for example in UnityGUI I could provide by buttons with an image, they would show the image but still maintain the button's background. how can I do that in NGUI? I messed around with 'image button' template but didn't seem to know how to make an image appear on it :/

The way I'm drawing my bag (inventory) is that i have a loop looping over my slots and doing:
  1. var rect = new Rect(slots[i][j].position.x, slots[i][j].position.y, slots[i][j].width, slots[i][j].height);
  2. var button = GUI.Button(rect, slots[i][j].texture, slotStyle);
  3. if (button && !contextMenu.shouldBeShown && acceptInput)
  4. // do stuff...
  5.  

How can I convert that code to NGUI?? :/

If you notice from my video, whenever I pickup an item, there's a box drawn around it (using GUI.Box). As well as a custom cursor texture and the item texture (using GUI.DrawTexture)... how can I do those things in NGUI?? :/

(If there is any important stuff that I should be aware of, please let me know if it)

Thanks a lot in advance...
« Last Edit: August 11, 2013, 11:30:25 AM by vexe »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Help migrating inventory system made with UnityGUI to NGUI
« Reply #1 on: August 09, 2013, 04:20:07 PM »
Have you looked at the inventory example that comes with NGUI?

vexe

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 153
    • View Profile
Re: Help migrating inventory system made with UnityGUI to NGUI
« Reply #2 on: August 10, 2013, 03:58:12 AM »
Yes I have looked, I managed to get somewhere... I created an empty scene and managed to instantiate the slots inside a backpack (just like in the example, a slot has a background and an icon) - everything worked well, slots had colliders attached to them, they acted perfectly like buttons, I also attached a collider to the bag and managed to drag it around using UIDragObject (I had to increase the z of the bag collider to detect the buttons/slots clicks)

But the problem is, when I went over to my project and do EXACTLY the same thing, I got the slots drawn but they did NOT detect input :(((( they had colliders attached to them, I tried changing the depth, the z I tried everything, buttons did NOT detect input at all :(( any idea why?

I have the InventoryManager as a child to my panel.

Here are my scripts:
1- Here's how I add a new bag to the inventory (which is a child to the panel)
  1. GameObject bagObject = NGUITools.AddChild(gameObject, bagPrefab);
  2. bagObject.transform.parent = _transform;
  3.  
2- Here's how I'm adding the slots to the bag:
  1. private void PopulateSlots()
  2. {
  3.         for (int i = 0; i < nRows; i++)
  4.         {
  5.                 slots.Add(new List<Slot>());
  6.                 for (int j = 0; j < nCols; j++)
  7.                 {
  8.                         GameObject slotObj = NGUITools.AddChild(gameObject, slotTemplate);
  9.                         Transform slotTrans = slotObj.transform;
  10.                         float x = -bagWidth + SPACE_BETWEEN_SLOTS * (j + 1) + j * slotSize;
  11.                         float y = -SPACE_BETWEEN_SLOTS * (i + 1) - i * slotSize - headerPadding;
  12.                         slotTrans.localPosition = new Vector3(x, y, 0f);
  13.  
  14.                         try
  15.                         {
  16.                                 var slot = slotObj.GetComponent<Slot>();
  17.                                 slot.Init(new Index2D(i, j), slotSize);
  18.                                 slots[i].Add(slot);
  19.                         }
  20.                         catch (Exception e)
  21.                         {
  22.                                 Debug.Log(e.Message);
  23.                         }
  24.                         availableIndices.Add(i, j);
  25.                 }
  26.         }
  27. }
  28.  

This is the Start of the Bag, where I call the previous method, and set the collider right for the bag:
  1. private void Start()
  2. {
  3.         bagHeight = nRows * (slotSize) + HEADER_SPACE + (nRows+1) * SPACE_BETWEEN_SLOTS;
  4.         bagWidth = nCols * (slotSize) + (nCols+1) * SPACE_BETWEEN_SLOTS;
  5.         _collider.size = new Vector3(bagWidth, bagHeight, 1);
  6.         _collider.center = new Vector3(_transform.localPosition.x - bagWidth / 2, _transform.localPosition.y - bagHeight / 2, _collider.center.z);
  7.         background.transform.localScale = new Vector3(bagWidth, bagHeight, 0);
  8.         PopulateSlots();
  9. }
  10.  

Here's how I'm setting the colliders for the slots:
  1. void Awake()
  2. {
  3.         _collider = GetComponent<BoxCollider>();
  4.         background.transform.localScale = new Vector3(size, size, 1);
  5.         icon = null;
  6.         _collider.size = new Vector3(size, size, 1);
  7.         _collider.center = new Vector3(size / 2, -size / 2, _collider.center.z);
  8. }
  9.  

Any idea what's going on? what am I missing? I'm going nuts, I'd REALLY appreciate any help. Thanks.

vexe

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 153
    • View Profile
Re: Help migrating inventory system made with UnityGUI to NGUI
« Reply #3 on: August 10, 2013, 04:05:39 AM »
There's a couple of lines the inventory example that I didn't know what purpose they served...
  1. Bounds b = new Bounds();
  2. b.Encapsulate(new Vector3(padding * 2f + (x + 1) * spacing, -padding * 2f - (y + 1) * spacing, 0f));
  3.  

What do they do, are they related to my problem?

vexe

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 153
    • View Profile
Re: Help migrating inventory system made with UnityGUI to NGUI
« Reply #4 on: August 10, 2013, 05:22:20 AM »
Note that Edit|Project settings|Physics|Raycasts hit triggers is checked.

vexe

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 153
    • View Profile
Re: Help migrating inventory system made with UnityGUI to NGUI
« Reply #5 on: August 10, 2013, 05:54:24 AM »
OK, for some reason, I REALLY DON"T KNOW WHY, but when I recreated everything (which I already did a couple of time with no good) it worked...........

vexe

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 153
    • View Profile
Re: Help migrating inventory system made with UnityGUI to NGUI
« Reply #6 on: August 10, 2013, 10:01:26 AM »
How can I mark this as solved?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Help migrating inventory system made with UnityGUI to NGUI
« Reply #7 on: August 10, 2013, 10:50:18 AM »
Just by editing the title of the original post.