/
arti4
/
Diplom
Обзор
Документация
Войти
/
arti4
/
Diplom
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Scripts/Buildings/Garage/CarColorSystem.cs
234 строки
7 KB
arti4
Загрузка всех скриптов
17 май 2026, 09:08
Верифицирован
17 май 2026, 09:08
cc2e1cd
Код
Авторство
О чём код?
using UnityEngine; using System.Collections.Generic; public class CarColorSystem : MonoBehaviour { public static CarColorSystem GetInstance() { if (Instance == null) { Instance = FindFirstObjectByType<CarColorSystem>(); if (Instance == null) { GameObject obj = new GameObject("CarColorSystem"); Instance = obj.AddComponent<CarColorSystem>(); DontDestroyOnLoad(obj); } } return Instance; } public static CarColorSystem Instance { get; private set; } [Header("Настройки")] public bool saveColorPerCar = true; public Color defaultCarColor = new Color(0.2f, 0.6f, 1f); [Header("Настройки материалов")] public string[] materialKeywords = { "BodyCars" }; public bool applyToAllMaterials = false; private Dictionary<int, Color> carColors = new Dictionary<int, Color>(); void Awake() { if (Instance == null) { Instance = this; if (transform.parent != null) { transform.SetParent(null, true); } } else { Destroy(gameObject); } } void Start() { LoadAllColors(); foreach (var kvp in carColors) { } } public void SaveCarColor(int carIndex, Color color) { string colorKey = GetColorKey(carIndex); string colorHex = ColorUtility.ToHtmlStringRGB(color); PlayerPrefs.SetString(colorKey, colorHex); PlayerPrefs.Save(); if (carColors.ContainsKey(carIndex)) carColors[carIndex] = color; else carColors.Add(carIndex, color); } public Color LoadCarColor(int carIndex) { if (carColors.ContainsKey(carIndex)) return carColors[carIndex]; string colorKey = GetColorKey(carIndex); if (PlayerPrefs.HasKey(colorKey)) { string colorHex = PlayerPrefs.GetString(colorKey); if (ColorUtility.TryParseHtmlString("#" + colorHex, out Color color)) { carColors[carIndex] = color; return color; } } return defaultCarColor; } public void ApplyColorToCar(GameObject car, int carIndex) { Color color = LoadCarColor(carIndex); ApplyColorToCar(car, color); } public void ApplyColorToCar(GameObject car, Color color) { if (car == null) return; Renderer[] allRenderers = car.GetComponentsInChildren<Renderer>(); foreach (Renderer renderer in allRenderers) { Material[] materials = renderer.materials; for (int i = 0; i < materials.Length; i++) { bool shouldChangeColor = applyToAllMaterials; string materialName = materials[i].name.ToLower(); if (!shouldChangeColor) { foreach (string keyword in materialKeywords) { if (!string.IsNullOrEmpty(keyword)) { string keywordLower = keyword.ToLower(); if (materialName.Contains(keywordLower)) { shouldChangeColor = true; break; } } } } if (shouldChangeColor) { if (materials[i].HasProperty("_Color")) { Color oldColor = materials[i].color; materials[i].color = new Color(color.r, color.g, color.b, oldColor.a); } else if (materials[i].HasProperty("_BaseColor")) { Color oldColor = materials[i].GetColor("_BaseColor"); materials[i].SetColor("_BaseColor", new Color(color.r, color.g, color.b, oldColor.a)); } else if (materials[i].HasProperty("_MainColor")) { Color oldColor = materials[i].GetColor("_MainColor"); materials[i].SetColor("_MainColor", new Color(color.r, color.g, color.b, oldColor.a)); } else { Shader shader = materials[i].shader; if (shader != null) { int propertyCount = shader.GetPropertyCount(); for (int p = 0; p < propertyCount; p++) { string propName = shader.GetPropertyName(p); } } } } else { } } renderer.materials = materials; } } public void ResetCarColor(int carIndex) { SaveCarColor(carIndex, defaultCarColor); } public void ResetAllColors() { carColors.Clear(); for (int i = 0; i < 20; i++) { string key = GetColorKey(i); if (PlayerPrefs.HasKey(key)) { PlayerPrefs.DeleteKey(key); } } PlayerPrefs.Save(); } void LoadAllColors() { carColors.Clear(); for (int i = 0; i < 20; i++) { string key = GetColorKey(i); if (PlayerPrefs.HasKey(key)) { string colorHex = PlayerPrefs.GetString(key); if (ColorUtility.TryParseHtmlString("#" + colorHex, out Color color)) { carColors[i] = color; } } } } string GetColorKey(int carIndex) { if (saveColorPerCar) { return $"CarColor_{carIndex}"; } else { return "CarColor_Global"; } } [ContextMenu("Показать все сохраненные цвета")] void DebugAllColors() { for (int i = 0; i < 10; i++) { string key = GetColorKey(i); if (PlayerPrefs.HasKey(key)) { string hex = PlayerPrefs.GetString(key); } } } }