Unity:装备的总体设计

目录

一、 人员分工

二、 业务流程图

三、 系统功能结构图

四、 UML类图

五、 详细设计

(1)背包界面

(2)物品栏界面

(3)人物装备界面

六、 遇到的问题和解决方法

七、附录(代码实现)


 

一、 人员分工

装备组人员:3人

分工:

1.

系统分析

项目创建

公共类设计

2.

系统目标

编码规范

运行环境

数据库与数据表设计

3.

人员分工

开发问题及解决

系统功能结构

业务流程图

UML类图

模块设计

二、 业务流程图

装备业务流程图

Unity:装备的总体设计_第1张图片

三、 系统功能结构图

装备功能模块

Unity:装备的总体设计_第2张图片

 

四、 UML类图

Unity:装备的总体设计_第3张图片

五、 详细设计

(1)背包界面

Press”i”,打开背包栏

Unity:装备的总体设计_第4张图片

(2)物品栏界面

Unity:装备的总体设计_第5张图片

(3)人物装备界面

Press”c”,打开人物装备栏

Unity:装备的总体设计_第6张图片

六、 遇到的问题和解决方法

Unity:装备的总体设计_第7张图片
float x = y * gameObject.GetComponent().aspect;
Unity:装备的总体设计_第8张图片
Request error (error): UnityEditor.AsyncHTTPClient:Done(State, Int32)
Unity:装备的总体设计_第9张图片

 

七、附录(代码实现)

using UnityEngine;

using System.Collections;

using System.Collections.Generic;

 

[System.Serializable]

public class Item

{

    public string itemName;                                     //itemName of the item

    public int itemID;                                          //itemID of the item

    public string itemDesc;                                     //itemDesc of the item

    public Sprite itemIcon;                                     //itemIcon of the item

    public GameObject itemModel;                                //itemModel of the item

    public int itemValue = 1;                                   //itemValue is at start 1

    public ItemType itemType;                                   //itemType of the Item

    public float itemWeight;                                    //itemWeight of the item

    public int maxStack = 1;

    public int indexItemInList = 999;    

    public int rarity;

 

    [SerializeField]

    public List itemAttributes = new List();    

    

    public Item(){}

 

    public Item(string name, int id, string desc, Sprite icon, GameObject model, int maxStack, ItemType type, string sendmessagetext, List itemAttributes)                 //function to create a instance of the Item

    {

        itemName = name;

        itemID = id;

        itemDesc = desc;

        itemIcon = icon;

        itemModel = model;

        itemType = type;

        this.maxStack = maxStack;

        this.itemAttributes = itemAttributes;

    }

 

    public Item getCopy()

    {

        return (Item)this.MemberwiseClone();        

    }   

    

    

}

using UnityEngine;

using System.Collections;

public class PickUpItem : MonoBehaviour

{

    public Item item;

    private Inventory _inventory;

    private GameObject _player;

    // Use this for initialization

 

    void Start()

    {

        _player = GameObject.FindGameObjectWithTag("Player");

        if (_player != null)

            _inventory = _player.GetComponent().inventory.GetComponent();

    }

 

    // Update is called once per frame

    void Update()

    {

        if (_inventory != null && Input.GetKeyDown(KeyCode.E))

        {

            float distance = Vector3.Distance(this.gameObject.transform.position, _player.transform.position);

 

            if (distance <= 3)

            {

                bool check = _inventory.checkIfItemAllreadyExist(item.itemID, item.itemValue);

                if (check)

                    Destroy(this.gameObject);

                else if (_inventory.ItemsInInventory.Count < (_inventory.width * _inventory.height))

                {

                    _inventory.addItemToInventory(item.itemID, item.itemValue);

                    _inventory.updateItemList();

                    _inventory.stackableSettings();

                    Destroy(this.gameObject);

                }

 

            }

        }

        if (Input.GetMouseButton(0))

        {

            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);//从摄像机发出到点击坐标的射线

            RaycastHit hitInfo;

            if (Physics.Raycast(ray, out hitInfo))

            {

                Debug.DrawLine(ray.origin, hitInfo.point);//划出射线,只有在scene视图中才能看到

                GameObject gameObj = hitInfo.collider.gameObject;

                Debug.Log("click object name is " + gameObj.name);

                if (gameObj.tag == "Item")//当射线碰撞目标为boot类型的物品 ,执行拾取操作

                {

                    _inventory.addItemToInventory(item.itemID, item.itemValue);

                    _inventory.updateItemList();

                    _inventory.stackableSettings();

                    Destroy(this.gameObject);

                    Debug.Log("pick up!");

                }

            }

        }

    }

 

}

using UnityEngine;

using System.Collections;

using UnityEngine.UI;

using UnityEngine.EventSystems;

using System;

 

public class ConsumeItem : MonoBehaviour, IPointerDownHandler

{

    public Item item;

    private static Tooltip tooltip;

    public ItemType[] itemTypeOfSlot;

    public static EquipmentSystem eS;

    public GameObject duplication;

    public static GameObject mainInventory;

 

    void Start()

    {

        item = GetComponent().item;

        if (GameObject.FindGameObjectWithTag("Tooltip") != null)

            tooltip = GameObject.FindGameObjectWithTag("Tooltip").GetComponent();

        if (GameObject.FindGameObjectWithTag("EquipmentSystem") != null)

            eS = GameObject.FindGameObjectWithTag("Player").GetComponent().characterSystem.GetComponent();

 

        if (GameObject.FindGameObjectWithTag("MainInventory") != null)

            mainInventory = GameObject.FindGameObjectWithTag("MainInventory");

    }

 

    public void OnPointerDown(PointerEventData data)

