[UNITY] ПОДБОР СТАКАЮЩИХ ПРЕДМЕТОВ | КАК СОЗДАТЬ ИНВЕНТАРЬ #2

preview_player
Показать описание
В этом уроке мы с вами разберемся с подбором стакающих предметов
Рекомендации по теме
Комментарии
Автор

Я считаю, что можно было сделать всё гораздо проще.
Можно в отдельном скрипте создать класс:

class Item()
{
public string name;
public int id;
public Object obj = GetComponent<Object>;
public int limitCount;
public int count;
while(limitCount!=null && count>limitCount)
{
count==limitCount;
}
}

reg
Автор

Все работает, автор - красавчик, с меня Лайк! Вот скрипт inventory:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Inventory : MonoBehaviour
{

List<Item> item;
public int Range;
public GameObject cellContainer;
public KeyCode showInventory;

// Use this for initialization
void Start()
{
item = new List<Item>();



for (int i = 0; i < i++)
{
item.Add(new Item());
}
}

// Update is called once per frame
void Update()
{

ToggleInventory();
if
{
Vector3 DirectionRay =
RaycastHit hit;
if (Physics.Raycast(transform.position, DirectionRay, out hit, Range))
{
if
{

}
}
}
}

void AddItem (Item currentItem)
{
if (currentItem.isStackable)
{

}
else
{

}
}

void AddUnstackableItem(Item currentItem)
{
for (int i = 0; i < item.Count; i++)
{
if (item[i].id == 0)
{
item[i] = currentItem;
item[i].countItem = 1;
DisplayItems();

break;
}
}
}

void AddStackableItem(Item currentItem)
{
for (int i = 0; i < item.Count; i++)
{
if (item[i].id==currentItem.id)
{
item[i].countItem++;
DisplayItems();

return;
}
}

}

void ToggleInventory()
{

if
{
if (cellContainer.activeSelf)
{

}
else
{


}
}
}

void DisplayItems()
{
for (int i = 0; i < item.Count; i++)
{
Transform cell =
Transform icon = cell.GetChild(0);
Transform count = icon.GetChild(0);

Text txt = count.GetComponent<Text>();
Image img = icon.GetComponent<Image>();
if (item[i].id != 0)
{

img.enabled = true;
img.sprite =
if (item[i].countItem>1)
{
txt.text =
}
}
else
{
img.enabled = false;
img.sprite = null;
txt.text = null;
}
}
}
}

vensentlion
Автор

Спасибо, предельно понятно и осознанно)

ЕлизаветаРоманова-пя
Автор

как же меня угорает эта музыка на заднем фоне

oynamalikanal
Автор

ошибка (59, 17) error cs1525 Unexpected symbol '<'
помогите срочно


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Inventory : MonoBehaviour
{
List<Item> item;
public GameObject cellContainer;
public KeyCode showInventory;
public KeyCode takeButton;

// Use this for initialization
void Start ()
{
item = new List<Item> ();

cellContainer.SetActive (false);

for (int i = 0; i < i++)
{
item.Add(new Item ());
}
}

void Update ()
{
ToggleInventory ();
if
{
Ray ray = Camera.main.ScreenPointToRay (new Vector2 (Screen.width / 2, Screen.height / 2));
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 2f))
{
if ())
{
AddItem ());
}
}
}
}


void AddItem (Item currrentItem)
{
if (currrentItem.isStackable)
{
AddstackableItem (currrentItem);
}
else
{
AddUnstackableItem (currrentItem);
}
}


void AddstackableItem(Item currrentItem)
{
for (int i =0; < item.Count; i++)
{
if
{
item[i].countItem++;
DisplayItems();

return;
}
}
}

void AddUnstackableItem(Item currrentItem)
{
for (int i = 0; i < item.Count; i++)
{
if (item[i].id==0)
{
item [i] = currrentItem;
item [i].countItem = 1;
DisplayItem();
Destroy (currrentItem.gameObject);
break;
}
}

}

void ToggleInventory()
{

{
if (cellContainer.activeSelf) {
cellContainer.SetActive (false);
}
else
{
cellContainer.SetActive (true);
}
}
}

void DisplayItem()
{
for (int i = 0; i < item.Count; i++)
{
Transform cell = (i);
Transform icon = cell.GetChild (0);
Transform count = icon.GetChild (0);

Text txt = count.GetComponent<Text> ();
Image img = icon.GetComponent<Image> ();

if (item [i].id != 0)
{

img.enabled = true;
img.sprite = Resources.Load<Sprite> (item [i].pathIcon);
txt.text = item [i].countItem.ToString();
}
else
{
img.enabled = false;
img.sprite = null;
}
}
}
}

tutnstn