added customization
This commit is contained in:
1 parent
1d7d176814
commit
51b5659f4a
116 files changed
+16093
-5
No files matched your search
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8e3eb242cd00460696432c79c4a1defd
|
||||
timeCreated: 1772956842
|
||||
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Saves
|
||||
{
|
||||
[Serializable]
|
||||
public class GameData
|
||||
{
|
||||
public int bodyMaterialIndex;
|
||||
public int faceMaterialIndex;
|
||||
public int hatIndex;
|
||||
|
||||
public Color platformColor = Color.white;
|
||||
public Color bonusColor = Color.white;
|
||||
|
||||
public string playerName = "Player";
|
||||
public int highScore;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f0ce86011fb84e229a6b0519f0cf3a6f
|
||||
timeCreated: 1772956848
|
||||
@@ -0,0 +1,23 @@
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Saves
|
||||
{
|
||||
public static class SaveManager
|
||||
{
|
||||
private static string Path => Application.persistentDataPath + "/save.json";
|
||||
|
||||
public static void Save(GameData data)
|
||||
{
|
||||
var json = JsonUtility.ToJson(data, true);
|
||||
File.WriteAllText(Path, json);
|
||||
}
|
||||
|
||||
public static GameData Load()
|
||||
{
|
||||
if (!File.Exists(Path)) return new GameData();
|
||||
var json = File.ReadAllText(Path);
|
||||
return JsonUtility.FromJson<GameData>(json);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d20ff63563944482a848fae3a09386de
|
||||
timeCreated: 1772956882
|
||||
@@ -0,0 +1,118 @@
|
||||
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(); }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 04986c5baedf4597a0ebf10e4b2bd85d
|
||||
timeCreated: 1772957483
|
||||
@@ -28,7 +28,7 @@ namespace Code.UI
|
||||
private void Start()
|
||||
{
|
||||
playButton.onClick.AddListener(() => LoadScene("Gameplay"));
|
||||
customizeButton.onClick.AddListener(() => Debug.Log("Customization"));
|
||||
customizeButton.onClick.AddListener(() => LoadScene("Customization"));
|
||||
leaderboardButton.onClick.AddListener(() => Debug.Log("Leaderboard"));
|
||||
exitButton.onClick.AddListener(Application.Quit);
|
||||
|
||||
|
||||
Reference in new issue
Block a user