    {

        if (this.gameObject.transform.parent.parent.parent.GetComponent() == null)

        {

            bool gearable = false;

            Inventory inventory = transform.parent.parent.parent.GetComponent();

 

            if (eS != null)

                itemTypeOfSlot = eS.itemTypeOfSlots;

 

            if (data.button == PointerEventData.InputButton.Right)

            {

                //item from craft system to inventory

                if (transform.parent.GetComponent() != null)

                {

                    bool check = GameObject.FindGameObjectWithTag("Player").GetComponent().inventory.GetComponent().checkIfItemAllreadyExist(item.itemID, item.itemValue);

 

                    if (!check)

                    {

                        GameObject.FindGameObjectWithTag("Player").GetComponent().inventory.GetComponent().addItemToInventory(item.itemID, item.itemValue);

                        GameObject.FindGameObjectWithTag("Player").GetComponent().inventory.GetComponent().stackableSettings();

                    }

                    CraftSystem cS = GameObject.FindGameObjectWithTag("Player").GetComponent().craftSystem.GetComponent();

                    cS.deleteItems(item);

                    CraftResultSlot result = GameObject.FindGameObjectWithTag("Player").GetComponent().craftSystem.transform.GetChild(3).GetComponent();

                    result.temp = 0;

                    tooltip.deactivateTooltip();

                    gearable = true;

                    GameObject.FindGameObjectWithTag("MainInventory").GetComponent().updateItemList();

                }

                else

                {

                    bool stop = false;

                    if (eS != null)

                    {

                        for (int i = 0; i < eS.slotsInTotal; i++)

                        {

                            if (itemTypeOfSlot[i].Equals(item.itemType))

                            {

                                if (eS.transform.GetChild(1).GetChild(i).childCount == 0)

                                {

                                    stop = true;

                                    if (eS.transform.GetChild(1).GetChild(i).parent.parent.GetComponent() != null && this.gameObject.transform.parent.parent.parent.GetComponent() != null) { }

                                    else                                    

                                        inventory.EquiptItem(item);

                                    

                                    this.gameObject.transform.SetParent(eS.transform.GetChild(1).GetChild(i));

                                    this.gameObject.GetComponent().localPosition = Vector3.zero;

                                    eS.gameObject.GetComponent().updateItemList();

                                    inventory.updateItemList();

                                    gearable = true;

                                    if (duplication != null)

                                        Destroy(duplication.gameObject);

                                    break;

                                }

                            }

                        }

 

 

                        if (!stop)

                        {

                            for (int i = 0; i < eS.slotsInTotal; i++)

                            {

                                if (itemTypeOfSlot[i].Equals(item.itemType))

                                {

                                    if (eS.transform.GetChild(1).GetChild(i).childCount != 0)

                                    {

                                        GameObject otherItemFromCharacterSystem = eS.transform.GetChild(1).GetChild(i).GetChild(0).gameObject;

                                        Item otherSlotItem = otherItemFromCharacterSystem.GetComponent().item;

                                        if (item.itemType == ItemType.UFPS_Weapon)

                                        {

                                            inventory.UnEquipItem1(otherItemFromCharacterSystem.GetComponent().item);

                                            inventory.EquiptItem(item);

                                        }

                                        else

                                        {

                                            inventory.EquiptItem(item);

                                            if (item.itemType != ItemType.Backpack)

                                                inventory.UnEquipItem1(otherItemFromCharacterSystem.GetComponent().item);

                                        }

                                        if (this == null)

                                        {

                                            GameObject dropItem = (GameObject)Instantiate(otherSlotItem.itemModel);

                                            dropItem.AddComponent();

                                            dropItem.GetComponent().item = otherSlotItem;

                                            dropItem.transform.localPosition = GameObject.FindGameObjectWithTag("Player").transform.localPosition;

                                            inventory.OnUpdateItemList();

                                        }

                                        else

                                        {

                                            otherItemFromCharacterSystem.transform.SetParent(this.transform.parent);

                                            otherItemFromCharacterSystem.GetComponent().localPosition = Vector3.zero;

                                            if (this.gameObject.transform.parent.parent.parent.GetComponent() != null)

                                                createDuplication(otherItemFromCharacterSystem);

 

                                            this.gameObject.transform.SetParent(eS.transform.GetChild(1).GetChild(i));

                                            this.gameObject.GetComponent().localPosition = Vector3.zero;

                                        }                                        

                                        

                                        gearable = true;                                        

                                        if (duplication != null)

                                            Destroy(duplication.gameObject);

                                        eS.gameObject.GetComponent().updateItemList();

                                        inventory.OnUpdateItemList();

                                        break;

                                    }

                                }

                            }

                        }

 

                    }

 

                }

                if (!gearable && item.itemType != ItemType.UFPS_Ammo && item.itemType != ItemType.UFPS_Grenade)

                {

 

                    Item itemFromDup = null;

                    if (duplication != null)

                        itemFromDup = duplication.GetComponent().item;

 

                    inventory.ConsumeItem(item);

 

                    item.itemValue--;

                    if (itemFromDup != null)

                    {

                        duplication.GetComponent().item.itemValue--;

                        if (itemFromDup.itemValue <= 0)

                        {

                            if (tooltip != null)

                                tooltip.deactivateTooltip();

                            inventory.deleteItemFromInventory(item);

                            Destroy(duplication.gameObject);

                        }

                    }

                    if (item.itemValue <= 0)

                    {

                        if (tooltip != null)

                            tooltip.deactivateTooltip();

                        inventory.deleteItemFromInventory(item);

                        Destroy(this.gameObject);                        

                    }

 

                }

                

            }

            

 

        }

    }    

 

    public void consumeIt()

