Files
DeviantMobile-Rohan/Assets/Scripts/Settings/LoadControls.cs

190 lines
6.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LoadControls : MonoBehaviour
{
//Left Controls
public RectTransform leftControlRectTransform;
// public RectTransform blockControlRectTransform; // Removed
// public RectTransform dodgeControlRectTransform; // Removed
//Right Controls
// public RectTransform rightControlRectTransform;
public RectTransform punchControlRectTransform;
public RectTransform kickControlRectTransform;
// New control buttons
public RectTransform weaponPickupButtonRectTransform;
public RectTransform specialMoveButtonRectTransform;
public RectTransform shieldButtonRectTransform;
public RectTransform splintButtonRectTransform;
public RectTransform leftControlMiddle;
// public RectTransform rightControlMiddle; // Removed
// public RectTransform punchControlMiddle; // Removed
// public RectTransform kickControlMiddle; // Removed
private const string GameSettingsKey = "GameSettings";
GameSettings LoadedGamesettings;
void OnEnable(){
//If Key is present, then load
if (PlayerPrefs.HasKey(GameSettingsKey))
{
LoadedGamesettings = LoadGameSettings(); //Load
SetControlValues(LoadedGamesettings);
// Debug.Log("Loaded Settings");
//Default Control Settings
}else{
// SetDefaultSettings();
}
}
// Sets the Loaded Settings
void SetControlValues(GameSettings LoadedGamesettings){
// Only apply mentioned/new controls
SetAnchoredPosition(leftControlRectTransform, LoadedGamesettings.leftControl);
SetAnchoredPosition(punchControlRectTransform, LoadedGamesettings.punchControl);
SetAnchoredPosition(kickControlRectTransform, LoadedGamesettings.kickControl);
SetAnchoredPosition(weaponPickupButtonRectTransform, LoadedGamesettings.weaponPickupButton);
SetAnchoredPosition(specialMoveButtonRectTransform, LoadedGamesettings.specialMoveButton);
SetAnchoredPosition(shieldButtonRectTransform, LoadedGamesettings.shieldButton);
SetAnchoredPosition(splintButtonRectTransform, LoadedGamesettings.splintButton);
// Scale only the left control middle as requested
if (leftControlMiddle != null)
leftControlMiddle.sizeDelta = new Vector2(
leftControlMiddle.sizeDelta.x * LoadedGamesettings.leftControlScaleValue,
leftControlMiddle.sizeDelta.y * LoadedGamesettings.leftControlScaleValue
);
// Removed: right/punch/kick control middle scaling
}
// Function to load position and size details for Controls
public void SetAnchoredPosition(RectTransform rtTransform, ControlSettings controlsettings)
{
if (rtTransform == null || controlsettings == null) return; // Guard when settings not present yet
rtTransform.anchoredPosition = new Vector2(controlsettings.positionX, controlsettings.positionY);
rtTransform.sizeDelta = new Vector2(controlsettings.scaleWidth, controlsettings.scaleHeight);
}
// Sets the Default Control Settings
void SetDefaultSettings(){
GameSettings settings = new GameSettings
{
// Mentioned controls only
leftControl = new ControlSettings
{
positionX = 764f,
positionY = 663f,
scaleWidth = 1064.764f,
scaleHeight = 886.327f
},
punchControl = new ControlSettings
{
positionX = -1514f,
positionY = 1175.6f,
scaleWidth = 1502f,
scaleHeight = 1178f
},
kickControl = new ControlSettings
{
positionX = -674f,
positionY = 1129f,
scaleWidth = 1502f,
scaleHeight = 1178f
},
// New buttons
weaponPickupButton = new ControlSettings
{
positionX = -5854f,
positionY = 1267.2f,
scaleWidth = 3596.359f,
scaleHeight = 3194.071f
},
specialMoveButton = new ControlSettings
{
positionX = -2338f,
positionY = 397.94f,
scaleWidth = 1800.09f,
scaleHeight = 1451.95f
},
shieldButton = new ControlSettings
{
positionX = -1056f,
positionY = 528f,
scaleWidth = 1502f,
scaleHeight = 1178f
},
splintButton = new ControlSettings
{
positionX = -6897f,
positionY = 1682.7f,
scaleWidth = 3596.359f,
scaleHeight = 3194.071f
},
leftControlScaleValue = 1f,
// rightControlScaleValue = 1f, // Removed
kickControlScaleValue = 1f,
punchControlScaleValue = 1f,
GameplayVolume_value = 0.5f,
UIVolume_value = 0.5f,
MusicVolume_value = 0.5f,
SFXVolume_value = 0.5f,
audioVolumes = new string[]{
"50%", "50%", "50%", "50%",
}
};
SaveGameSettings(settings);
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
//Saving and Loading Game Settings
#region
public static void SaveGameSettings(GameSettings settings){
string json = JsonUtility.ToJson(settings);
PlayerPrefs.SetString(GameSettingsKey, json);
PlayerPrefs.Save();
}
public static GameSettings LoadGameSettings(){
if (PlayerPrefs.HasKey(GameSettingsKey))
{
string json = PlayerPrefs.GetString(GameSettingsKey);
return JsonUtility.FromJson<GameSettings>(json);
}
return new GameSettings(); // Return default settings if none are saved
}
#endregion
}