using UnityEngine; using UnityEngine.UI; using System.Collections.Generic; using TMPro;…
- User
using UnityEngine; using UnityEngine.UI; using System.Collections.Generic; using TMPro;
public class CharacterManager : MonoBehaviour { [Header("Тестовый режим")] public bool TEST = false;
[System.Serializable] public class CharacterSkin { public string skinName; public Sprite pressedSprite; public Sprite releasedSprite; public bool isUnlocked = false; public int requiredMeters; public int requiredCoins; public int requiredStars; public int price; [HideInInspector] public bool isHiddenFromShop = false; } public List<CharacterSkin> characterSkins; private SpriteRenderer spriteRenderer; private int currentSkinIndex = 0; private bool isGameStarted = false; private bool isAttached = false; public GameManager gameManager; // public GameObject shopCanvas; // public Transform shopContent; // public GameObject shopItemPrefab; public Button resetSkinsButton; void Start() { spriteRenderer = GetComponent<SpriteRenderer>(); if (gameManager == null) { gameManager = GameManager.Instance; } if (PlayerPrefs.HasKey("SelectedSkinIndex")) { currentSkinIndex = PlayerPrefs.GetInt("SelectedSkinIndex"); if (currentSkinIndex < 0 || currentSkinIndex >= characterSkins.Count) { currentSkinIndex = 0; } } else { currentSkinIndex = 0; } LoadSkinUnlockStatus(); if (characterSkins.Count > 0) { isAttached = true; UpdateCharacterSprite(); } // InitializeShop(); if (resetSkinsButton != null) { resetSkinsButton.onClick.AddListener(ResetSkinUnlocks); } } void Update() { if (!isGameStarted) { return; } if (gameManager.collectManager != null) gameManager.totalCoins = gameManager.collectManager.coinCount; if (gameManager.snitchText != null) { int stars; if (int.TryParse(gameManager.snitchText.text.Replace("x ", ""), out stars)) { gameManager.totalStars = stars; } } if (Input.GetMouseButtonDown(0)) { isAttached = true; UpdateCharacterSprite(); } else if (Input.GetMouseButtonUp(0)) { isAttached = false; UpdateCharacterSprite(); } UpdateUI(); } void UpdateUI() { // Можно добавить логику обновления UI, если требуется } public void StartGame() { isGameStarted = true; } public void SwitchSkin() { if (isGameStarted) return; if (characterSkins.Count == 0) return; int startingIndex = currentSkinIndex; int nextSkinIndex = (currentSkinIndex + 1) % characterSkins.Count; bool found = false; while (nextSkinIndex != startingIndex) { if (IsSkinAvailable(nextSkinIndex)) { found = true; break; } nextSkinIndex = (nextSkinIndex + 1) % characterSkins.Count; } if (found && IsSkinAvailable(nextSkinIndex)) { currentSkinIndex = nextSkinIndex; UpdateCharacterSprite(); PlayerPrefs.SetInt("SelectedSkinIndex", currentSkinIndex); PlayerPrefs.Save(); } } private bool IsSkinAvailable(int index) { if (TEST) return true; return characterSkins[index].isUnlocked; } /* public void OpenShop() { if (shopCanvas != null) { shopCanvas.SetActive(true); } } public void CloseShop() { if (shopCanvas != null) { shopCanvas.SetActive(false); } } */ /* void InitializeShop() { if (shopContent == null || shopItemPrefab == null) { return; } foreach (Transform child in shopContent) { Destroy(child.gameObject); } for (int i = 0; i < characterSkins.Count; i++) { CharacterSkin skin = characterSkins[i]; if (skin.isHiddenFromShop) continue; GameObject item = Instantiate(shopItemPrefab, shopContent); item.transform.localScale = Vector3.one; Image skinImage = item.transform.Find("SkinImage").GetComponent<Image>(); if (skinImage != null && skin.releasedSprite != null) { skinImage.sprite = skin.releasedSprite; } TextMeshProUGUI skinNameText = item.transform.Find("SkinNameText").GetComponent<TextMeshProUGUI>(); if (skinNameText != null) { skinNameText.text = skin.skinName; } TextMeshProUGUI skinPriceText = item.transform.Find("SkinPriceText").GetComponent<TextMeshProUGUI>(); if (skinPriceText != null) { skinPriceText.text = (TEST || skin.isUnlocked) ? "Выбрать" : $"Цена: {skin.price} монет"; } Button purchaseButton = item.transform.Find("PurchaseButton").GetComponent<Button>(); TextMeshProUGUI buttonText = purchaseButton.GetComponentInChildren<TextMeshProUGUI>(); if (TEST || skin.isUnlocked) { buttonText.text = "Выбрать"; purchaseButton.onClick.AddListener(() => SelectSkin(i)); } else { buttonText.text = "Купить"; purchaseButton.onClick.AddListener(() => PurchaseSkinByIndex(i)); } } } */ public void SelectSkin(int skinIndex) { if (skinIndex < 0 || skinIndex >= characterSkins.Count) return; CharacterSkin skin = characterSkins[skinIndex]; if (TEST || skin.isUnlocked) { currentSkinIndex = skinIndex; UpdateCharacterSprite(); PlayerPrefs.SetInt("SelectedSkinIndex", currentSkinIndex); PlayerPrefs.Save(); // CloseShop(); } } /* public void PurchaseSkinByIndex(int skinIndex) { if (skinIndex < 0 || skinIndex >= characterSkins.Count) return; CharacterSkin skin = characterSkins[skinIndex]; if (skin.isUnlocked) { return; } GameManager gameManagerInstance = GameManager.Instance; if (gameManagerInstance == null) { return; } int currentMeters = Mathf.FloorToInt(gameManagerInstance.scoreScript.TotalMeters); int currentCoins = gameManagerInstance.totalCoins; int currentStars = gameManagerInstance.totalStars; if (currentMeters >= skin.requiredMeters && currentCoins >= skin.requiredCoins && currentStars >= skin.requiredStars && currentCoins >= skin.price) { gameManagerInstance.totalCoins -= skin.price; skin.isUnlocked = true; SaveSkinUnlockStatus(); gameManagerInstance.SavePlayerStats(); InitializeShop(); } } */ void SaveSkinUnlockStatus() { for (int i = 0; i < characterSkins.Count; i++) { if (TEST) { PlayerPrefs.SetInt($"SkinUnlocked_{i}", 1); characterSkins[i].isUnlocked = true; } else { PlayerPrefs.SetInt($"SkinUnlocked_{i}", characterSkins[i].isUnlocked ? 1 : 0); } } PlayerPrefs.Save(); } void LoadSkinUnlockStatus() { for (int i = 0; i < characterSkins.Count; i++) { CharacterSkin skin = characterSkins[i]; if (TEST) { skin.isUnlocked = true; PlayerPrefs.SetInt($"SkinUnlocked_{i}", 1); } else { if (skin.isHiddenFromShop) { skin.isUnlocked = true; PlayerPrefs.SetInt($"SkinUnlocked_{i}", 1); } else { if (PlayerPrefs.HasKey($"SkinUnlocked_{i}")) { skin.isUnlocked = PlayerPrefs.GetInt($"SkinUnlocked_{i}") == 1; } else { PlayerPrefs.SetInt($"SkinUnlocked_{i}", skin.isUnlocked ? 1 : 0); } } } } PlayerPrefs.Save(); } public void ResetSkinUnlocks() { for (int i = 0; i < characterSkins.Count; i++) { if (characterSkins[i].isHiddenFromShop) { characterSkins[i].isUnlocked = true; PlayerPrefs.SetInt($"SkinUnlocked_{i}", 1); } else { characterSkins[i].isUnlocked = false; PlayerPrefs.SetInt($"SkinUnlocked_{i}", 0); } } currentSkinIndex = 0; PlayerPrefs.SetInt("SelectedSkinIndex", currentSkinIndex); PlayerPrefs.Save(); LoadSkinUnlockStatus(); // InitializeShop(); UpdateCharacterSprite(); } void UpdateCharacterSprite() { if (characterSkins == null || characterSkins.Count == 0) { Debug.LogError("Character skins list is empty or null."); return; } if (currentSkinIndex < 0 || currentSkinIndex >= characterSkins.Count) { currentSkinIndex = 0; } CharacterSkin currentSkin = characterSkins[currentSkinIndex]; if (spriteRenderer == null) { Debug.LogError("SpriteRenderer is not assigned or missing on the GameObject."); return; } if (currentSkin == null) { Debug.LogError("Current skin is null."); return; } if (isAttached && currentSkin.pressedSprite != null) { spriteRenderer.sprite = currentSkin.pressedSprite; } else if (!isAttached && currentSkin.releasedSprite != null) { spriteRenderer.sprite = currentSkin.releasedSprite; } else { Debug.LogWarning("No sprite assigned for the current skin state."); } } public void MoveSkinUp(int index) { if (index <= 0 || index >= characterSkins.Count) return; var temp = characterSkins[index - 1]; characterSkins[index - 1] = characterSkins[index]; characterSkins[index] = temp; } public void MoveSkinDown(int index) { if (index < 0 || index >= characterSkins.Count - 1) return; var temp = characterSkins[index + 1]; characterSkins[index + 1] = characterSkins[index]; characterSkins[index] = temp; } void OnValidate() { if (Application.isPlaying) { SaveSkinUnlockStatus(); // InitializeShop(); UpdateCharacterSprite(); } } - Assistant
- User
- Assistant
- User
- Assistant
- User
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.