    {

        Inventory inventory = transform.parent.parent.parent.GetComponent();

 

        bool gearable = false;

 

        if (GameObject.FindGameObjectWithTag("EquipmentSystem") != null)

            eS = GameObject.FindGameObjectWithTag("Player").GetComponent().characterSystem.GetComponent();

 

        if (eS != null)

            itemTypeOfSlot = eS.itemTypeOfSlots;

 

        Item itemFromDup = null;

        if (duplication != null)

            itemFromDup = duplication.GetComponent().item;       

 

        bool stop = false;

        if (eS != null)

        {

            

            for (int i = 0; i < eS.slotsInTotal; i++)

            {

                if (itemTypeOfSlot[i].Equals(item.itemType))

                {

                    if (eS.transform.GetChild(1).GetChild(i).childCount == 0)

                    {

                        stop = true;

                        this.gameObject.transform.SetParent(eS.transform.GetChild(1).GetChild(i));

                        this.gameObject.GetComponent().localPosition = Vector3.zero;

                        eS.gameObject.GetComponent().updateItemList();

                        inventory.updateItemList();

                        inventory.EquiptItem(item);

                        gearable = true;

                        if (duplication != null)

                            Destroy(duplication.gameObject);

                        break;

                    }

                }

            }

 

            if (!stop)

            {

                for (int i = 0; i < eS.slotsInTotal; i++)

                {

                    if (itemTypeOfSlot[i].Equals(item.itemType))

                    {

                        if (eS.transform.GetChild(1).GetChild(i).childCount != 0)

                        {

                            GameObject otherItemFromCharacterSystem = eS.transform.GetChild(1).GetChild(i).GetChild(0).gameObject;

                            Item otherSlotItem = otherItemFromCharacterSystem.GetComponent().item;

                            if (item.itemType == ItemType.UFPS_Weapon)

                            {

                                inventory.UnEquipItem1(otherItemFromCharacterSystem.GetComponent().item);

                                inventory.EquiptItem(item);

                            }

                            else

                            {

                                inventory.EquiptItem(item);

                                if (item.itemType != ItemType.Backpack)

                                    inventory.UnEquipItem1(otherItemFromCharacterSystem.GetComponent().item);

                            }

                            if (this == null)

                            {

                                GameObject dropItem = (GameObject)Instantiate(otherSlotItem.itemModel);

                                dropItem.AddComponent();

                                dropItem.GetComponent().item = otherSlotItem;

                                dropItem.transform.localPosition = GameObject.FindGameObjectWithTag("Player").transform.localPosition;

                                inventory.OnUpdateItemList();

                            }

                            else

                            {

                                otherItemFromCharacterSystem.transform.SetParent(this.transform.parent);

                                otherItemFromCharacterSystem.GetComponent().localPosition = Vector3.zero;

                                if (this.gameObject.transform.parent.parent.parent.GetComponent() != null)

                                    createDuplication(otherItemFromCharacterSystem);

 

                                this.gameObject.transform.SetParent(eS.transform.GetChild(1).GetChild(i));

                                this.gameObject.GetComponent().localPosition = Vector3.zero;

                            }

 

                            gearable = true;

                            if (duplication != null)

                                Destroy(duplication.gameObject);

                            eS.gameObject.GetComponent().updateItemList();

                            inventory.OnUpdateItemList();

                            break;                           

                        }

                    }

                }

            }

 

 

        }

        if (!gearable && item.itemType != ItemType.UFPS_Ammo && item.itemType != ItemType.UFPS_Grenade)

        {

 

            if (duplication != null)

                itemFromDup = duplication.GetComponent().item;

 

            inventory.ConsumeItem(item);

 

 

            item.itemValue--;

            if (itemFromDup != null)

            {

                duplication.GetComponent().item.itemValue--;

                if (itemFromDup.itemValue <= 0)

                {

                    if (tooltip != null)

                        tooltip.deactivateTooltip();

                    inventory.deleteItemFromInventory(item);

                    Destroy(duplication.gameObject);

 

                }

            }

            if (item.itemValue <= 0)

            {

                if (tooltip != null)

                    tooltip.deactivateTooltip();

                inventory.deleteItemFromInventory(item);

                Destroy(this.gameObject);

            }

 

        }        

    }

 

    public void createDuplication(GameObject Item)

    {

        Item item = Item.GetComponent().item;

        GameObject dup = mainInventory.GetComponent().addItemToInventory(item.itemID, item.itemValue);

        Item.GetComponent().duplication = dup;

        dup.GetComponent().duplication = Item;

    }

}

using UnityEngine;

using System.Collections;

#if UNITY_EDITOR

using UnityEditor;

#endif

using UnityEngine.UI;

using System.Collections.Generic;

 

public class Inventory : MonoBehaviour

{

    //Prefabs

    [SerializeField]

    private GameObject prefabCanvasWithPanel;

    [SerializeField]

    private GameObject prefabSlot;

    [SerializeField]

    private GameObject prefabSlotContainer;

    [SerializeField]

    private GameObject prefabItem;

    [SerializeField]

    private GameObject prefabDraggingItemContainer;

    [SerializeField]

    private GameObject prefabPanel;

 

    //Itemdatabase

    [SerializeField]

    private ItemDataBaseList itemDatabase;

 

    //GameObjects which are alive

    [SerializeField]

    private string inventoryTitle;

    [SerializeField]

    private RectTransform PanelRectTransform;

    [SerializeField]

    private Image PanelImage;

    [SerializeField]

    private GameObject SlotContainer;

    [SerializeField]

    private GameObject DraggingItemContainer;

    [SerializeField]

    private RectTransform SlotContainerRectTransform;

    [SerializeField]

    private GridLayoutGroup SlotGridLayout;

    [SerializeField]

    private RectTransform SlotGridRectTransform;

 

    //Inventory Settings

    [SerializeField]

    public bool mainInventory;

    [SerializeField]

    public List ItemsInInventory = new List();

    [SerializeField]

    public int height;

    [SerializeField]

    public int width;

    [SerializeField]

    public bool stackable;

    [SerializeField]

    public static bool inventoryOpen;

 

 

    //GUI Settings

    [SerializeField]

    public int slotSize;

    [SerializeField]

    public int iconSize;

    [SerializeField]

    public int paddingBetweenX;

    [SerializeField]

    public int paddingBetweenY;

    [SerializeField]

    public int paddingLeft;

    [SerializeField]

    public int paddingRight;

    [SerializeField]

    public int paddingBottom;

    [SerializeField]

    public int paddingTop;

    [SerializeField]

    public int positionNumberX;

    [SerializeField]

    public int positionNumberY;

 

    InputManager inputManagerDatabase;

 

    //event delegates for consuming, gearing

    public delegate void ItemDelegate(Item item);

    public static event ItemDelegate ItemConsumed;

    public static event ItemDelegate ItemEquip;

    public static event ItemDelegate UnEquipItem;

 

    public delegate void InventoryOpened();

    public static event InventoryOpened InventoryOpen;

    public static event InventoryOpened AllInventoriesClosed;

 

    void Start()

    {

        if (transform.GetComponent() == null)

            this.gameObject.SetActive(false);

 

        updateItemList();

 

        inputManagerDatabase = (InputManager)Resources.Load("InputManager");

    }

 

    public void sortItems()

