Базовый скрипт для создания предметов
This commit is contained in:
1 parent
4adfb81d1d
commit
b25c561a97
367 files changed
+79704
-28
No files matched your search
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public class FurnitureBuilder : MonoBehaviour
|
||||
{
|
||||
[Header("Настройки")]
|
||||
public GameObject currentPrefab;
|
||||
public LayerMask floorLayer;
|
||||
[SerializeField] private GameObject shopPanel;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
currentPrefab = null;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (Input.GetMouseButtonDown(0)&&!shopPanel.activeSelf)
|
||||
{
|
||||
if (currentPrefab != null)
|
||||
{
|
||||
TryPlaceFurniture();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TryPlaceFurniture()
|
||||
{
|
||||
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
||||
RaycastHit hit;
|
||||
|
||||
if (Physics.Raycast(ray, out hit, 100f, floorLayer))
|
||||
{
|
||||
Instantiate(currentPrefab, hit.point, Quaternion.identity);
|
||||
currentPrefab = null;
|
||||
}
|
||||
}
|
||||
public void SelectFurnitureAndCloseShop(GameObject prefabToSelect)
|
||||
{
|
||||
currentPrefab = prefabToSelect;
|
||||
shopPanel.SetActive(false);
|
||||
}
|
||||
public void SelectFurniture(GameObject prefabToSelect)
|
||||
{
|
||||
currentPrefab = prefabToSelect;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 37c5b010452efc242ae2c7e3114721a7
|
||||
@@ -0,0 +1,52 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI; // Обязательно добавляем для работы с интерфейсом
|
||||
|
||||
// Этот класс - наша "карточка товара". Он хранит иконку и 3D-модель.
|
||||
[System.Serializable] // Позволяет отображать этот класс в инспекторе Unity
|
||||
public class FurnitureItem
|
||||
{
|
||||
public string itemName; // Название (просто для вашего удобства в инспекторе)
|
||||
public Sprite icon; // Иконка для магазина
|
||||
public GameObject prefab; // 3D-модель для установки
|
||||
}
|
||||
|
||||
public class ShopManager : MonoBehaviour
|
||||
{
|
||||
[Header("Связи")]
|
||||
public FurnitureBuilder builder; // Ссылка на ваш скрипт строительства
|
||||
public GameObject buttonTemplate; // Наш префаб кнопки магазина
|
||||
public Transform contentPanel; // Тот самый объект Content с Grid Layout Group
|
||||
|
||||
[Header("Каталог мебели")]
|
||||
public List<FurnitureItem> items; // Список всей мебели в игре
|
||||
|
||||
void Start()
|
||||
{
|
||||
// Как только игра запускается, генерируем магазин
|
||||
GenerateShop();
|
||||
}
|
||||
|
||||
void GenerateShop()
|
||||
{
|
||||
// Проходимся по каждому предмету в нашем каталоге
|
||||
foreach (FurnitureItem item in items)
|
||||
{
|
||||
// 1. Создаем копию шаблона кнопки внутри панели Content
|
||||
GameObject newButton = Instantiate(buttonTemplate, contentPanel);
|
||||
|
||||
// 2. Находим компонент Image на этой кнопке и меняем картинку на нашу иконку
|
||||
newButton.GetComponent<Image>().sprite = item.icon;
|
||||
|
||||
// 3. Находим компонент Button, чтобы настроить клик
|
||||
Button btn = newButton.GetComponent<Button>();
|
||||
|
||||
// Сохраняем префаб в локальную переменную (это важно для правильной работы кода)
|
||||
GameObject prefabToSelect = item.prefab;
|
||||
|
||||
// 4. Программно добавляем действие при клике (то же самое, что плюсик в инспекторе)
|
||||
// Мы вызываем метод из вашего прошлого скрипта!
|
||||
btn.onClick.AddListener(() => builder.SelectFurnitureAndCloseShop(prefabToSelect));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 07c4fd35416ae7d4aa742d551654809f
|
||||
Reference in new issue
Block a user