chunk 1: core gameplay scripts scenes runtime assets
This commit is contained in:
184
Assets/Scripts/Settings/ControlScaler.cs
Normal file
184
Assets/Scripts/Settings/ControlScaler.cs
Normal file
@@ -0,0 +1,184 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using TMPro;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class ControlScaler : MonoBehaviour{
|
||||
public static ControlScaler instance;
|
||||
public float leftControlScale = 1f, rightControlScale = 1f, punchControlScale = 1f, kickControlScale = 1f;
|
||||
|
||||
public Slider buttonScaler; //Slider used to alter button size, scaling up or down
|
||||
public Button[] buttonsArray; //Buttons considered to alter size
|
||||
|
||||
private Button selectedButton; //Reference to the current selected button
|
||||
private Color defaultButtonColor = new Color(1f, 1f, 1f); //Button default color
|
||||
public Color selectedButtonColor = new Color(0.9411765f, 0.6196079f, 0.03137255f); // Color when the button is selected
|
||||
|
||||
private GameSettings gameSettings; //Reference to GameSettings
|
||||
private bool isUpdatingSlider = false; //Flag for slider value change
|
||||
|
||||
private const string GameSettingsKey = "GameSettings";
|
||||
|
||||
|
||||
void OnEnable(){
|
||||
foreach (Button button in buttonsArray)
|
||||
{
|
||||
SetButtonColor(button, defaultButtonColor);
|
||||
}
|
||||
selectedButton = null;
|
||||
|
||||
|
||||
if (PlayerPrefs.HasKey(GameSettingsKey))
|
||||
{
|
||||
gameSettings = SettingsHandler.LoadGameSettings();
|
||||
|
||||
leftControlScale = gameSettings.leftControlScaleValue;
|
||||
rightControlScale = gameSettings.rightControlScaleValue;
|
||||
punchControlScale = gameSettings.punchControlScaleValue;
|
||||
kickControlScale = gameSettings.kickControlScaleValue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (instance == null)
|
||||
{
|
||||
instance = this;
|
||||
}
|
||||
else
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Start()
|
||||
{
|
||||
// Initialize the default button color
|
||||
if (buttonsArray.Length > 0)
|
||||
{
|
||||
defaultButtonColor = buttonsArray[0].colors.normalColor;
|
||||
}
|
||||
|
||||
// Add listeners to the buttons
|
||||
foreach (Button button in buttonsArray)
|
||||
{
|
||||
button.onClick.AddListener(() => ControlButtonClicked(button));
|
||||
// button.onClick.AddListener(ControlButtonClicked);
|
||||
}
|
||||
|
||||
// Add a listener to the slider
|
||||
buttonScaler.onValueChanged.AddListener(OnSliderValueChanged);
|
||||
}
|
||||
|
||||
void Update(){
|
||||
if (selectedButton != null)
|
||||
{
|
||||
// Debug.Log(selectedButton.name);
|
||||
SetButtonColor(selectedButton, selectedButtonColor); //Set color of current selected button
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Function called when slider value is changed, which should change button size based on the slider value
|
||||
void OnSliderValueChanged(float value)
|
||||
{
|
||||
if (isUpdatingSlider) return;
|
||||
|
||||
// For now, it just ensures the selected button color is maintained
|
||||
if (selectedButton != null)
|
||||
{
|
||||
SetButtonColor(selectedButton, selectedButtonColor);
|
||||
|
||||
string clickedGO = (selectedButton.gameObject).name;
|
||||
(float originalWidth, float originalHeight) = SettingsHandler.OriginalScales(clickedGO);
|
||||
SetButtonSize(value, originalHeight, originalWidth, clickedGO);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Function to change current selected button's color
|
||||
private void SetButtonColor(Button button, Color color){
|
||||
ColorBlock colorBlock = button.colors;
|
||||
colorBlock.normalColor = color;
|
||||
button.colors = colorBlock;
|
||||
}
|
||||
|
||||
|
||||
// Function to set the size of the button based on the slider value, adjusting the button's size
|
||||
void SetButtonSize(float value, float originalHeight, float originalWidth, string clickedGO){
|
||||
if (selectedButton != null){
|
||||
// Calculate the new width and height based on the slider value
|
||||
float newWidth = originalWidth * value;
|
||||
float newHeight = originalHeight * value;
|
||||
|
||||
RectTransform buttonRectTransform = selectedButton.GetComponent<RectTransform>();
|
||||
buttonRectTransform.sizeDelta = new Vector2(newWidth, newHeight);
|
||||
|
||||
|
||||
//leftControlScale = 1f, rightControlScale = 1f, punchControlScale = 1f, kickControlScale = 1f;
|
||||
switch(clickedGO){
|
||||
case "left_control":
|
||||
leftControlScale = value;
|
||||
Debug.Log($"Left {leftControlScale}");
|
||||
break;
|
||||
|
||||
case "right_control":
|
||||
rightControlScale = value;
|
||||
Debug.Log($"Right {rightControlScale}");
|
||||
break;
|
||||
|
||||
case "kick":
|
||||
kickControlScale = value;
|
||||
Debug.Log($"Kick {kickControlScale}");
|
||||
break;
|
||||
|
||||
case "punch":
|
||||
punchControlScale = value;
|
||||
Debug.Log($"Punch {punchControlScale}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Function called when a button is clicked, the button is then to be scaled
|
||||
void ControlButtonClicked(Button button){
|
||||
gameSettings = SettingsHandler.LoadGameSettings();
|
||||
string clickedGO = (button.gameObject).name;
|
||||
|
||||
Debug.Log($"{SettingsHandler.OriginalScales(clickedGO)}");
|
||||
|
||||
(float originalWidth, float originalHeight) = SettingsHandler.OriginalScales(clickedGO);
|
||||
(float currentControlWidth, float currentControlHeight) = SettingsHandler.LoadedScale(clickedGO);
|
||||
SetScalerSlider(originalWidth, originalHeight, currentControlWidth, currentControlHeight);
|
||||
|
||||
selectedButton = button;
|
||||
(buttonScaler.gameObject).SetActive(true);
|
||||
|
||||
foreach (Button btn in buttonsArray){
|
||||
if (btn.name == button.name){
|
||||
continue;
|
||||
|
||||
}else{
|
||||
ColorBlock colorBlock = btn.colors;
|
||||
colorBlock.normalColor = defaultButtonColor;
|
||||
btn.colors = colorBlock;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//Sets the Slider value to show value of the current button clicked, which is to be scaled
|
||||
void SetScalerSlider(float originalWidth, float originalHeight, float currentControlWidth, float currentControlHeight){
|
||||
// Calculate the normalized values
|
||||
float normalizedWidth = currentControlWidth / originalWidth;
|
||||
|
||||
// Temporarily disable the slider's onValueChanged listener
|
||||
isUpdatingSlider = true;
|
||||
buttonScaler.value = normalizedWidth;
|
||||
isUpdatingSlider = false;
|
||||
}
|
||||
|
||||
}
|
||||
11
Assets/Scripts/Settings/ControlScaler.cs.meta
Normal file
11
Assets/Scripts/Settings/ControlScaler.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 96a7028574585b245ab7fe5defb734f3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
81
Assets/Scripts/Settings/DraggableUI.cs
Normal file
81
Assets/Scripts/Settings/DraggableUI.cs
Normal file
@@ -0,0 +1,81 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class DraggableUI : MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler
|
||||
{
|
||||
private RectTransform rectTransform;
|
||||
private Canvas canvas;
|
||||
private CanvasGroup canvasGroup;
|
||||
private Vector2 pointerStartLocalCursor; //The initial pointer position when the drag starts
|
||||
private bool isDragging = false; //Flag to check if a component is being dragged
|
||||
|
||||
private SettingsHandler settingsHandler; // Reference to SettingsHandler
|
||||
public GameSettings gameSettings; // Reference to GameSettings
|
||||
|
||||
void Start()
|
||||
{
|
||||
rectTransform = GetComponent<RectTransform>();
|
||||
canvas = GetComponentInParent<Canvas>();
|
||||
canvasGroup = GetComponent<CanvasGroup>();
|
||||
|
||||
// Find SettingsHandler in the scene or use another method to get reference
|
||||
settingsHandler = FindObjectOfType<SettingsHandler>();
|
||||
}
|
||||
|
||||
|
||||
// Function for initial when gameobject is pressed on
|
||||
public void OnPointerDown(PointerEventData eventData)
|
||||
{
|
||||
//converts the screen point where the pointer was pressed to a local point within the rectangle
|
||||
RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, eventData.position, eventData.pressEventCamera, out pointerStartLocalCursor);
|
||||
|
||||
canvasGroup.blocksRaycasts = false;
|
||||
isDragging = false;
|
||||
}
|
||||
|
||||
|
||||
// Function for when a gameobject is being dragged
|
||||
public void OnDrag(PointerEventData eventData)
|
||||
{
|
||||
isDragging = true;
|
||||
Vector2 localPointerPos;
|
||||
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform, eventData.position, eventData.pressEventCamera, out localPointerPos))
|
||||
{
|
||||
rectTransform.localPosition = localPointerPos - pointerStartLocalCursor;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Function for when a gameobject is released, after being dragged
|
||||
public void OnPointerUp(PointerEventData eventData)
|
||||
{
|
||||
canvasGroup.blocksRaycasts = true;
|
||||
|
||||
if (!isDragging)
|
||||
{
|
||||
// Simulate a button click
|
||||
Button button = GetComponent<Button>();
|
||||
if (button != null)
|
||||
{
|
||||
button.onClick.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Function to Save Control Settings
|
||||
void SaveSettings()
|
||||
{
|
||||
if (settingsHandler != null)
|
||||
{
|
||||
settingsHandler.SaveSettings(); // Call SaveSettings from SettingsHandler
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("SettingsHandler reference is null.");
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Settings/DraggableUI.cs.meta
Normal file
11
Assets/Scripts/Settings/DraggableUI.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bc520d5cbdb54bc49834fbe54b560c4c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
189
Assets/Scripts/Settings/LoadControls.cs
Normal file
189
Assets/Scripts/Settings/LoadControls.cs
Normal file
@@ -0,0 +1,189 @@
|
||||
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
|
||||
}
|
||||
11
Assets/Scripts/Settings/LoadControls.cs.meta
Normal file
11
Assets/Scripts/Settings/LoadControls.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 94b41b7a4546d084e9355c50b0fee95f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
434
Assets/Scripts/Settings/SettingsHandler.cs
Normal file
434
Assets/Scripts/Settings/SettingsHandler.cs
Normal file
@@ -0,0 +1,434 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using TMPro;
|
||||
using System;
|
||||
|
||||
public class SettingsHandler : MonoBehaviour{
|
||||
/*STEPS
|
||||
1. Load PlayerPrefs for Settings, if not available then load default values
|
||||
2. If PlayerPrefs are available, then load the Settings UI with the current values
|
||||
3. Upon changing a setting, automatically update the PlayerPref associated with that setting
|
||||
|
||||
Sounds
|
||||
Vibration
|
||||
*/
|
||||
private GameSettings gameSettings;
|
||||
|
||||
private const string GameSettingsKey = "GameSettings";
|
||||
|
||||
public GameObject popup;
|
||||
|
||||
|
||||
//Define and Attach all GameObjects that need Positioning and Scaling here, total 6 in other game
|
||||
//Left Controls
|
||||
public RectTransform leftControlRectTransform;
|
||||
// public RectTransform blockControlRectTransform; // Removed
|
||||
// public RectTransform dodgeControlRectTransform; // Removed
|
||||
|
||||
//Right Controls
|
||||
// public RectTransform rightControlRectTransform; // Removed
|
||||
public RectTransform punchControlRectTransform;
|
||||
public RectTransform kickControlRectTransform;
|
||||
|
||||
// New control buttons for unified system
|
||||
public RectTransform weaponPickupButtonRectTransform;
|
||||
public RectTransform specialMoveButtonRectTransform;
|
||||
public RectTransform shieldButtonRectTransform;
|
||||
public RectTransform splintButtonRectTransform;
|
||||
public Slider[] VolumeSliderArray; //0. GameplayVolume, 1. UIVolume, 2. MusicVolume, 3. SFXVolume
|
||||
public TextMeshProUGUI[] VolumeSliderValues; //0. GameplayVolume, 1. UIVolume, 2. MusicVolume, 3. SFXVolume
|
||||
|
||||
// public TextMeshProUGUI lt;
|
||||
|
||||
//For Privacy & Socials
|
||||
public Button[] SocialButtons;
|
||||
|
||||
void OnEnable(){
|
||||
//If Key is present, then load
|
||||
if (PlayerPrefs.HasKey(GameSettingsKey))
|
||||
{
|
||||
LoadSettings(); //Load
|
||||
// ResetControlSettings();
|
||||
Debug.Log("Present");
|
||||
|
||||
|
||||
}else{
|
||||
DefaultSettings(); //Set Volume and Controls Settings
|
||||
// SaveGameSettings();
|
||||
}
|
||||
|
||||
foreach (Button btn in SocialButtons){
|
||||
btn.onClick.AddListener(() => OpenSocialsLink((btn.gameObject).name));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Function to open a specific social based on User interaction
|
||||
void OpenSocialsLink(String SocialsClickedName){
|
||||
switch(SocialsClickedName){
|
||||
case "facebook":
|
||||
//Open Facebook
|
||||
Application.OpenURL("https://www.facebook.com/PlayDEVIANT");
|
||||
break;
|
||||
|
||||
case "youtube":
|
||||
//Open Youtube
|
||||
Application.OpenURL("https://www.youtube.com/@PlayDEVIANT");
|
||||
break;
|
||||
|
||||
case "instagram":
|
||||
//Open Instagram
|
||||
Application.OpenURL("https://www.instagram.com/playdeviant/");
|
||||
break;
|
||||
|
||||
case "whatsapp":
|
||||
//Open Whatsapp
|
||||
Application.OpenURL("https://whatsapp.com/channel/0029Va8alUzHgZWdoyh8db1K");
|
||||
break;
|
||||
|
||||
case "tiktok":
|
||||
//Open Tiktok
|
||||
Application.OpenURL("https://www.tiktok.com/@playdeviant");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Start(){
|
||||
// DefaultSettings(); //Set Volume and Controls Settings
|
||||
|
||||
//If Key is present, then load
|
||||
if (PlayerPrefs.HasKey(GameSettingsKey))
|
||||
{
|
||||
LoadSettings(); //Load
|
||||
|
||||
}else{
|
||||
DefaultSettings(); //Set Volume and Controls Settings
|
||||
// SaveGameSettings();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void DefaultSettings(){
|
||||
DefaultControlsSettings();
|
||||
|
||||
//Reset Volume Settings, Set to Full Volume
|
||||
foreach (Slider sld in VolumeSliderArray){
|
||||
sld.value = 1f;
|
||||
}
|
||||
|
||||
foreach (TextMeshProUGUI txtM in VolumeSliderValues){
|
||||
txtM.text = "100%";
|
||||
}
|
||||
|
||||
SaveSettings();
|
||||
}
|
||||
|
||||
|
||||
void Update(){
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void SaveSettings()
|
||||
{
|
||||
GameSettings settings = new GameSettings
|
||||
{
|
||||
leftControl = new ControlSettings
|
||||
{
|
||||
positionX = leftControlRectTransform.anchoredPosition.x,
|
||||
positionY = leftControlRectTransform.anchoredPosition.y,
|
||||
scaleWidth = leftControlRectTransform.sizeDelta.x,
|
||||
scaleHeight = leftControlRectTransform.sizeDelta.y
|
||||
},
|
||||
punchControl = new ControlSettings
|
||||
{
|
||||
positionX = punchControlRectTransform.anchoredPosition.x,
|
||||
positionY = punchControlRectTransform.anchoredPosition.y,
|
||||
scaleWidth = punchControlRectTransform.sizeDelta.x,
|
||||
scaleHeight = punchControlRectTransform.sizeDelta.y
|
||||
},
|
||||
kickControl = new ControlSettings
|
||||
{
|
||||
positionX = kickControlRectTransform.anchoredPosition.x,
|
||||
positionY = kickControlRectTransform.anchoredPosition.y,
|
||||
scaleWidth = kickControlRectTransform.sizeDelta.x,
|
||||
scaleHeight = kickControlRectTransform.sizeDelta.y
|
||||
},
|
||||
|
||||
// New buttons
|
||||
weaponPickupButton = new ControlSettings
|
||||
{
|
||||
positionX = weaponPickupButtonRectTransform.anchoredPosition.x,
|
||||
positionY = weaponPickupButtonRectTransform.anchoredPosition.y,
|
||||
scaleWidth = weaponPickupButtonRectTransform.sizeDelta.x,
|
||||
scaleHeight = weaponPickupButtonRectTransform.sizeDelta.y
|
||||
},
|
||||
specialMoveButton = new ControlSettings
|
||||
{
|
||||
positionX = specialMoveButtonRectTransform.anchoredPosition.x,
|
||||
positionY = specialMoveButtonRectTransform.anchoredPosition.y,
|
||||
scaleWidth = specialMoveButtonRectTransform.sizeDelta.x,
|
||||
scaleHeight = specialMoveButtonRectTransform.sizeDelta.y
|
||||
},
|
||||
shieldButton = new ControlSettings
|
||||
{
|
||||
positionX = shieldButtonRectTransform.anchoredPosition.x,
|
||||
positionY = shieldButtonRectTransform.anchoredPosition.y,
|
||||
scaleWidth = shieldButtonRectTransform.sizeDelta.x,
|
||||
scaleHeight = shieldButtonRectTransform.sizeDelta.y
|
||||
},
|
||||
splintButton = new ControlSettings
|
||||
{
|
||||
positionX = splintButtonRectTransform.anchoredPosition.x,
|
||||
positionY = splintButtonRectTransform.anchoredPosition.y,
|
||||
scaleWidth = splintButtonRectTransform.sizeDelta.x,
|
||||
scaleHeight = splintButtonRectTransform.sizeDelta.y
|
||||
},
|
||||
|
||||
////leftControlScale = 1f, rightControlScale = 1f, punchControlScale = 1f, kickControlScale = 1f;
|
||||
leftControlScaleValue = ControlScaler.instance.leftControlScale,
|
||||
rightControlScaleValue = ControlScaler.instance.rightControlScale,
|
||||
kickControlScaleValue = ControlScaler.instance.kickControlScale,
|
||||
punchControlScaleValue = ControlScaler.instance.punchControlScale,
|
||||
|
||||
GameplayVolume_value = VolumeSliderArray[0].value,
|
||||
UIVolume_value = VolumeSliderArray[1].value,
|
||||
MusicVolume_value = VolumeSliderArray[2].value,
|
||||
SFXVolume_value = VolumeSliderArray[3].value,
|
||||
|
||||
|
||||
audioVolumes = new string[]{
|
||||
(VolumeSliderValues[0]).text,
|
||||
(VolumeSliderValues[1]).text,
|
||||
(VolumeSliderValues[2]).text,
|
||||
(VolumeSliderValues[3]).text,
|
||||
}
|
||||
};
|
||||
|
||||
SaveGameSettings(settings);
|
||||
}
|
||||
|
||||
|
||||
// public static GameSettings LoadSettings()
|
||||
GameSettings settings;
|
||||
void LoadSettings()
|
||||
{
|
||||
settings = LoadGameSettings();
|
||||
|
||||
// Apply settings
|
||||
SetAnchoredPosition(leftControlRectTransform, settings.leftControl);
|
||||
SetAnchoredPosition(punchControlRectTransform, settings.punchControl);
|
||||
SetAnchoredPosition(kickControlRectTransform, settings.kickControl);
|
||||
|
||||
SetAnchoredPosition(weaponPickupButtonRectTransform, settings.weaponPickupButton);
|
||||
SetAnchoredPosition(specialMoveButtonRectTransform, settings.specialMoveButton);
|
||||
SetAnchoredPosition(shieldButtonRectTransform, settings.shieldButton);
|
||||
SetAnchoredPosition(splintButtonRectTransform, settings.splintButton);
|
||||
|
||||
VolumeSliderArray[0].value = settings.GameplayVolume_value;
|
||||
VolumeSliderArray[1].value = settings.UIVolume_value;
|
||||
VolumeSliderArray[2].value = settings.MusicVolume_value;
|
||||
VolumeSliderArray[3].value = settings.SFXVolume_value;
|
||||
|
||||
(VolumeSliderValues[0]).text = settings.audioVolumes[0];
|
||||
(VolumeSliderValues[1]).text = settings.audioVolumes[1];
|
||||
(VolumeSliderValues[2]).text = settings.audioVolumes[2];
|
||||
(VolumeSliderValues[3]).text = settings.audioVolumes[3];
|
||||
}
|
||||
|
||||
|
||||
// Function to load position and size details for Controls
|
||||
public void SetAnchoredPosition(RectTransform rtTransform, ControlSettings controlsettings)
|
||||
{
|
||||
if (rtTransform == null || controlsettings == null) return;
|
||||
rtTransform.anchoredPosition = new Vector2(controlsettings.positionX, controlsettings.positionY);
|
||||
rtTransform.sizeDelta = new Vector2(controlsettings.scaleWidth, controlsettings.scaleHeight);
|
||||
}
|
||||
|
||||
|
||||
// Function that returns Original Scales of Controls
|
||||
// private (float, float)
|
||||
// leftControlSize = (1064.764f, 886.327f),
|
||||
// blockControlSize = (596.32f, 160.784f),
|
||||
// dodgeControlSize = (596.32f, 160.784f),
|
||||
// rightControlSize = (1064.764f, 886.3271f),
|
||||
// kickControlSize = (1064.764f, 886.3271f),
|
||||
// punchControlSize = (1064.764f, 886.3271f);
|
||||
|
||||
public static (float, float) OriginalScales(string rectReference){
|
||||
switch (rectReference){
|
||||
case "left_control":
|
||||
return (leftControlSize.Item1, leftControlSize.Item2);
|
||||
// return (1550.608f, 1290.752f);
|
||||
|
||||
case "weapon_pickup":
|
||||
return (weaponPickupButtonSize.Item1, weaponPickupButtonSize.Item2);
|
||||
|
||||
case "special_move":
|
||||
return (specialMoveButtonSize.Item1, specialMoveButtonSize.Item2);
|
||||
|
||||
case "shield":
|
||||
return (shieldButtonSize.Item1, shieldButtonSize.Item2);
|
||||
|
||||
case "splint":
|
||||
return (splintButtonSize.Item1, splintButtonSize.Item2);
|
||||
|
||||
case "kick":
|
||||
return (kickControlSize.Item1, kickControlSize.Item2);
|
||||
|
||||
case "punch":
|
||||
return (punchControlSize.Item1, punchControlSize.Item2);
|
||||
|
||||
default:
|
||||
return (0f, 0f);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private static GameSettings ControlScaleGameSettings;
|
||||
public static (float, float) LoadedScale(string rectReference){
|
||||
ControlScaleGameSettings = LoadGameSettings();
|
||||
|
||||
switch (rectReference){
|
||||
case "left_control":
|
||||
return (ControlScaleGameSettings.leftControl.scaleWidth, ControlScaleGameSettings.leftControl.scaleHeight);
|
||||
|
||||
case "weapon_pickup":
|
||||
return (ControlScaleGameSettings.weaponPickupButton != null ? ControlScaleGameSettings.weaponPickupButton.scaleWidth : 0f,
|
||||
ControlScaleGameSettings.weaponPickupButton != null ? ControlScaleGameSettings.weaponPickupButton.scaleHeight : 0f);
|
||||
|
||||
case "special_move":
|
||||
return (ControlScaleGameSettings.specialMoveButton != null ? ControlScaleGameSettings.specialMoveButton.scaleWidth : 0f,
|
||||
ControlScaleGameSettings.specialMoveButton != null ? ControlScaleGameSettings.specialMoveButton.scaleHeight : 0f);
|
||||
|
||||
case "shield":
|
||||
return (ControlScaleGameSettings.shieldButton != null ? ControlScaleGameSettings.shieldButton.scaleWidth : 0f,
|
||||
ControlScaleGameSettings.shieldButton != null ? ControlScaleGameSettings.shieldButton.scaleHeight : 0f);
|
||||
|
||||
case "splint":
|
||||
return (ControlScaleGameSettings.splintButton != null ? ControlScaleGameSettings.splintButton.scaleWidth : 0f,
|
||||
ControlScaleGameSettings.splintButton != null ? ControlScaleGameSettings.splintButton.scaleHeight : 0f);
|
||||
|
||||
case "kick":
|
||||
return (ControlScaleGameSettings.kickControl.scaleWidth, ControlScaleGameSettings.kickControl.scaleHeight);
|
||||
|
||||
case "punch":
|
||||
return (ControlScaleGameSettings.punchControl.scaleWidth, ControlScaleGameSettings.punchControl.scaleHeight);
|
||||
|
||||
default:
|
||||
return (0f, 0f);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Default Position and Scale values
|
||||
private static (float, float)
|
||||
leftControlPosition = (1012f, 836f),
|
||||
punchControlPosition = (-1046f, 1497f),
|
||||
kickControlPosition = (-651f, 856f),
|
||||
weaponPickupButtonPosition = (1500f, 500f),
|
||||
specialMoveButtonPosition = (1750f, 350f),
|
||||
shieldButtonPosition = (1250f, 350f),
|
||||
splintButtonPosition = (1000f, 350f);
|
||||
|
||||
|
||||
private static (float, float)
|
||||
leftControlSize = (1236.143f, 1028.98f),
|
||||
punchControlSize = (786.249f, 654.485f),
|
||||
kickControlSize = (802.66f, 668.147f),
|
||||
weaponPickupButtonSize = (300f, 300f),
|
||||
specialMoveButtonSize = (300f, 300f),
|
||||
shieldButtonSize = (300f, 300f),
|
||||
splintButtonSize = (300f, 300f);
|
||||
|
||||
|
||||
// Function that contains default Control sizes
|
||||
void DefaultControlsSettings(){
|
||||
//Controls Position
|
||||
leftControlRectTransform.anchoredPosition = new Vector2(leftControlPosition.Item1, leftControlPosition.Item2);
|
||||
punchControlRectTransform.anchoredPosition = new Vector2(punchControlPosition.Item1, punchControlPosition.Item2);
|
||||
kickControlRectTransform.anchoredPosition = new Vector2(kickControlPosition.Item1, kickControlPosition.Item2);
|
||||
if (weaponPickupButtonRectTransform != null)
|
||||
weaponPickupButtonRectTransform.anchoredPosition = new Vector2(weaponPickupButtonPosition.Item1, weaponPickupButtonPosition.Item2);
|
||||
if (specialMoveButtonRectTransform != null)
|
||||
specialMoveButtonRectTransform.anchoredPosition = new Vector2(specialMoveButtonPosition.Item1, specialMoveButtonPosition.Item2);
|
||||
if (shieldButtonRectTransform != null)
|
||||
shieldButtonRectTransform.anchoredPosition = new Vector2(shieldButtonPosition.Item1, shieldButtonPosition.Item2);
|
||||
if (splintButtonRectTransform != null)
|
||||
splintButtonRectTransform.anchoredPosition = new Vector2(splintButtonPosition.Item1, splintButtonPosition.Item2);
|
||||
|
||||
|
||||
//Controls Scale, Width and Height
|
||||
leftControlRectTransform.sizeDelta = new Vector2(leftControlSize.Item1, leftControlSize.Item2);
|
||||
kickControlRectTransform.sizeDelta = new Vector2(kickControlSize.Item1, kickControlSize.Item2);
|
||||
punchControlRectTransform.sizeDelta = new Vector2(punchControlSize.Item1, punchControlSize.Item2);
|
||||
if (weaponPickupButtonRectTransform != null)
|
||||
weaponPickupButtonRectTransform.sizeDelta = new Vector2(weaponPickupButtonSize.Item1, weaponPickupButtonSize.Item2);
|
||||
if (specialMoveButtonRectTransform != null)
|
||||
specialMoveButtonRectTransform.sizeDelta = new Vector2(specialMoveButtonSize.Item1, specialMoveButtonSize.Item2);
|
||||
if (shieldButtonRectTransform != null)
|
||||
shieldButtonRectTransform.sizeDelta = new Vector2(shieldButtonSize.Item1, shieldButtonSize.Item2);
|
||||
if (splintButtonRectTransform != null)
|
||||
splintButtonRectTransform.sizeDelta = new Vector2(splintButtonSize.Item1, splintButtonSize.Item2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Function to Reset Control Settings, Linked to UI Reset button
|
||||
public void ResetControlSettings(){
|
||||
// Apply these values to the RectTransform of the UI element
|
||||
|
||||
// Save the settings
|
||||
DefaultControlsSettings();
|
||||
SaveSettings();
|
||||
StartCoroutine(AnimatePopup("Reset Successful"));
|
||||
}
|
||||
|
||||
|
||||
// Function to Save Control Settings, Linked to UI Save button
|
||||
public void SaveControlSettings(){
|
||||
|
||||
// Save the settings
|
||||
SaveSettings();
|
||||
StartCoroutine(AnimatePopup("Save Successful"));
|
||||
}
|
||||
|
||||
|
||||
// Function to show and hide GameObject
|
||||
IEnumerator AnimatePopup(string msg)
|
||||
{
|
||||
popup.SetActive(true);
|
||||
((popup.transform.Find("text").gameObject).GetComponent<TextMeshProUGUI>()).text = msg;
|
||||
yield return new WaitForSeconds(1f);
|
||||
popup.SetActive(false);
|
||||
}
|
||||
|
||||
|
||||
|
||||
//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
|
||||
}
|
||||
|
||||
|
||||
|
||||
11
Assets/Scripts/Settings/SettingsHandler.cs.meta
Normal file
11
Assets/Scripts/Settings/SettingsHandler.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 12932df221112fc45ab13569b72aff04
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
193
Assets/Scripts/Settings/SettingsNavigation.cs
Normal file
193
Assets/Scripts/Settings/SettingsNavigation.cs
Normal file
@@ -0,0 +1,193 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using TMPro;
|
||||
using System;
|
||||
|
||||
|
||||
public class SettingsNavigation : MonoBehaviour
|
||||
{
|
||||
public GameObject TabContent_Parent;
|
||||
|
||||
//4 total Main tabs in the Settings UI
|
||||
// public GameObject Gameplay_Content;
|
||||
public GameObject Controls_Content;
|
||||
public GameObject GraphicsAudio_Content;
|
||||
// public GameObject Controller_Content;
|
||||
public GameObject Language_Content;
|
||||
public GameObject PrivacySocials_Content;
|
||||
public Color ButtonSelected_Color = new Color(0.149f, 0.682f, 0.373f); // Color for "#26AE5F"
|
||||
public Color WhiteColor = new Color(1f, 1f, 1f); // Color for "#FFFFFF", or White
|
||||
|
||||
//Controls Navigation
|
||||
public GameObject[] ControlsNavigation; //0. Controller Button, 1. Controller GameObject, 2. Mobile Button, 3. Mobile GameObject, 4. Save button, 5. Reset button
|
||||
|
||||
// Assuming you have multiple buttons as child objects of this GameObject
|
||||
private Button[] settings_navigation_buttons;
|
||||
|
||||
|
||||
void PlayClickAudio(){
|
||||
// audioSource.Play();
|
||||
}
|
||||
|
||||
|
||||
void OnEnable(){
|
||||
HandleControlSettings();
|
||||
|
||||
settings_navigation_buttons = GetComponentsInChildren<Button>();
|
||||
|
||||
foreach (Button button in settings_navigation_buttons){
|
||||
button.onClick.AddListener(() => SettingsClickHandler(button));
|
||||
}
|
||||
|
||||
ActivateUI(GraphicsAudio_Content, "GraphicsAudio");
|
||||
}
|
||||
|
||||
|
||||
//For Control Settings, Handle Mobile and Controller Settings, so that Controller settings are the first shown when Controls tab is selected
|
||||
#region
|
||||
void HandleControlSettings(){
|
||||
GameObject gameobject;
|
||||
for (int i = 0; i < ControlsNavigation.Length; i++) {
|
||||
gameobject = ControlsNavigation[i];
|
||||
|
||||
//Buttons
|
||||
if (i == 0 || i == 2) {
|
||||
|
||||
// Create a local copy of i
|
||||
int index = i;
|
||||
|
||||
//Adding Listeners
|
||||
Button button = gameobject.GetComponent<Button>();
|
||||
button.onClick.AddListener(() => HandleControlView(index));
|
||||
|
||||
continue; // Skip index 0 and 2
|
||||
}
|
||||
|
||||
//Default to view
|
||||
if (i == 1){
|
||||
gameobject.SetActive(true);
|
||||
|
||||
}else{
|
||||
gameobject.SetActive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HandleControlView(int i){
|
||||
// Debug.Log($"{i} I hate it");
|
||||
GameObject controllerGameobject = ControlsNavigation[1];
|
||||
GameObject mobileGameobject = ControlsNavigation[3];
|
||||
if (i == 0){
|
||||
mobileGameobject.SetActive(false);
|
||||
controllerGameobject.SetActive(true);
|
||||
|
||||
(ControlsNavigation[4]).SetActive(false);
|
||||
(ControlsNavigation[5]).SetActive(false);
|
||||
(ControlsNavigation[6]).SetActive(false);
|
||||
}
|
||||
|
||||
if (i == 2){
|
||||
controllerGameobject.SetActive(false);
|
||||
mobileGameobject.SetActive(true);
|
||||
|
||||
(ControlsNavigation[4]).SetActive(true);
|
||||
(ControlsNavigation[5]).SetActive(true);
|
||||
(ControlsNavigation[6]).SetActive(true);
|
||||
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
// Method to handle button clicks
|
||||
void SettingsClickHandler(Button clickedButton){
|
||||
|
||||
// Determine which button is clicked
|
||||
if (clickedButton != null){
|
||||
string buttonName = clickedButton.gameObject.name;
|
||||
|
||||
//Perform actions based on the button clicked
|
||||
switch (buttonName){
|
||||
// case "GamePlay":
|
||||
// ActivateUI(Gameplay_Content, buttonName);
|
||||
// break;
|
||||
|
||||
case "Controls":
|
||||
StopSounds();
|
||||
ActivateUI(Controls_Content, buttonName);
|
||||
break;
|
||||
|
||||
case "GraphicsAudio":
|
||||
StopSounds();
|
||||
ActivateUI(GraphicsAudio_Content, buttonName);
|
||||
break;
|
||||
|
||||
// case "Controller":
|
||||
// ActivateUI(Controller_Content, buttonName);
|
||||
// break;
|
||||
|
||||
case "Language":
|
||||
StopSounds();
|
||||
ActivateUI(Language_Content, buttonName);
|
||||
break;
|
||||
|
||||
case "PrivacySocials":
|
||||
StopSounds();
|
||||
ActivateUI(PrivacySocials_Content, buttonName);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Function to stop all audio in the Graphics & Audio tab content from playing
|
||||
void StopSounds(){
|
||||
SliderHandler sld = gameObject.GetComponent<SliderHandler>();
|
||||
sld.StopAllAudios();
|
||||
}
|
||||
|
||||
|
||||
// Function to activate a UI based on user interaction
|
||||
void ActivateUI(GameObject ActivegameObject, string buttonName){
|
||||
PlayClickAudio();
|
||||
|
||||
//Change text size
|
||||
TextMeshProUGUI Selectedbutton_txt = (gameObject.transform.Find(buttonName).gameObject).GetComponent<TextMeshProUGUI>();
|
||||
Selectedbutton_txt.color = ButtonSelected_Color;
|
||||
Selectedbutton_txt.fontSize = 180;
|
||||
|
||||
foreach(Transform child in (gameObject.transform)){
|
||||
|
||||
if ((child.gameObject.name != "Dividers") && (child.gameObject.name != buttonName)){
|
||||
|
||||
// Find the child with the name "hello"
|
||||
TextMeshProUGUI child_txt = child.GetComponent<TextMeshProUGUI>();
|
||||
child_txt.color = WhiteColor; //Set to white
|
||||
child_txt.fontSize = 160; //Set to white
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
foreach (Transform child in (TabContent_Parent.transform)){
|
||||
string currentGameObject = child.gameObject.name;
|
||||
child.gameObject.SetActive(false);
|
||||
}
|
||||
ActivegameObject.SetActive(true);
|
||||
}
|
||||
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Settings/SettingsNavigation.cs.meta
Normal file
11
Assets/Scripts/Settings/SettingsNavigation.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 58c94f6dcf84936408a2d03e842656ff
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
148
Assets/Scripts/Settings/SliderHandler.cs
Normal file
148
Assets/Scripts/Settings/SliderHandler.cs
Normal file
@@ -0,0 +1,148 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using TMPro;
|
||||
|
||||
|
||||
public class SliderHandler : MonoBehaviour
|
||||
{
|
||||
//First element is the text (shows Percent), Second element is the slider to change the value, Third element is the Gameobject with Audio Source
|
||||
public GameObject[] GameplayVolume;
|
||||
public GameObject[] UIVolume;
|
||||
public GameObject[] MusicVolume;
|
||||
public GameObject[] SFXVolume;
|
||||
public GameObject[] ControllerSensitivity;
|
||||
|
||||
private GameSettings gameSettings;
|
||||
|
||||
//Array of Audios, First element is Audio for Gameplay, Second for UI, Third for Music, Fourth for SFX
|
||||
public static AudioSource[] audioSources;
|
||||
|
||||
void OnEnable(){
|
||||
// Call the static LoadGameSettings function from SettingsHandler
|
||||
gameSettings = SettingsHandler.LoadGameSettings();
|
||||
|
||||
audioSources = new AudioSource[4]; //4 total elements
|
||||
audioSources[0] = GameplayVolume[2].GetComponent<AudioSource>(); //Gameplay Volume
|
||||
audioSources[1] = UIVolume[2].GetComponent<AudioSource>(); //UI Volume
|
||||
audioSources[2] = MusicVolume[2].GetComponent<AudioSource>(); //Music Volume
|
||||
audioSources[3] = SFXVolume[2].GetComponent<AudioSource>(); //SFX Volume
|
||||
|
||||
|
||||
(GameplayVolume[1].GetComponent<Slider>()).value = gameSettings.GameplayVolume_value;
|
||||
(UIVolume[1].GetComponent<Slider>()).value = gameSettings.UIVolume_value;
|
||||
(MusicVolume[1].GetComponent<Slider>()).value = gameSettings.MusicVolume_value;
|
||||
(SFXVolume[1].GetComponent<Slider>()).value = gameSettings.SFXVolume_value;
|
||||
|
||||
StopAllAudios();
|
||||
}
|
||||
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
AttachSliderListener(GameplayVolume);
|
||||
AttachSliderListener(UIVolume);
|
||||
AttachSliderListener(MusicVolume);
|
||||
AttachSliderListener(SFXVolume);
|
||||
// AttachSliderListener(ControllerSensitivity);
|
||||
}
|
||||
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
void AttachSliderListener(GameObject[] sliderArray){
|
||||
// Get the TextMeshProUGUI and Slider components
|
||||
TextMeshProUGUI textMeshPro = sliderArray[0].GetComponent<TextMeshProUGUI>();
|
||||
Slider slider = sliderArray[1].GetComponent<Slider>();
|
||||
|
||||
// Add listener for the slider value change
|
||||
// if (sliderArray != ControllerSensitivity){
|
||||
// AudioSource audio = sliderArray[2].GetComponent<AudioSource>();
|
||||
// }
|
||||
|
||||
AudioSource audio = sliderArray[2].GetComponent<AudioSource>();
|
||||
slider.onValueChanged.AddListener(value => OnSliderValueChanged(value, textMeshPro, audio, (sliderArray[1]).name));
|
||||
}
|
||||
|
||||
|
||||
// Method to handle slider value change
|
||||
GameSettings settings;
|
||||
void OnSliderValueChanged(float value, TextMeshProUGUI textMeshPro, AudioSource audio, string sliderName){
|
||||
float normalizedValue = value * 100f;
|
||||
textMeshPro.text = $"{normalizedValue:F0}%";
|
||||
|
||||
settings = SettingsHandler.LoadGameSettings();
|
||||
|
||||
switch(sliderName){
|
||||
case "GameplayVolume_value":
|
||||
settings.GameplayVolume_value = value;
|
||||
settings.audioVolumes[0] = textMeshPro.text; //Update Audio value to Audio Settings
|
||||
break;
|
||||
|
||||
case "UIVolume_value":
|
||||
settings.UIVolume_value = value;
|
||||
settings.audioVolumes[1] = textMeshPro.text; //Update Audio value to Audio Settings
|
||||
break;
|
||||
|
||||
case "MusicVolume_value":
|
||||
settings.MusicVolume_value = value;
|
||||
settings.audioVolumes[2] = textMeshPro.text; //Update Audio value to Audio Settings
|
||||
break;
|
||||
|
||||
case "SFXVolume_value":
|
||||
settings.SFXVolume_value = value;
|
||||
settings.audioVolumes[3] = textMeshPro.text; //Update Audio value to Audio Settings
|
||||
break;
|
||||
}
|
||||
|
||||
// settings.sliderName = value;
|
||||
SettingsHandler.SaveGameSettings(settings);
|
||||
|
||||
StopAllAudios();
|
||||
|
||||
// Set the audio volume based on the slider value
|
||||
audio.volume = value;
|
||||
audio.Play();
|
||||
|
||||
// Start the coroutine to play the audio for 1 second
|
||||
// StartCoroutine(PlayAudioForOneSecond(audio));
|
||||
|
||||
// Play the audio if it's not already playing
|
||||
// if (!audio.isPlaying)
|
||||
// {
|
||||
// audio.Play();
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
// Function to Stop all audio in the UI
|
||||
public void StopAllAudios(){
|
||||
foreach (AudioSource aud in audioSources){
|
||||
aud.time = 0f;
|
||||
aud.Stop();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Coroutine to play the audio for 1 second
|
||||
IEnumerator PlayAudioForOneSecond(AudioSource audio)
|
||||
{
|
||||
|
||||
// Play the audio
|
||||
audio.Play();
|
||||
|
||||
// Wait for 1 second
|
||||
yield return new WaitForSeconds(3f);
|
||||
|
||||
// Stop the audio
|
||||
// audio.Stop();
|
||||
StopAllAudios();
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Settings/SliderHandler.cs.meta
Normal file
11
Assets/Scripts/Settings/SliderHandler.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fba336899fdd81d4e91f16246d4938a5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
70
Assets/Scripts/Settings/ToggleHandler.cs
Normal file
70
Assets/Scripts/Settings/ToggleHandler.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class ToggleHandler : MonoBehaviour
|
||||
{
|
||||
//Each Toggle Array takes in values associated with each other, such as ON and OFF
|
||||
//Audio & Graphics Settings
|
||||
public GameObject[] GraphicsToggles;
|
||||
public GameObject[] FrameRateToggles;
|
||||
public GameObject[] ScreenVibrationToggles;
|
||||
|
||||
//Controller Settings
|
||||
public GameObject[] ControllerSupportToggles;
|
||||
|
||||
//Language Settings
|
||||
public GameObject[] LanguageToggles;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
AttachToggleListener(GraphicsToggles); //Attach listener to GraphicsToggles
|
||||
AttachToggleListener(FrameRateToggles); //Attach listener to FrameRateToggles
|
||||
AttachToggleListener(ScreenVibrationToggles); //Attach listener to ScreenVibrationToggles
|
||||
AttachToggleListener(ControllerSupportToggles); //Attach listener to ControllerSupportToggles
|
||||
AttachToggleListener(LanguageToggles); //Attach listener to LanguageToggles
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Function called to keep track of toggle interaction
|
||||
void AttachToggleListener(GameObject[] toggleArray){
|
||||
// Loop through each toggle to add a listener to its Toggle component
|
||||
foreach (GameObject toggleObj in toggleArray){
|
||||
Toggle toggle = toggleObj.GetComponent<Toggle>();
|
||||
if (toggle != null)
|
||||
{
|
||||
toggle.onValueChanged.AddListener((isSelected) => OnToggleValueChanged(toggleArray, toggle));
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("GameObject does not have a Toggle component!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Called when any toggle's value changes
|
||||
void OnToggleValueChanged(GameObject[] toggleArray, Toggle selectedToggle)
|
||||
{
|
||||
if (selectedToggle.isOn)
|
||||
{
|
||||
// Deselect all other toggles
|
||||
foreach (GameObject toggleObj in toggleArray)
|
||||
{
|
||||
Toggle toggle = toggleObj.GetComponent<Toggle>();
|
||||
if (toggle != null && toggle != selectedToggle)
|
||||
{
|
||||
toggle.isOn = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Settings/ToggleHandler.cs.meta
Normal file
11
Assets/Scripts/Settings/ToggleHandler.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ea792604bbc6f204d9b035506b4764cb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user