khác

Hàng tồn kho

Hàng tồn kho
Đã kiểm tra với phiên bản: 5.5
– –
Độ khó: Trung cấp
Giai đoạn 2 dạy bạn cách xây dựng một khoảng không quảng cáo bao gồm xây dựng một giao diện người dùng và hệ thống quản lý mục cho khoảng không quảng cáo người chơi bằng cách sử dụng Hệ thống giao diện người dùng của Unity và một số kịch bản trình soạn thảo. Tải xuống gói nội dung cho Giai đoạn 2 tại đây.https://www.assetstore.unity3d.com/en/?&_ga=2.86810904.1082230255.1536856804-659907597.1536596969#!/content/76784


Tải xuống gói nội dung cho Giai đoạn 2 tại đây.https://www.assetstore.unity3d.com/en/?&_ga=2.155165112.1082230255.1536856804-659907597.1536596969#!/content/76784

Inventory

Expand view

Copy code
using UnityEngine;
using UnityEngine.UI;
public class Inventory : MonoBehaviour
{
public Image[] itemImages = new Image[numItemSlots];
public Item[] items = new Item[numItemSlots];
public const int numItemSlots = 4;
public void AddItem(Item itemToAdd)
{
for (int i = 0; i < items.Length; i++)
{
if (items[i] == null)
{
items[i] = itemToAdd;
itemImages[i].sprite = itemToAdd.sprite;
itemImages[i].enabled = true;
return;
}
}
}
public void RemoveItem (Item itemToRemove)
{
for (int i = 0; i < items.Length; i++)
{
if (items[i] == itemToRemove)
{
items[i] = null;
itemImages[i].sprite = null;
itemImages[i].enabled = false;
return;
}
}
}
}


1


2


3


4


5


6


7


8


9


10


11


12


13


14


15


16


17


18


19


20


21


22


23


24


25


26


27


28


29


30


31


32


33


34


InventoryEditor

Expand view

Copy code
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(Inventory))]public class InventoryEditor : Editor
{
private bool[] showItemSlots = new bool[Inventory.numItemSlots];
private SerializedProperty itemImagesProperty;
private SerializedProperty itemsProperty;
private const string inventoryPropItemImagesName = "itemImages";
private const string inventoryPropItemsName = "items";
private void OnEnable ()
{
itemImagesProperty = serializedObject.FindProperty (inventoryPropItemImagesName);
itemsProperty = serializedObject.FindProperty (inventoryPropItemsName);
}
public override void OnInspectorGUI ()
{
serializedObject.Update ();
for (int i = 0; i < Inventory.numItemSlots; i++)
{
ItemSlotGUI (i);
}
serializedObject.ApplyModifiedProperties ();
}
private void ItemSlotGUI (int index)
{
EditorGUILayout.BeginVertical (GUI.skin.box);
EditorGUI.indentLevel++;

showItemSlots[index] = EditorGUILayout.Foldout (showItemSlots[index], "Item slot " + index);
if (showItemSlots[index])
{
EditorGUILayout.PropertyField (itemImagesProperty.GetArrayElementAtIndex (index));
EditorGUILayout.PropertyField (itemsProperty.GetArrayElementAtIndex (index));
}
EditorGUI.indentLevel--;
EditorGUILayout.EndVertical ();
}
}


1


2


3


4


5


6


7


8


9


10


11


12


13


14


15


16


17


18


19


20


21


22


23


24


25


26


27


28


29


30


31


32


33


34


35


36


37


38


39


About the author

lbtmicr06

Leave a Comment

Translate »