    {

        int empty = -1;

        for (int i = 0; i < SlotContainer.transform.childCount; i++)

        {

            if (SlotContainer.transform.GetChild(i).childCount == 0 && empty == -1)

                empty = i;

            else

            {

                if (empty > -1)

                {

                    if (SlotContainer.transform.GetChild(i).childCount != 0)

                    {

                        RectTransform rect = SlotContainer.transform.GetChild(i).GetChild(0).GetComponent();

                        SlotContainer.transform.GetChild(i).GetChild(0).transform.SetParent(SlotContainer.transform.GetChild(empty).transform);

                        rect.localPosition = Vector3.zero;

                        i = empty + 1;

                        empty = i;

                    }

                }

            }

        }

    }

 

    void Update()

    {

        updateItemIndex();

    }

 

 

    public void setAsMain()

    {

        if (mainInventory)

            this.gameObject.tag = "Untagged";

        else if (!mainInventory)

            this.gameObject.tag = "MainInventory";

    }

 

    public void OnUpdateItemList()

    {

        updateItemList();

    }

 

    public void closeInventory()

    {

        this.gameObject.SetActive(false);

        checkIfAllInventoryClosed();

    }

 

    public void openInventory()

    {

        this.gameObject.SetActive(true);

        if (InventoryOpen != null)

            InventoryOpen();

    }

 

    public void checkIfAllInventoryClosed()

    {

        GameObject canvas = GameObject.FindGameObjectWithTag("Canvas");

 

        for (int i = 0; i < canvas.transform.childCount; i++)

        {

            GameObject child = canvas.transform.GetChild(i).gameObject;

            if (!child.activeSelf && (child.tag == "EquipmentSystem" || child.tag == "Panel" || child.tag == "MainInventory" || child.tag == "CraftSystem"))

            {

                if (AllInventoriesClosed != null && i == canvas.transform.childCount - 1)

                    AllInventoriesClosed();

            }

            else if (child.activeSelf && (child.tag == "EquipmentSystem" || child.tag == "Panel" || child.tag == "MainInventory" || child.tag == "CraftSystem"))

                break;

 

            else if (i == canvas.transform.childCount - 1)

            {

                if (AllInventoriesClosed != null)

                    AllInventoriesClosed();

            }

 

 

        }

    }

    public void ConsumeItem(Item item)

    {

        if (ItemConsumed != null)

            ItemConsumed(item);

    }

 

    public void EquiptItem(Item item)

    {

        if (ItemEquip != null)

            ItemEquip(item);

    }

 

    public void UnEquipItem1(Item item)

    {

        if (UnEquipItem != null)

            UnEquipItem(item);

    }

 

#if UNITY_EDITOR

    [MenuItem("Master System/Create/Inventory and Storage")]        //creating the menu item

    public static void menuItemCreateInventory()       //create the inventory at start

    {

        GameObject Canvas = null;

        if (GameObject.FindGameObjectWithTag("Canvas") == null)

        {

            GameObject inventory = new GameObject();

            inventory.name = "Inventories";

            Canvas = (GameObject)Instantiate(Resources.Load("Prefabs/Canvas - Inventory") as GameObject);

            Canvas.transform.SetParent(inventory.transform, true);

            GameObject panel = (GameObject)Instantiate(Resources.Load("Prefabs/Panel - Inventory") as GameObject);

            panel.GetComponent().localPosition = new Vector3(0, 0, 0);

            panel.transform.SetParent(Canvas.transform, true);

            GameObject draggingItem = (GameObject)Instantiate(Resources.Load("Prefabs/DraggingItem") as GameObject);

            draggingItem.transform.SetParent(Canvas.transform, true);

            Inventory temp = panel.AddComponent();

            Instantiate(Resources.Load("Prefabs/EventSystem") as GameObject);

            panel.AddComponent();

            temp.getPrefabs();

        }

        else

        {

            GameObject panel = (GameObject)Instantiate(Resources.Load("Prefabs/Panel - Inventory") as GameObject);

            panel.transform.SetParent(GameObject.FindGameObjectWithTag("Canvas").transform, true);

            panel.GetComponent().localPosition = new Vector3(0, 0, 0);

            Inventory temp = panel.AddComponent();

            panel.AddComponent();

            DestroyImmediate(GameObject.FindGameObjectWithTag("DraggingItem"));

            GameObject draggingItem = (GameObject)Instantiate(Resources.Load("Prefabs/DraggingItem") as GameObject);

            draggingItem.transform.SetParent(GameObject.FindGameObjectWithTag("Canvas").transform, true);

            temp.getPrefabs();

        }

    }

#endif

 

    public void setImportantVariables()

    {

        PanelRectTransform = GetComponent();

        SlotContainer = transform.GetChild(1).gameObject;

        SlotGridLayout = SlotContainer.GetComponent();

        SlotGridRectTransform = SlotContainer.GetComponent();

    }

 

    public void getPrefabs()

    {

        if (prefabCanvasWithPanel == null)

            prefabCanvasWithPanel = Resources.Load("Prefabs/Canvas - Inventory") as GameObject;

        if (prefabSlot == null)

            prefabSlot = Resources.Load("Prefabs/Slot - Inventory") as GameObject;

        if (prefabSlotContainer == null)

            prefabSlotContainer = Resources.Load("Prefabs/Slots - Inventory") as GameObject;

        if (prefabItem == null)

            prefabItem = Resources.Load("Prefabs/Item") as GameObject;

        if (itemDatabase == null)

            itemDatabase = (ItemDataBaseList)Resources.Load("ItemDatabase");

        if (prefabDraggingItemContainer == null)

            prefabDraggingItemContainer = Resources.Load("Prefabs/DraggingItem") as GameObject;

        if (prefabPanel == null)

            prefabPanel = Resources.Load("Prefabs/Panel - Inventory") as GameObject;

 

        setImportantVariables();

        setDefaultSettings();

        adjustInventorySize();

        updateSlotAmount(width, height);

        updateSlotSize();

        updatePadding(paddingBetweenX, paddingBetweenY);

 

    }

    public void updateItemList()

    {

        ItemsInInventory.Clear();

        for (int i = 0; i < SlotContainer.transform.childCount; i++)

        {

            Transform trans = SlotContainer.transform.GetChild(i);

            if (trans.childCount != 0)

            {

                ItemsInInventory.Add(trans.GetChild(0).GetComponent().item);

            }

        }

 

    }

 

    public bool characterSystem()

    {

        if (GetComponent() != null)

            return true;

        else

            return false;

    }

    public void setDefaultSettings()

