авто добавления кропаля на коллайдерах. Авто поиск всех элементов мебели. Полный газик
This commit is contained in:
1 parent
772fd3d7f6
commit
eff9613938
34 files changed
+2036
-98
No files matched your search
@@ -1,12 +1,14 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.AddressableAssets;
|
||||
using UnityEngine.ResourceManagement.AsyncOperations;
|
||||
|
||||
public class FurnitureController : MonoBehaviour
|
||||
{
|
||||
public enum EditTool { Move, Rotate }
|
||||
|
||||
[Header("Настройки")]
|
||||
public GameObject currentPrefab;
|
||||
public FurnitureItemSO currentItemData; // Заменили GameObject на SO
|
||||
public LayerMask floorLayer;
|
||||
public LayerMask wallLayer;
|
||||
public LayerMask furnitureLayer;
|
||||
@@ -19,7 +21,7 @@ public class FurnitureController : MonoBehaviour
|
||||
public float rotationSpeed = 5f;
|
||||
|
||||
[Header("Настройки двойного клика")]
|
||||
public float doubleClickThreshold = 0.3f; // Максимальное время между кликами в секундах
|
||||
public float doubleClickThreshold = 0.3f;
|
||||
|
||||
[Header("Связь с камерой")]
|
||||
public MonoBehaviour cameraController;
|
||||
@@ -33,7 +35,7 @@ public class FurnitureController : MonoBehaviour
|
||||
private Vector3 _lastInputPos;
|
||||
private Vector3 _dragOffset;
|
||||
|
||||
private float _lastClickTime = 0f; // Хранит время последнего клика
|
||||
private float _lastClickTime = 0f;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
@@ -41,7 +43,7 @@ public class FurnitureController : MonoBehaviour
|
||||
if (cameraController == null && _cam != null)
|
||||
cameraController = _cam.GetComponent("CameraController") as MonoBehaviour;
|
||||
|
||||
currentPrefab = null;
|
||||
currentItemData = null;
|
||||
editPanel.SetActive(false);
|
||||
if (rotate90Button != null) rotate90Button.SetActive(false);
|
||||
}
|
||||
@@ -52,26 +54,23 @@ public class FurnitureController : MonoBehaviour
|
||||
{
|
||||
HandleEditing();
|
||||
}
|
||||
else if (currentPrefab != null)
|
||||
else if (currentItemData != null)
|
||||
{
|
||||
// Оставляем одинарный клик для установки НОВОЙ мебели
|
||||
if (Input.GetMouseButtonDown(0) && !shopPanel.activeSelf && !IsPointerOverUI())
|
||||
TryPlaceFurniture();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Логика двойного клика для выбора УЖЕ УСТАНОВЛЕННОЙ мебели
|
||||
if (Input.GetMouseButtonDown(0) && !shopPanel.activeSelf && !IsPointerOverUI())
|
||||
{
|
||||
float timeSinceLastClick = Time.time - _lastClickTime;
|
||||
|
||||
if (timeSinceLastClick <= doubleClickThreshold)
|
||||
{
|
||||
// Если время между кликами меньше порога — это двойной клик
|
||||
TrySelectPlacedFurniture();
|
||||
}
|
||||
|
||||
_lastClickTime = Time.time; // Запоминаем время текущего клика
|
||||
_lastClickTime = Time.time;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -99,7 +98,6 @@ public class FurnitureController : MonoBehaviour
|
||||
|
||||
if (foundFurniture)
|
||||
{
|
||||
// Для пола считаем смещение, настенные предметы просто цепляем к курсору
|
||||
if (!_activeFurnitureScript.isWallObject && Physics.Raycast(ray, out RaycastHit floorHit, 100f, floorLayer))
|
||||
{
|
||||
_dragOffset = _activeFurniture.transform.position - floorHit.point;
|
||||
@@ -122,17 +120,14 @@ public class FurnitureController : MonoBehaviour
|
||||
|
||||
if (_currentTool == EditTool.Move)
|
||||
{
|
||||
// === ЛОГИКА ДЛЯ НАСТЕННЫХ ПРЕДМЕТОВ ===
|
||||
if (_activeFurnitureScript.isWallObject)
|
||||
{
|
||||
if (Physics.Raycast(ray, out RaycastHit hit, 100f, wallLayer))
|
||||
{
|
||||
_activeFurniture.transform.position = hit.point;
|
||||
// Магия: Поворачиваем объект "лицом" от стены
|
||||
_activeFurniture.transform.rotation = Quaternion.LookRotation(hit.normal);
|
||||
}
|
||||
}
|
||||
// === ЛОГИКА ДЛЯ НАПОЛЬНЫХ ПРЕДМЕТОВ (Старая) ===
|
||||
else
|
||||
{
|
||||
if (Physics.Raycast(ray, out RaycastHit hit, 100f, floorLayer))
|
||||
@@ -180,7 +175,6 @@ public class FurnitureController : MonoBehaviour
|
||||
{
|
||||
float deltaX = Input.mousePosition.x - _lastInputPos.x;
|
||||
|
||||
// Настенные предметы крутим вокруг своей оси Z (по циферблату), а напольные - вокруг мировой Y
|
||||
if (_activeFurnitureScript.isWallObject)
|
||||
_activeFurniture.transform.Rotate(Vector3.forward, -deltaX * rotationSpeed * 0.2f, Space.Self);
|
||||
else
|
||||
@@ -258,30 +252,51 @@ public class FurnitureController : MonoBehaviour
|
||||
{
|
||||
Ray ray = _cam.ScreenPointToRay(Input.mousePosition);
|
||||
|
||||
// Определяем, куда пускать луч при установке в зависимости от типа предмета
|
||||
FurnitureObject prefabScript = currentPrefab.GetComponent<FurnitureObject>();
|
||||
LayerMask targetLayer = prefabScript.isWallObject ? wallLayer : floorLayer;
|
||||
// Берем слой для первичной установки из карточки товара SO!
|
||||
LayerMask targetLayer = currentItemData.isWallObject ? wallLayer : floorLayer;
|
||||
|
||||
if (Physics.Raycast(ray, out RaycastHit hit, 100f, targetLayer))
|
||||
{
|
||||
_activeFurniture = Instantiate(currentPrefab, hit.point, Quaternion.identity);
|
||||
|
||||
// Если это стена - сразу поворачиваем спиной к ней
|
||||
if (prefabScript.isWallObject)
|
||||
// Блокируем новые клики, пока грузится ассет
|
||||
_isInteracting = true;
|
||||
|
||||
Quaternion spawnRot = Quaternion.identity;
|
||||
if (currentItemData.isWallObject)
|
||||
spawnRot = Quaternion.LookRotation(hit.normal);
|
||||
|
||||
// Запускаем асинхронную загрузку из Addressables
|
||||
currentItemData.prefabRef.InstantiateAsync(hit.point, spawnRot).Completed += handle =>
|
||||
{
|
||||
_activeFurniture.transform.rotation = Quaternion.LookRotation(hit.normal);
|
||||
}
|
||||
if (handle.Status == AsyncOperationStatus.Succeeded)
|
||||
{
|
||||
_activeFurniture = handle.Result;
|
||||
_activeFurnitureScript = _activeFurniture.GetComponent<FurnitureObject>();
|
||||
|
||||
// --- МАГИЯ АРХИТЕКТУРЫ ---
|
||||
// Находим скрипт визуала и применяем базовый цвет (Preset 0)
|
||||
FurnitureVisuals visuals = _activeFurniture.GetComponent<FurnitureVisuals>();
|
||||
if (visuals != null && currentItemData.availablePresets != null && currentItemData.availablePresets.Count > 0)
|
||||
{
|
||||
visuals.ApplyPreset(currentItemData.availablePresets[0]);
|
||||
}
|
||||
// -------------------------
|
||||
|
||||
_activeFurnitureScript = _activeFurniture.GetComponent<FurnitureObject>();
|
||||
_activeFurnitureScript.SetEditMode(true);
|
||||
|
||||
_currentTool = EditTool.Move;
|
||||
if (rotate90Button != null) rotate90Button.SetActive(false);
|
||||
_activeFurnitureScript.SetEditMode(true);
|
||||
|
||||
_currentTool = EditTool.Move;
|
||||
if (rotate90Button != null) rotate90Button.SetActive(false);
|
||||
|
||||
currentPrefab = null;
|
||||
_isInteracting = false;
|
||||
LockCamera(false);
|
||||
UpdateUIPosition();
|
||||
currentItemData = null; // Сбрасываем карточку из буфера
|
||||
_isInteracting = false;
|
||||
LockCamera(false);
|
||||
UpdateUIPosition();
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError($"Не удалось загрузить {currentItemData.itemName}. Проверьте галочку Addressables на префабе!");
|
||||
_isInteracting = false;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -291,7 +306,6 @@ public class FurnitureController : MonoBehaviour
|
||||
{
|
||||
if (_activeFurnitureScript.isWallObject)
|
||||
{
|
||||
// Настенные объекты поворачиваем по их локальной оси Z (как часы)
|
||||
_activeFurniture.transform.Rotate(0, 0, 90f, Space.Self);
|
||||
}
|
||||
else
|
||||
@@ -322,7 +336,9 @@ public class FurnitureController : MonoBehaviour
|
||||
{
|
||||
if (_activeFurniture != null)
|
||||
{
|
||||
Destroy(_activeFurniture);
|
||||
// ПРАВИЛЬНОЕ удаление объекта, заспавненного через Addressables
|
||||
Addressables.ReleaseInstance(_activeFurniture);
|
||||
|
||||
_activeFurniture = null;
|
||||
_activeFurnitureScript = null;
|
||||
editPanel.SetActive(false);
|
||||
@@ -330,9 +346,10 @@ public class FurnitureController : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
public void SelectFurnitureAndCloseShop(GameObject prefabToSelect)
|
||||
// Эта функция вызывается из ShopManager
|
||||
public void SelectFurnitureAndCloseShop(FurnitureItemSO itemToSelect)
|
||||
{
|
||||
currentPrefab = prefabToSelect;
|
||||
currentItemData = itemToSelect;
|
||||
shopPanel.SetActive(false);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user