using TMPro; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; public class SingleLevelProbabilityPanel : MonoBehaviour { public TextMeshProUGUI levelNameText; public TMP_InputField inputField; public Button lockButton; public Image lockImg; public Image unlockImg; public Slider probabilitySlider; [SerializeField] private float probabilityValue = 0f; private bool isLocked = false; /// /// Initialize the level probability panel, setting the level name and probability value. /// /// The level name. /// The probability value. public void InitializeLevelProbabilityPanel(string levelName, float probability) { SetLevelName(levelName); SetProbability(probability); InitializeButton(); } /// /// Initialize the level probability panel, setting the level number and probability value. /// /// The level number. /// The probability value. public void InitializeLevelProbabilityPanel(int levelName, float probability) { SetLevelName(levelName); SetProbability(probability); InitializeButton(); } /// /// Set the level name. /// /// The level name. public void SetLevelName(string levelName) { levelNameText.text = levelName; } /// /// Set the level name. /// /// The level number. public void SetLevelName(int levelName) { levelNameText.text = "Level" + levelName.ToString(); } /// /// Set the probability value. /// /// The probability value. public void SetProbability(float prob) { probabilityValue = prob; UpdateProbabilityText(); UpdateProbabilitySlider(); } /// /// Updates the probability text display and can optionally synchronize the probability slider update. /// /// The probability value (optional). /// Whether to synchronize the probability slider update (default is false). /// /// If a probability value (value) is provided, it updates the probability text using that value. Otherwise, it parses the percentage value from the input field and updates the text. /// If syncToSlider is true, it also synchronizes the probability slider update. /// public void UpdateProbabilityText(float? value = null) { if (value != null) { probabilityValue = (float)value; } inputField.text = (probabilityValue * 100f).ToString("F1") + "%"; } /// /// Updates the value of the probability slider and can optionally synchronize the probability text update. /// /// The probability value (optional). /// Whether to synchronize the probability text update (default is false). /// /// If a probability value (value) is provided, it updates the probability slider using that value. Otherwise, it parses the percentage value from the input field and updates the slider. /// If syncToText is true, it also synchronizes the probability text update. /// public void UpdateProbabilitySlider(float? value = null) { if (value != null) { probabilityValue = (float)value; } probabilitySlider.value = probabilityValue; } /// /// Gets the current probability value. /// /// /// This property allows external code to read the current probability value. /// public float ProbabilityValue { get { return probabilityValue; } } /// /// Gets a value indicating whether the object is locked. /// /// /// Returns true if the object is locked; otherwise, returns false. /// public bool IsLocked { get { return isLocked; } } /// /// Gets a value indicating whether the object is unlocked. /// /// /// Returns true if the object is unlocked; otherwise, returns false. /// public bool UnLocked { get { return !isLocked; } } private void InitializeButton() { EventTrigger eventTrigger = lockButton.GetComponent(); if (eventTrigger == null) { eventTrigger = lockButton.gameObject.AddComponent(); } EventTrigger.Entry entry = new EventTrigger.Entry(); entry.eventID = EventTriggerType.PointerClick; entry.callback.AddListener((data) => { OnLockButtonClicked(); }); eventTrigger.triggers.Add(entry); // set lockImg as Unlock isLocked = false; UpdateLockImg(); } private void OnLockButtonClicked() { isLocked = !isLocked; UpdateLockImg(); probabilitySlider.interactable = !isLocked; inputField.interactable = !isLocked; } private void UpdateLockImg() { lockImg.gameObject.SetActive(isLocked); unlockImg.gameObject.SetActive(!isLocked); } }