    {

        if (GetComponent() == null && GetComponent() == null && GetComponent() == null)

        {

            height = 5;

            width = 5;

 

            slotSize = 50;

            iconSize = 45;

 

            paddingBetweenX = 5;

            paddingBetweenY = 5;

            paddingTop = 35;

            paddingBottom = 10;

            paddingLeft = 10;

            paddingRight = 10;

        }

        else if (GetComponent() != null)

        {

            height = 1;

            width = 9;

            slotSize = 50;

            iconSize = 45;

            paddingBetweenX = 5;

            paddingBetweenY = 5;

            paddingTop = 10;

            paddingBottom = 10;

            paddingLeft = 10;

            paddingRight = 10;

        }

        else if (GetComponent() != null)

        {

            height = 3;

            width = 3;

            slotSize = 55;

            iconSize = 45;

 

            paddingBetweenX = 5;

            paddingBetweenY = 5;

            paddingTop = 35;

            paddingBottom = 95;

            paddingLeft = 25;

            paddingRight = 25;

        }

        else

        {

            height = 4;

            width = 2;

 

            slotSize = 50;

            iconSize = 45;

 

            paddingBetweenX = 100;

            paddingBetweenY = 20;

            paddingTop = 35;

            paddingBottom = 10;

            paddingLeft = 10;

            paddingRight = 10;

        }

    }

    public void adjustInventorySize()

    {

        int x = (((width * slotSize) + ((width - 1) * paddingBetweenX)) + paddingLeft + paddingRight);

        int y = (((height * slotSize) + ((height - 1) * paddingBetweenY)) + paddingTop + paddingBottom);

        PanelRectTransform.sizeDelta = new Vector2(x, y);

 

        SlotGridRectTransform.sizeDelta = new Vector2(x, y);

    }

    public void updateSlotAmount(int width, int height)

    {

        if (prefabSlot == null)

            prefabSlot = Resources.Load("Prefabs/Slot - Inventory") as GameObject;

 

        if (SlotContainer == null)

        {

            SlotContainer = (GameObject)Instantiate(prefabSlotContainer);

            SlotContainer.transform.SetParent(PanelRectTransform.transform);

            SlotContainerRectTransform = SlotContainer.GetComponent();

            SlotGridRectTransform = SlotContainer.GetComponent();

            SlotGridLayout = SlotContainer.GetComponent();

        }

 

        if (SlotContainerRectTransform == null)

            SlotContainerRectTransform = SlotContainer.GetComponent();

 

        SlotContainerRectTransform.localPosition = Vector3.zero;

 

        List itemsToMove = new List();

        List slotList = new List();

        foreach (Transform child in SlotContainer.transform)

        {

            if (child.tag == "Slot") { slotList.Add(child.gameObject); }

        }

        while (slotList.Count > width * height)

        {

            GameObject go = slotList[slotList.Count - 1];

            ItemOnObject itemInSlot = go.GetComponentInChildren();

            if (itemInSlot != null)

            {

                itemsToMove.Add(itemInSlot.item);

                ItemsInInventory.Remove(itemInSlot.item);

            }

            slotList.Remove(go);

            DestroyImmediate(go);

        }

 

        if (slotList.Count < width * height)

        {

            for (int i = slotList.Count; i < (width * height); i++)

            {

                GameObject Slot = (GameObject)Instantiate(prefabSlot);

                Slot.name = (slotList.Count + 1).ToString();

                Slot.transform.SetParent(SlotContainer.transform);

                slotList.Add(Slot);

            }

        }

 

        if (itemsToMove != null && ItemsInInventory.Count < width * height)

        {

            foreach (Item i in itemsToMove)

            {

                addItemToInventory(i.itemID);

            }

        }

 

        setImportantVariables();

    }

 

    public void updateSlotAmount()

    {

 

        if (prefabSlot == null)

            prefabSlot = Resources.Load("Prefabs/Slot - Inventory") as GameObject;

 

        if (SlotContainer == null)

        {

            SlotContainer = (GameObject)Instantiate(prefabSlotContainer);

            SlotContainer.transform.SetParent(PanelRectTransform.transform);

            SlotContainerRectTransform = SlotContainer.GetComponent();

            SlotGridRectTransform = SlotContainer.GetComponent();

            SlotGridLayout = SlotContainer.GetComponent();

        }

 

        if (SlotContainerRectTransform == null)

            SlotContainerRectTransform = SlotContainer.GetComponent();

        SlotContainerRectTransform.localPosition = Vector3.zero;

 

        List itemsToMove = new List();

        List slotList = new List();

        foreach (Transform child in SlotContainer.transform)

        {

            if (child.tag == "Slot") { slotList.Add(child.gameObject); }

        }

 

        while (slotList.Count > width * height)

        {

            GameObject go = slotList[slotList.Count - 1];

            ItemOnObject itemInSlot = go.GetComponentInChildren();

            if (itemInSlot != null)

            {

                itemsToMove.Add(itemInSlot.item);

                ItemsInInventory.Remove(itemInSlot.item);

            }

            slotList.Remove(go);

            DestroyImmediate(go);

        }

        if (slotList.Count < width * height)

        {

            for (int i = slotList.Count; i < (width * height); i++)

            {

                GameObject Slot = (GameObject)Instantiate(prefabSlot);

                Slot.name = (slotList.Count + 1).ToString();

                Slot.transform.SetParent(SlotContainer.transform);

                slotList.Add(Slot);

            }

        }

        if (itemsToMove != null && ItemsInInventory.Count < width * height)

        {

            foreach (Item i in itemsToMove)

            {

                addItemToInventory(i.itemID);

            }

        }

        setImportantVariables();

    }

    public void updateSlotSize(int slotSize)

    {

        SlotGridLayout.cellSize = new Vector2(slotSize, slotSize);

 

        updateItemSize();

    }

    public void updateSlotSize()

    {

        SlotGridLayout.cellSize = new Vector2(slotSize, slotSize);

 

        updateItemSize();

    }

    void updateItemSize()

    {

        for (int i = 0; i < SlotContainer.transform.childCount; i++)

        {

            if (SlotContainer.transform.GetChild(i).childCount > 0)

            {

                SlotContainer.transform.GetChild(i).GetChild(0).GetComponent().sizeDelta = new Vector2(slotSize, slotSize);

                SlotContainer.transform.GetChild(i).GetChild(0).GetChild(2).GetComponent().sizeDelta = new Vector2(slotSize, slotSize);

            }

 

        }

    }

 

