Based on the MVC model, the knapsack system is realized by Ugui.
Includes click and drag logic, with source code, engineering and analysis added later.
Model Layer
Using unityengine;using unityengine.ui;using system.collections;using system.collections.generic;///<summary >///scripting Features: MVC mode--model layer, define item structure, save item Data//Add object: Bag Backpack (empty object under canvas)//Copyright notice: Copyright (c) duzixi.com All rights Re served///Date Created: 2015.5.8///knowledge Points:///1. mvc///2. Custom Classes///3. Nesting of classes//</summary>public class Itemmodel:monobehaviour {//Item class definition public class Item {public stri ng name; Item name Public Sprite img; Item Picture//Constructor public item (string name, Sprite img) {this.name = name; This.img = img; }} public static list<item> items; Save Object Collection//Item picture array public int size = 16; Sprite[] Sprites; void Awake ()//data initialization {items = new list<item> ();//Initialize list<item> sprites = new Sprite[size] ; Initialize item list for (int i = 0; i < Bagview.row, i++) {for (int j = 0; J < Bagview.col; J + +) according to row and column values { Items. ADD (New Item ("", null)); }}//"Note" The following parts of the actual development should be replaced by the database for (int i = 0; i < size; i++) {String name = i < 9? "0" + (i + 1): "" + (i + 1); Sprites[i] = resources.load (name, typeof (Sprite)) as sprite;items[i] = new Item ("", Sprites[i]);}}}
View Layer
Using unityengine;using unityengine.ui;using system.collections;///<summary>///scripting feature: MVC mode--view View, control how the backpack is displayed Add object: Bag Backpack (empty object under canvas)//Copyright notice: Copyright (c) duzixi.com All rights reserved///created: 2015.05.08///Modified record: 2015.0 5.18 Add number///Modify Record: 2015.07.03 package shows the item lattice method//Knowledge Points:///1. mvc///2. Ugui//</summary>public class Bagview:monobehaviour {//Backpack specification public static int row = 4; Line public static int col = 5; column//Backpack grid public gameobject grid; float width; Lattice width float height; Lattice height//Awake () () {width = grid) with Grid presets. Getcomponent<recttransform> (). Rect.width; Height = grid. Getcomponent<recttransform> (). Rect.height; }//Initial state: tile lattice, creating backpack void Start () {for (int i = 0; i < row; i++) {for (int j = 0; J < Col; J + +) { Calculate ID Value (item list subscript) int id = j + i * COL; Instantiate a lattice preset, press the wide-height layout gameobject Itemgrid = Instantiate (grid, transForm.position + New Vector3 (J * Width,-i * height, 0), quaternion.identity) as Gameobject; Sets the instantiated lattice object to the child object of the Backpack itemGrid.transform.SetParent (transform);//Call custom method: Displays the lattice content of an ID showitem (Itemgrid.trans form, id); The lattice Pickupdrop component number, pick down with itemgrid.getcomponent<pickupdrop> (). Gridid = ID; }}}//re-refresh the backpack display (when the item position changes) public void Showitems () {for (int i = 0; i < row * COL; i++) { Transform Itemgrid = Transform. Getchild (i); ShowItem (Itemgrid, i); }}//show item lattice private void ShowItem (Transform itemgrid, int id) {//Show item name Text Txtugui = Itemgrid.get Componentinchildren<text> (); Txtugui.text = itemmodel.items[id].name;//Gets the image component of the item icon image Imageugui = itemgrid.getchild (0). Getcomponent<image> ();//If there is an item, display the picture if (itemmodel.items[id].img! = null) {Imageugui.color = Color.White;} else {/ /otherwise does not show imageugui.color = Color.clear;} ImageugUi.sprite = itemmodel.items[id].img;}}
Using unityengine;using unityengine.ui;using system.collections;///<summary>///scripting feature: Let the pickup backpack items move with the mouse// Add object: Pickeditem picked items//copyright notice: Copyright (c) duzixi.com All rights reserved///Date created: 2015.05.18///Modified record: 2015.07.03 add shot Line ignoring///knowledge Points:///1. Ugui Recttransform, Anchor Point, center Point///2. Ignore Ray interface icanvasraycastfilter///</summary>public class Movewithmouse:monobehaviour, Icanvasraycastfilter {RectT Ransform rect; Get Ugui positioning component image icon; Displays the icon for the currently picked item void Awake () {rect = getcomponent<recttransform> (); The "note" icon object is the No. 0 sub-object icon = transform. Getchild (0). Getcomponent<image> (); }void Update () {//Position rect.anchoredposition3d = Input.mouseposition with mouse location to icon picture; Depending on whether there is a picture to determine the transparency if (PickUpDrop.pickedItem.img! = null) {Icon.color = Color.White;} else {icon.color = color.clear;} Icon.sprite = PickUpDrop.pickedItem.img;} Ignore ray on mouse icon public bool Israycastlocationvalid (Vector2 sp, Camera Eventcamera) {return false;}}
Control Layer
Using unityengine;using unityengine.eventsystems;using system.collections;///<summary>///scripting feature: MVC mode-- control controls, backpack items placed///Add object: Item check preset///Copyright notice: Copyright (c) duzixi.com All rights reserved///Date created: 2015.05.18 Duzi xi.com///modification Record: 2015.07.03 add ray ignore///Knowledge Points:///1. Ugui, MVC design pattern///</summary>public class Pickupdrop:monobehaviour, idrophandler {public int gridid; public static Itemmodel.item Pickeditem; The currently picked item void Start () {//initializes the currently picked item as empty Pickeditem = new Itemmodel.item ("", null);} Backpack Core Logic: Exchange public static void Swapitem (int gridid) {//swap backpack for items and pickup items Itemmodel.item temp = Pickedit Em Pickeditem = Itemmodel.items[gridid]; Itemmodel.items[gridid] = temp; Refresh Backpack display Gameobject.find ("bag"). Getcomponent<bagview> (). Showitems (); }//when the item button is clicked (click Trigger mode) public void Drop () {Swapitem (gridid); }//When the item is placed in the grid (drag trigger mode) public void OnDrop (Pointereventdata eventData) {swapitem (Gridid);}}
Using unityengine;using unityengine.ui;using unityengine.eventsystems;using system.collections;///<summary>/ Scripting Features: MVC mode--control control, Operation Logic, drag-and-drop event handling//Add object: Bag Backpack (empty object under canvas)//Copyright notice: Copyright (c) duzixi.com all rights R eserved///creation Time: 2015.07.03///knowledge Points:///1. unityengine.eventsystem///2. Ibegindraghandlder, Idraghandler, ienddraghander///</summary>public class Dragevent:monobehaviour, ibegindraghandler,idraghandler,ienddraghandler {int gridid = 0; Lattice number void start () {Gridid = Getcomponentinparent<pickupdrop> (). Gridid;//Get Lattice number} //start dragging public void Onbegindrag (Pointereventdata eventData) { //Call interchange Method Pickupdrop.swapitem (Gridid);} Drag in public void Ondrag (Pointereventdata eventData) {//"note" to implement the interface even if there is no code handling, otherwise the drag is unsuccessful} //end drag public void Onenddrag (Pointereventdata eventData) { //Call interchange Method Pickupdrop.swapitem (Gridid);}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Unity" Ugui implementation of knapsack system based on MVC pattern