65 lines
2.6 KiB
C#
65 lines
2.6 KiB
C#
using UnityEngine;
|
|
|
|
public class CarConditionGenerator : MonoBehaviour
|
|
{
|
|
[Header("Настройки спавна")]
|
|
[Range(0f, 1f)] [SerializeField] private float rustProbability = 0.7f;
|
|
[SerializeField] private float minRustAmount = 0.2f;
|
|
[SerializeField] private float maxRustAmount = 0.75f;
|
|
|
|
[Header("Цвет кузова")]
|
|
[Tooltip("Включите, только если хотите принудительно перекрасить машину при старте")]
|
|
[SerializeField] private bool overrideMaterialColor = false;
|
|
[SerializeField] private Color baseCarColor = Color.blue;
|
|
|
|
private void Start()
|
|
{
|
|
GenerateCondition();
|
|
}
|
|
|
|
[ContextMenu("Сгенерировать состояние")]
|
|
public void GenerateCondition()
|
|
{
|
|
// 1. Обрабатываем все съемные детали (CarPart)
|
|
CarPart[] allParts = GetComponentsInChildren<CarPart>(true);
|
|
foreach (CarPart part in allParts)
|
|
{
|
|
CarPartVisuals visuals = part.GetComponent<CarPartVisuals>();
|
|
if (visuals == null) visuals = part.gameObject.AddComponent<CarPartVisuals>();
|
|
|
|
if (overrideMaterialColor)
|
|
visuals.SetPaintColor(baseCarColor);
|
|
|
|
if (part.Data != null && !part.Data.canRust)
|
|
{
|
|
visuals.SetDamage(0f);
|
|
continue;
|
|
}
|
|
|
|
float damage = Random.value < rustProbability
|
|
? Random.Range(minRustAmount, maxRustAmount)
|
|
: 0f;
|
|
|
|
visuals.SetDamage(damage);
|
|
}
|
|
|
|
// 2. Обрабатываем основной кузов (body), на котором нет CarPart, чтобы он не оставался ржавым из материала
|
|
int rustProp = Shader.PropertyToID("_RustAmount");
|
|
foreach (Renderer r in GetComponentsInChildren<Renderer>(true))
|
|
{
|
|
if (r.GetComponentInParent<CarPart>(true) != null) continue;
|
|
|
|
foreach (Material m in r.sharedMaterials)
|
|
{
|
|
if (m != null && m.HasProperty(rustProp))
|
|
{
|
|
CarPartVisuals bodyVis = r.GetComponent<CarPartVisuals>();
|
|
if (bodyVis == null) bodyVis = r.gameObject.AddComponent<CarPartVisuals>();
|
|
if (overrideMaterialColor) bodyVis.SetPaintColor(baseCarColor);
|
|
bodyVis.SetDamage(Random.value < rustProbability ? Random.Range(minRustAmount, maxRustAmount) : 0f);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |