118 lines
4.6 KiB
C#
118 lines
4.6 KiB
C#
using Code.Saves;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Code.Customization
|
|
{
|
|
public class CustomizationController : MonoBehaviour
|
|
{
|
|
[SerializeField] private SkinnedMeshRenderer characterRenderer;
|
|
[SerializeField] private Transform hatContainer;
|
|
|
|
[SerializeField] private Renderer platformRenderer;
|
|
[SerializeField] private Renderer bonusRenderer;
|
|
|
|
[SerializeField] private Slider platformR, platformG, platformB;
|
|
[SerializeField] private Image platformPreview;
|
|
|
|
[SerializeField] private Slider bonusR, bonusG, bonusB;
|
|
[SerializeField] private Image bonusPreview;
|
|
|
|
private Material[] bodyMaterials;
|
|
private Material[] faceMaterials;
|
|
private GameObject[] hatPrefabs;
|
|
private GameData _currentData;
|
|
|
|
private static readonly int ShaderColorId = Shader.PropertyToID("_BaseColor");
|
|
|
|
private void Start()
|
|
{
|
|
_currentData = SaveManager.Load();
|
|
if (_currentData == null) _currentData = new GameData();
|
|
|
|
LoadAssets();
|
|
UpdateSlidersUI();
|
|
ApplyAllVisuals();
|
|
}
|
|
|
|
private void LoadAssets()
|
|
{
|
|
bodyMaterials = Resources.LoadAll<Material>("MaterialsCharacter/Body");
|
|
faceMaterials = Resources.LoadAll<Material>("MaterialsCharacter/Face");
|
|
hatPrefabs = Resources.LoadAll<GameObject>("Prefabs/Character/Hats");
|
|
}
|
|
|
|
public void OnPlatformSliderChanged()
|
|
{
|
|
if (_currentData == null || platformR == null) return;
|
|
|
|
_currentData.platformColor = new Color(platformR.value, platformG.value, platformB.value);
|
|
if (platformPreview != null) platformPreview.color = _currentData.platformColor;
|
|
|
|
if (platformRenderer != null)
|
|
platformRenderer.material.SetColor(ShaderColorId, _currentData.platformColor);
|
|
}
|
|
|
|
public void OnBonusSliderChanged()
|
|
{
|
|
if (_currentData == null || bonusR == null) return;
|
|
|
|
_currentData.bonusColor = new Color(bonusR.value, bonusG.value, bonusB.value);
|
|
if (bonusPreview != null) bonusPreview.color = _currentData.bonusColor;
|
|
|
|
if (bonusRenderer != null)
|
|
bonusRenderer.material.SetColor(ShaderColorId, _currentData.bonusColor);
|
|
}
|
|
|
|
private void ApplyAllVisuals()
|
|
{
|
|
ApplyCharacter();
|
|
OnPlatformSliderChanged();
|
|
OnBonusSliderChanged();
|
|
}
|
|
|
|
private void ApplyCharacter()
|
|
{
|
|
if (characterRenderer == null) return;
|
|
|
|
var mats = characterRenderer.sharedMaterials;
|
|
if (bodyMaterials.Length > 0) mats[0] = bodyMaterials[Mathf.Clamp(_currentData.bodyMaterialIndex, 0, bodyMaterials.Length - 1)];
|
|
if (faceMaterials.Length > 0) mats[1] = faceMaterials[Mathf.Clamp(_currentData.faceMaterialIndex, 0, faceMaterials.Length - 1)];
|
|
characterRenderer.sharedMaterials = mats;
|
|
|
|
foreach (Transform child in hatContainer) Destroy(child.gameObject);
|
|
if (_currentData.hatIndex > 0 && hatPrefabs.Length >= _currentData.hatIndex)
|
|
{
|
|
Instantiate(hatPrefabs[_currentData.hatIndex - 1], hatContainer);
|
|
}
|
|
}
|
|
|
|
private void UpdateSlidersUI()
|
|
{
|
|
if (_currentData == null) return;
|
|
platformR.value = _currentData.platformColor.r;
|
|
platformG.value = _currentData.platformColor.g;
|
|
platformB.value = _currentData.platformColor.b;
|
|
bonusR.value = _currentData.bonusColor.r;
|
|
bonusG.value = _currentData.bonusColor.g;
|
|
bonusB.value = _currentData.bonusColor.b;
|
|
}
|
|
|
|
public void SaveAndExit()
|
|
{
|
|
SaveManager.Save(_currentData);
|
|
UnityEngine.SceneManagement.SceneManager.LoadScene("MainMenu");
|
|
}
|
|
|
|
private static int GetNextIndex(int current, int dir, int length)
|
|
{
|
|
if (length == 0) return 0;
|
|
var next = (current + dir) % length;
|
|
return next < 0 ? length - 1 : next;
|
|
}
|
|
|
|
public void ChangeBody(int dir) { _currentData.bodyMaterialIndex = GetNextIndex(_currentData.bodyMaterialIndex, dir, bodyMaterials.Length); ApplyCharacter(); }
|
|
public void ChangeFace(int dir) { _currentData.faceMaterialIndex = GetNextIndex(_currentData.faceMaterialIndex, dir, faceMaterials.Length); ApplyCharacter(); }
|
|
public void ChangeHat(int dir) { _currentData.hatIndex = GetNextIndex(_currentData.hatIndex, dir, hatPrefabs.Length + 1); ApplyCharacter(); }
|
|
}
|
|
} |