Делаю режим строительства
This commit is contained in:
1 parent
09be8e6737
commit
4ccd30d788
11 files changed
+1422
-163
No files matched your search
@@ -0,0 +1,69 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class FurnitureObject : MonoBehaviour
|
||||
{
|
||||
[Header("Настройки")]
|
||||
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;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
Renderer[] renderers = GetComponentsInChildren<Renderer>();
|
||||
foreach (var r in renderers)
|
||||
{
|
||||
foreach (var mat in r.materials)
|
||||
{
|
||||
_materials.Add(mat);
|
||||
_originalColors.Add(mat.color);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void SetEditMode(bool active)
|
||||
{
|
||||
if (!active)
|
||||
{
|
||||
for (int i = 0; i < _materials.Count; i++)
|
||||
{
|
||||
_materials[i].color = _originalColors[i];
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
UpdateColor();
|
||||
}
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
if (((1 << other.gameObject.layer) & obstacleLayers) != 0)
|
||||
{
|
||||
_obstaclesCount++;
|
||||
UpdateColor();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerExit(Collider other)
|
||||
{
|
||||
if (((1 << other.gameObject.layer) & obstacleLayers) != 0)
|
||||
{
|
||||
_obstaclesCount--;
|
||||
UpdateColor();
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
mat.color = targetColor;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user