This article is to summarize the inventory pro in the implementation of the General window, but still want to emphasize that the focus of the plug-in is the equipment system rather than the universal window system, so the general window class mentioned here is actually a generic equipment window class (in fact, the plug-in also has non-equipment windows such as NOTICEUI, etc.).
The functions covered in this article are marked with the addition of the following functions:
1, implemented two windows, by clicking on the keyboard I to open or close the window is the toggle function
2. The number of inventory spaces in the equipment window dynamically generated controllable, can be manually configured in the Properties window
3. The window has the function of dragging and dragging
4, window items with drag, and window drag
5, can use the function of the item in the window, the item has the consumption fan-shaped display function
6. Generic window class architecture
Here are some questions to ask yourself before you start:
1, Ugui native realization of the use of objects fan-shaped consumption effect (that is, cooling implementation) is how to achieve?
This is relatively simple, the Ugui picture itself has this radar effect mask, this article is very detailed, here is needless to say
[Getting Started tutorial] using Ugui to achieve skill cooling effect
2. How is the equipment lattice joined with the method mentioned in 1?
Recall here that the equipment lattice is implemented with Inventoryuitemwrapper this UI class, so the answer is in this class, but the use of this class is not easy, it is using the Itemcollectionbase Class (collection Container) dynamically generated, in the previous article said here to review, Here we find that if there is no merit, it takes the Inventorysettingsmanager.itembuttonprefab, which is a device lattice preset
protected Virtual voidFillui () {if(Manuallydefinecollection = =false) {Items=NewInventoryuiitemwrapperbase[initialcollectionsize]; //Fill the container on startup, can add/remove later on for(UINTi =0; i < initialcollectionsize; i++) {Items[i]= Createuiitem<inventoryuiitemwrapper> (i, itembuttonprefab! =)NULL?ItemButtonPrefab:InventorySettingsManager.instance.itemButtonPrefab); } } Else { for(UINTi =0; I < items. Length; i++) {items[i].itemcollection= This; Items[i].index=i; } } } protectedT createuiitem<t> (UINTI, Gameobject prefab)wheret:inventoryuiitemwrapperbase {T Item= Gameobject.instantiate<gameobject> (prefab). Getcomponent<t>(); Item.transform.SetParent (container); Item.transform.localPosition=NewVector3 (item.transform.localposition.x, ITEM.TRANSFORM.LOCALPOSITION.Y,0.0f); Item.itemcollection= This; Item.transform.localScale=Vector3.one; Item.index=i; returnitem; }
Recall that this configuration is a required configuration and must be set in the setting.
Find the preset and find the truth
See, this preset is bound to the Inventoryuiitemwrapper class, and its public field is listed as the Amout text item number, item name (None), icon (image) This is the icons for the objects, such as apples, Cooldown image is the mask layer on the surface of the picture, used for radar effect.
3. How is the equipment lattice bonded with the dynamic items that can be eaten by apples?
From the above picture we also see, in fact, the default icon should be equipped with the background of the lattice (black), how it becomes an apple pear, or sword? Here is a simple clue is to pick up the parcel on the ground, and then in the backpack more than one item (this process does not describe the more complex, left in the later specialized analysis), the trail, eventually to add to the backpack inside, So we go to see the AddItem method in the Itemcollectionbase class, here the method itself is very complex, mainly there are similar to 20 blood bottles 1 dozen things to do when you need to recalculate lattice what is more troublesome, the core function is SetItem
/// <summary> ///This function can being overridden to add custom behavior whenever a object is placed in the inventory. ///This happens when 2 items were swapped, items are merged, anytime an object was put in a slot. /// <b>Does not handle repainting</b> /// </summary> /// <param name= "slots" ></param> /// <param name= "item" ></param> /// <returns>Returns True if the item is set, False if not.</returns> Public Virtual BOOLSetItem (UINTslot, inventoryitembase item) { if(Cansetitem (slot, item) = =false) return false; //_item ugly work around, but no other-to-keep it safe ...Items[slot].item =item; return true; }
Here we remember that Inventoryuiitemwrapper is the item's wrapper, so here's the item set up for the new item, and the rest of it is related to the drawing section, Take a look at the drawing section of Inventoryuiitemwrapper to see how it is displayed and how the cooling effect is achieved.
Public Override voidRepaint () {if(Item! =NULL) { if(Amounttext! =NULL) { //Only show if we have more then 1 item. if(Item.currentstacksize >1) Amounttext.text=item.currentStackSize.ToString (); ElseAmounttext.text=string. Empty; } if(ItemName! =NULL) Itemname.text=Item.name; if(Icon! =NULL) Icon.sprite=Item.icon; } Else { if(Amounttext! =NULL) Amounttext.text=string. Empty; if(ItemName! =NULL) Itemname.text=string. Empty; if(Icon! =NULL) Icon.sprite= Starticon! =NULL?StartIcon:InventorySettingsManager.instance.defaultSlotIcon; } //Repaintcooldown ();//already called by update loop}
Icon.sprite = Item.icon; This line of code we see, actually icon this image Field corresponds to Item.icon, and is a Sprite object (the original sprite is a part of the image), Then look at the code for the update loop, just call it at each frame
Repaintcooldown (), that is, to perform a cooling refresh, the specific code is as follows:
Public Virtual voidRepaintcooldown () {if(Cooldownimage = =NULL) return; if(Item! =NULL) { if(item.isincooldown) {Cooldownimage.fillamount=1.0f-Item.cooldownfactor; return; } } //To avoid GC if(Cooldownimage.fillamount! =0.0f) Cooldownimage.fillamount=0.0f; }
Of course there is the logic to control cooldown, Isincooldown is a property logic inside, including the use of the same type of object cooling control (a bit complicated, not here first table)
4. How is the item triggered for use?
Trigger use must be triggered at the time of the click, bonded Ugui event mechanism, Inventory Pro respectively in Onpointerdown and Onpointerup implemented, there are some touch-related judgments see the source
Public Virtual voidOnpointerdown (Pointereventdata eventData) {if(ItemCollection = =NULL) return; Pointerdownonuielement=inventoryuiutility.clickeduielement; if(Pointerdownonuielement = =false) return; if(InventorySettingsManager.instance.useContextMenu && (Eventdata.button = = InventorySettingsManager.instance.triggerContextMenuButton | |application.ismobileplatform)) {if(Item! =NULL) Triggercontextmenu (); return; } if(InventorySettingsManager.instance.mobileUnstackItemKey = =Mobileuiactions.singletap) {triggerunstack (); return; } Else if(InventorySettingsManager.instance.mobileUseItemButton = =Mobileuiactions.singletap) {triggeruse (); return; } if(Item! =NULL&& pressing = =false&& Time.timesincelevelload-inventorysettingsmanager.instance.mobiledoubletaptime <lastdowntime) { //Did double tap if(InventorySettingsManager.instance.mobileUnstackItemKey = =Mobileuiactions.doubletap) {triggerunstack (); return; } Else if(InventorySettingsManager.instance.mobileUseItemButton = =Mobileuiactions.doubletap) {triggeruse (); return; }} lastdowntime=Time.timesincelevelload; Pressing=true; }
The specific use of the item is implemented in Triggeruse ()
Public Override voidTriggeruse () {if(Item = =NULL) return; if(Itemcollection.canusefromcollection = =false) return; intused =item. Use (); if(Used >=0) {Repaint (); } }
Here the end of the use of the item called the item model to implement, this should be a base class method, the need for a specific subclass of item to implement, concrete implementation and the container has a certain relationship, more complex this article does not table, have the opportunity to expand later, anyway, after the use of the call repaint ( The method is to redraw the refresh operation after the finished item is equipped with a lattice.
Summarize
After answering these four questions, the basic topic of this article is clear.
Generic window class Inventory Pro 2.1.2 Demo1 (next sequel), item consumption fan display function