Сырая но рабочая система расстановки мебели
This commit is contained in:
1 parent
4ccd30d788
commit
6984717957
61 files changed
+6742
-149
No files matched your search
@@ -1,42 +1,59 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class FurnitureObject : MonoBehaviour
|
||||
{
|
||||
[Header("Настройки")]
|
||||
public LayerMask obstacleLayers; // Слои объектов, с которыми нельзя пересекаться
|
||||
public LayerMask obstacleLayers;
|
||||
|
||||
private List<Material> _materials = new List<Material>();
|
||||
private List<Color> _originalColors = new List<Color>();
|
||||
private int _obstaclesCount = 0;
|
||||
|
||||
public bool IsPlacementValid => _obstaclesCount == 0;
|
||||
|
||||
// Переменные для нашей "коробки"
|
||||
private GameObject _highlightBox;
|
||||
private Material _highlightMaterial;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
Renderer[] renderers = GetComponentsInChildren<Renderer>();
|
||||
foreach (var r in renderers)
|
||||
CreateHighlightBox();
|
||||
}
|
||||
|
||||
private void CreateHighlightBox()
|
||||
{
|
||||
BoxCollider col = GetComponent<BoxCollider>();
|
||||
if (col == null)
|
||||
{
|
||||
foreach (var mat in r.materials)
|
||||
{
|
||||
_materials.Add(mat);
|
||||
_originalColors.Add(mat.color);
|
||||
}
|
||||
Debug.LogError("На префабе мебели нет BoxCollider! Коробка не появится.");
|
||||
return;
|
||||
}
|
||||
|
||||
// 1. Создаем обычный куб
|
||||
_highlightBox = GameObject.CreatePrimitive(PrimitiveType.Cube);
|
||||
|
||||
// 2. Удаляем с него коллайдер, чтобы он не мешал физике
|
||||
Destroy(_highlightBox.GetComponent<Collider>());
|
||||
|
||||
// 3. Делаем куб дочерним к мебели и задаем размеры коллайдера
|
||||
_highlightBox.transform.SetParent(transform);
|
||||
_highlightBox.transform.localPosition = col.center;
|
||||
_highlightBox.transform.localRotation = Quaternion.identity;
|
||||
_highlightBox.transform.localScale = col.size;
|
||||
|
||||
// 4. Магия: Создаем прозрачный материал через код.
|
||||
// Шейдер "Sprites/Default" есть в Unity всегда, он светится в темноте и поддерживает прозрачность.
|
||||
_highlightMaterial = new Material(Shader.Find("Sprites/Default"));
|
||||
_highlightBox.GetComponent<Renderer>().material = _highlightMaterial;
|
||||
|
||||
// Выключаем куб при старте
|
||||
_highlightBox.SetActive(false);
|
||||
}
|
||||
|
||||
public void SetEditMode(bool active)
|
||||
{
|
||||
if (!active)
|
||||
if (_highlightBox != null)
|
||||
{
|
||||
for (int i = 0; i < _materials.Count; i++)
|
||||
{
|
||||
_materials[i].color = _originalColors[i];
|
||||
}
|
||||
return;
|
||||
_highlightBox.SetActive(active);
|
||||
if (active) UpdateColor();
|
||||
}
|
||||
|
||||
UpdateColor();
|
||||
}
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
@@ -59,11 +76,11 @@ public class FurnitureObject : MonoBehaviour
|
||||
|
||||
private void UpdateColor()
|
||||
{
|
||||
Color targetColor = IsPlacementValid ? new Color(0, 1, 0, 0.6f) : new Color(1, 0, 0, 0.6f);
|
||||
|
||||
foreach (var mat in _materials)
|
||||
if (_highlightMaterial != null)
|
||||
{
|
||||
mat.color = targetColor;
|
||||
// Зеленый или красный цвет (0.4f - это степень прозрачности)
|
||||
Color targetColor = IsPlacementValid ? new Color(0, 1, 0, 0.4f) : new Color(1, 0, 0, 0.4f);
|
||||
_highlightMaterial.color = targetColor;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user