    public void updatePadding(int spacingBetweenX, int spacingBetweenY)

    {

        SlotGridLayout.spacing = new Vector2(paddingBetweenX, paddingBetweenY);

        SlotGridLayout.padding.bottom = paddingBottom;

        SlotGridLayout.padding.right = paddingRight;

        SlotGridLayout.padding.left = paddingLeft;

        SlotGridLayout.padding.top = paddingTop;

    }

 

    public void updatePadding()

    {

        SlotGridLayout.spacing = new Vector2(paddingBetweenX, paddingBetweenY);

        SlotGridLayout.padding.bottom = paddingBottom;

        SlotGridLayout.padding.right = paddingRight;

        SlotGridLayout.padding.left = paddingLeft;

        SlotGridLayout.padding.top = paddingTop;

    }

 

    public void addAllItemsToInventory()

    {

        for (int k = 0; k < ItemsInInventory.Count; k++)

        {

            for (int i = 0; i < SlotContainer.transform.childCount; i++)

            {

                if (SlotContainer.transform.GetChild(i).childCount == 0)

                {

                    GameObject item = (GameObject)Instantiate(prefabItem);

                    item.GetComponent().item = ItemsInInventory[k];

                    item.transform.SetParent(SlotContainer.transform.GetChild(i));

                    item.GetComponent().localPosition = Vector3.zero;

                    item.transform.GetChild(0).GetComponent().sprite = ItemsInInventory[k].itemIcon;

                    updateItemSize();

                    break;

                }

            }

        }

        stackableSettings();

    }

    public bool checkIfItemAllreadyExist(int itemID, int itemValue)

    {

        updateItemList();

        int stack;

        for (int i = 0; i < ItemsInInventory.Count; i++)

        {

            if (ItemsInInventory[i].itemID == itemID)

            {

                stack = ItemsInInventory[i].itemValue + itemValue;

                if (stack <= ItemsInInventory[i].maxStack)

                {

                    ItemsInInventory[i].itemValue = stack;

                    GameObject temp = getItemGameObject(ItemsInInventory[i]);

                    if (temp != null && temp.GetComponent().duplication != null)

                        temp.GetComponent().duplication.GetComponent().item.itemValue = stack;

                    return true;

                }

            }

        }

        return false;

    }

    public void addItemToInventory(int id)

    {

        for (int i = 0; i < SlotContainer.transform.childCount; i++)

        {

            if (SlotContainer.transform.GetChild(i).childCount == 0)

            {

                GameObject item = (GameObject)Instantiate(prefabItem);

                item.GetComponent().item = itemDatabase.getItemByID(id);

                item.transform.SetParent(SlotContainer.transform.GetChild(i));

                item.GetComponent().localPosition = Vector3.zero;

                item.transform.GetChild(0).GetComponent().sprite = item.GetComponent().item.itemIcon;

                item.GetComponent().item.indexItemInList = ItemsInInventory.Count - 1;

                break;

            }

        }

        stackableSettings();

        updateItemList();

 

    }

    public GameObject addItemToInventory(int id, int value)

    {

        for (int i = 0; i < SlotContainer.transform.childCount; i++)

        {

            if (SlotContainer.transform.GetChild(i).childCount == 0)

            {

                GameObject item = (GameObject)Instantiate(prefabItem);

                ItemOnObject itemOnObject = item.GetComponent();

                itemOnObject.item = itemDatabase.getItemByID(id);

                if (itemOnObject.item.itemValue <= itemOnObject.item.maxStack && value <= itemOnObject.item.maxStack)

                    itemOnObject.item.itemValue = value;

                else

                    itemOnObject.item.itemValue = 1;

                item.transform.SetParent(SlotContainer.transform.GetChild(i));

                item.GetComponent().localPosition = Vector3.zero;

                item.transform.GetChild(0).GetComponent().sprite = itemOnObject.item.itemIcon;

                itemOnObject.item.indexItemInList = ItemsInInventory.Count - 1;

                if (inputManagerDatabase == null)

                    inputManagerDatabase = (InputManager)Resources.Load("InputManager");

                return item;

            }

        }

 

        stackableSettings();

        updateItemList();

        return null;

    }

    public void addItemToInventoryStorage(int itemID, int value)

    {

 

        for (int i = 0; i < SlotContainer.transform.childCount; i++)

        {

            if (SlotContainer.transform.GetChild(i).childCount == 0)

            {

                GameObject item = (GameObject)Instantiate(prefabItem);

                ItemOnObject itemOnObject = item.GetComponent();

                itemOnObject.item = itemDatabase.getItemByID(itemID);

                if (itemOnObject.item.itemValue < itemOnObject.item.maxStack && value <= itemOnObject.item.maxStack)

                    itemOnObject.item.itemValue = value;

                else

                    itemOnObject.item.itemValue = 1;

                item.transform.SetParent(SlotContainer.transform.GetChild(i));

                item.GetComponent().localPosition = Vector3.zero;

                itemOnObject.item.indexItemInList = 999;

                if (inputManagerDatabase == null)

                    inputManagerDatabase = (InputManager)Resources.Load("InputManager");

                updateItemSize();

                stackableSettings();

                break;

            }

        }

        stackableSettings();

        updateItemList();

    }

    public void updateIconSize(int iconSize)

    {

        for (int i = 0; i < SlotContainer.transform.childCount; i++)

        {

            if (SlotContainer.transform.GetChild(i).childCount > 0)

            {

                SlotContainer.transform.GetChild(i).GetChild(0).GetChild(0).GetComponent().sizeDelta = new Vector2(iconSize, iconSize);

            }

        }

        updateItemSize();

 

    }

    public void updateIconSize()

    {

        for (int i = 0; i < SlotContainer.transform.childCount; i++)

        {

            if (SlotContainer.transform.GetChild(i).childCount > 0)

            {

                SlotContainer.transform.GetChild(i).GetChild(0).GetChild(0).GetComponent().sizeDelta = new Vector2(iconSize, iconSize);

            }

        }

        updateItemSize();

 

    }

 

    public void stackableSettings(bool stackable, Vector3 posi)

    {

        for (int i = 0; i < SlotContainer.transform.childCount; i++)

        {

            if (SlotContainer.transform.GetChild(i).childCount > 0)

            {

                ItemOnObject item = SlotContainer.transform.GetChild(i).GetChild(0).GetComponent();

                if (item.item.maxStack > 1)

                {

                    RectTransform textRectTransform = SlotContainer.transform.GetChild(i).GetChild(0).GetChild(1).GetComponent();

                    Text text = SlotContainer.transform.GetChild(i).GetChild(0).GetChild(1).GetComponent();

                    text.text = "" + item.item.itemValue;

                    text.enabled = stackable;

                    textRectTransform.localPosition = posi;

                }

            }

        }

    }

 

 

    public void deleteAllItems()

    {

        for (int i = 0; i < SlotContainer.transform.childCount; i++)

        {

            if (SlotContainer.transform.GetChild(i).childCount != 0)

            {

                Destroy(SlotContainer.transform.GetChild(i).GetChild(0).gameObject);

            }

        }

    }

 

    public List getItemList()

    {

        List theList = new List();

        for (int i = 0; i < SlotContainer.transform.childCount; i++)

        {

            if (SlotContainer.transform.GetChild(i).childCount != 0)

                theList.Add(SlotContainer.transform.GetChild(i).GetChild(0).GetComponent().item);

        }

        return theList;

    }

 

    public void stackableSettings()

    {

        for (int i = 0; i < SlotContainer.transform.childCount; i++)

        {

            if (SlotContainer.transform.GetChild(i).childCount > 0)

            {

                ItemOnObject item = SlotContainer.transform.GetChild(i).GetChild(0).GetComponent();

                if (item.item.maxStack > 1)

                {

                    RectTransform textRectTransform = SlotContainer.transform.GetChild(i).GetChild(0).GetChild(1).GetComponent();

                    Text text = SlotContainer.transform.GetChild(i).GetChild(0).GetChild(1).GetComponent();

                    text.text = "" + item.item.itemValue;

                    text.enabled = stackable;

                    textRectTransform.localPosition = new Vector3(positionNumberX, positionNumberY, 0);

                }

                else

                {

                    Text text = SlotContainer.transform.GetChild(i).GetChild(0).GetChild(1).GetComponent();

                    text.enabled = false;

                }

            }

        }

 

    }

 

    public GameObject getItemGameObjectByName(Item item)

    {

        for (int k = 0; k < SlotContainer.transform.childCount; k++)

        {

            if (SlotContainer.transform.GetChild(k).childCount != 0)

            {

                GameObject itemGameObject = SlotContainer.transform.GetChild(k).GetChild(0).gameObject;

                Item itemObject = itemGameObject.GetComponent().item;

                if (itemObject.itemName.Equals(item.itemName))

                {

                    return itemGameObject;

                }

            }

        }

        return null;

    }

 

    public GameObject getItemGameObject(Item item)

    {

        for (int k = 0; k < SlotContainer.transform.childCount; k++)

        {

            if (SlotContainer.transform.GetChild(k).childCount != 0)

            {

                GameObject itemGameObject = SlotContainer.transform.GetChild(k).GetChild(0).gameObject;

                Item itemObject = itemGameObject.GetComponent().item;

                if (itemObject.Equals(item))

                {

                    return itemGameObject;

                }

            }

        }

        return null;

    }

 

 

 

    public void changeInventoryPanelDesign(Image image)

    {

        Image inventoryDesign = transform.GetChild(0).GetChild(0).GetComponent();

        inventoryDesign.sprite = (Sprite)image.sprite;

        inventoryDesign.color = image.color;

        inventoryDesign.material = image.material;

        inventoryDesign.type = image.type;

        inventoryDesign.fillCenter = image.fillCenter;

    }

 

    public void deleteItem(Item item)

    {

        for (int i = 0; i < ItemsInInventory.Count; i++)

        {

            if (item.Equals(ItemsInInventory[i]))

                ItemsInInventory.RemoveAt(item.indexItemInList);

        }

    }

 

    

 

    public void deleteItemFromInventory(Item item)

    {

        for (int i = 0; i < ItemsInInventory.Count; i++)

        {

            if (item.Equals(ItemsInInventory[i]))

                ItemsInInventory.RemoveAt(i);

        }

    }

 

    public void deleteItemFromInventoryWithGameObject(Item item)

    {

        for (int i = 0; i < ItemsInInventory.Count; i++)

        {

            if (item.Equals(ItemsInInventory[i]))

            {

                ItemsInInventory.RemoveAt(i);

            }

        }

 

        for (int k = 0; k < SlotContainer.transform.childCount; k++)

        {

            if (SlotContainer.transform.GetChild(k).childCount != 0)

            {

                GameObject itemGameObject = SlotContainer.transform.GetChild(k).GetChild(0).gameObject;

                Item itemObject = itemGameObject.GetComponent().item;

                if (itemObject.Equals(item))

                {

                    Destroy(itemGameObject);

                    break;

                }

            }

        }

    }

 

    public int getPositionOfItem(Item item)

    {

        for (int i = 0; i < SlotContainer.transform.childCount; i++)

        {

            if (SlotContainer.transform.GetChild(i).childCount != 0)

            {

                Item item2 = SlotContainer.transform.GetChild(i).GetChild(0).GetComponent().item;

                if (item.Equals(item2))

                    return i;

            }

        }

        return -1;

    }

 

    public void addItemToInventory(int ignoreSlot, int itemID, int itemValue)

    {

 

        for (int i = 0; i < SlotContainer.transform.childCount; i++)

        {

            if (SlotContainer.transform.GetChild(i).childCount == 0 && i != ignoreSlot)

            {

                GameObject item = (GameObject)Instantiate(prefabItem);

                ItemOnObject itemOnObject = item.GetComponent();

                itemOnObject.item = itemDatabase.getItemByID(itemID);

                if (itemOnObject.item.itemValue < itemOnObject.item.maxStack && itemValue <= itemOnObject.item.maxStack)

                    itemOnObject.item.itemValue = itemValue;

                else

                    itemOnObject.item.itemValue = 1;

                item.transform.SetParent(SlotContainer.transform.GetChild(i));

                item.GetComponent().localPosition = Vector3.zero;

                itemOnObject.item.indexItemInList = 999;

                updateItemSize();

                stackableSettings();

                break;

            }

        }

        stackableSettings();

        updateItemList();

    }

 

 

 

 

    public void updateItemIndex()

    {

        for (int i = 0; i < ItemsInInventory.Count; i++)

        {

            ItemsInInventory[i].indexItemInList = i;

        }

    }

}

using System;

using UnityEngine;

using System.Collections;

using System.Collections.Generic;

#if UNITY_EDITOR

using UnityEditor;

#endif

using UnityEngine.UI;

using UnityEngine.EventSystems;

 

 

public class EquipmentSystem : MonoBehaviour

{

    [SerializeField]

    public int slotsInTotal;

    [SerializeField]

    public ItemType[] itemTypeOfSlots = new ItemType[999];

 

    void Start()

    {

        ConsumeItem.eS = GetComponent();

    }

 

    public void getSlotsInTotal()

    {

        Inventory inv = GetComponent();

        slotsInTotal = inv.width * inv.height;

    }

#if UNITY_EDITOR

    [MenuItem("Master System/Create/Equipment")]        //creating the menu item

    public static void menuItemCreateInventory()       //create the inventory at start

    {

        GameObject Canvas = null;

        if (GameObject.FindGameObjectWithTag("Canvas") == null)

        {

            GameObject inventory = new GameObject();

            inventory.name = "Inventories";

            Canvas = (GameObject)Instantiate(Resources.Load("Prefabs/Canvas - Inventory") as GameObject);

            Canvas.transform.SetParent(inventory.transform, true);

            GameObject panel = (GameObject)Instantiate(Resources.Load("Prefabs/Panel - EquipmentSystem") as GameObject);

            panel.GetComponent().localPosition = new Vector3(0, 0, 0);

            panel.transform.SetParent(Canvas.transform, true);

            GameObject draggingItem = (GameObject)Instantiate(Resources.Load("Prefabs/DraggingItem") as GameObject);

            draggingItem.transform.SetParent(Canvas.transform, true);

            Instantiate(Resources.Load("Prefabs/EventSystem") as GameObject);

            Inventory inv = panel.AddComponent();

            panel.AddComponent();

            panel.AddComponent();

            inv.getPrefabs();

        }

        else

        {

            GameObject panel = (GameObject)Instantiate(Resources.Load("Prefabs/Panel - EquipmentSystem") as GameObject);

            panel.transform.SetParent(GameObject.FindGameObjectWithTag("Canvas").transform, true);

            panel.GetComponent().localPosition = new Vector3(0, 0, 0);

            Inventory inv = panel.AddComponent();

            panel.AddComponent();

            DestroyImmediate(GameObject.FindGameObjectWithTag("DraggingItem"));

            GameObject draggingItem = (GameObject)Instantiate(Resources.Load("Prefabs/DraggingItem") as GameObject);

            panel.AddComponent();

            draggingItem.transform.SetParent(GameObject.FindGameObjectWithTag("Canvas").transform, true);

            inv.getPrefabs();

        }

    }

#endif

 

}

 

using UnityEngine;

using System.Collections;

#if UNITY_EDITOR

using UnityEditor;

#endif

using System.Collections.Generic;

using System.Linq;

 

public class Hotbar : MonoBehaviour

{

 

    [SerializeField]

    public KeyCode[] keyCodesForSlots = new KeyCode[999];

    [SerializeField]

    public int slotsInTotal;

 

#if UNITY_EDITOR

    [MenuItem("Master System/Create/Hotbar")]        //creating the menu item

    public static void menuItemCreateInventory()       //create the inventory at start

    {

        GameObject Canvas = null;

        if (GameObject.FindGameObjectWithTag("Canvas") == null)

        {

            GameObject inventory = new GameObject();

            inventory.name = "Inventories";

            Canvas = (GameObject)Instantiate(Resources.Load("Prefabs/Canvas - Inventory") as GameObject);

            Canvas.transform.SetParent(inventory.transform, true);

            GameObject panel = (GameObject)Instantiate(Resources.Load("Prefabs/Panel - Hotbar") as GameObject);

            panel.GetComponent().localPosition = new Vector3(0, 0, 0);

            panel.transform.SetParent(Canvas.transform, true);

            GameObject draggingItem = (GameObject)Instantiate(Resources.Load("Prefabs/DraggingItem") as GameObject);

            Instantiate(Resources.Load("Prefabs/EventSystem") as GameObject);

            draggingItem.transform.SetParent(Canvas.transform, true);

            Inventory inv = panel.AddComponent();

            panel.AddComponent();

            panel.AddComponent();

            inv.getPrefabs();

        }

        else

        {

            GameObject panel = (GameObject)Instantiate(Resources.Load("Prefabs/Panel - Hotbar") as GameObject);

            panel.transform.SetParent(GameObject.FindGameObjectWithTag("Canvas").transform, true);

            panel.GetComponent().localPosition = new Vector3(0, 0, 0);

            Inventory inv = panel.AddComponent();

            panel.AddComponent();

            DestroyImmediate(GameObject.FindGameObjectWithTag("DraggingItem"));

            GameObject draggingItem = (GameObject)Instantiate(Resources.Load("Prefabs/DraggingItem") as GameObject);

            draggingItem.transform.SetParent(GameObject.FindGameObjectWithTag("Canvas").transform, true);

            panel.AddComponent();

            inv.getPrefabs();

        }

    }

#endif

 

    void Update()

    {

        for (int i = 0; i < slotsInTotal; i++)

        {

            if (Input.GetKeyDown(keyCodesForSlots[i]))

            {

                if (transform.GetChild(1).GetChild(i).childCount != 0 && transform.GetChild(1).GetChild(i).GetChild(0).GetComponent().item.itemType != ItemType.UFPS_Ammo)

                {

                    if (transform.GetChild(1).GetChild(i).GetChild(0).GetComponent().duplication != null && transform.GetChild(1).GetChild(i).GetChild(0).GetComponent().item.maxStack == 1)

                    {

                        Destroy(transform.GetChild(1).GetChild(i).GetChild(0).GetComponent().duplication);

                    }

                    transform.GetChild(1).GetChild(i).GetChild(0).GetComponent().consumeIt();

                }

            }

        }

    }

 

    public int getSlotsInTotal()

    {

        Inventory inv = GetComponent();

        return slotsInTotal = inv.width * inv.height;

    }

}

 

 

 

你可能感兴趣的:(Unity开发)