V3.3.3 LevelProbabilityPanel联动完成
添加限制总值小于1,最低值大于0限制
This commit is contained in:
@@ -1,14 +1,22 @@
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class SingleLevelProbabilityPanel : MonoBehaviour
|
||||
{
|
||||
public TextMeshProUGUI levelNameText;
|
||||
public TextMeshProUGUI probabilityText;
|
||||
public TMP_InputField inputField;
|
||||
public Button lockButton;
|
||||
public Image lockImg;
|
||||
public Image unlockImg;
|
||||
|
||||
public Slider probabilitySlider;
|
||||
public float probabilityValue = 0;
|
||||
|
||||
[SerializeField]
|
||||
private float probabilityValue = 0f;
|
||||
|
||||
private bool isLocked = false;
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the level probability panel, setting the level name and probability value.
|
||||
@@ -19,6 +27,7 @@ public class SingleLevelProbabilityPanel : MonoBehaviour
|
||||
{
|
||||
SetLevelName(levelName);
|
||||
SetProbability(probability);
|
||||
InitializeButton();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -28,8 +37,10 @@ public class SingleLevelProbabilityPanel : MonoBehaviour
|
||||
/// <param name="probability">The probability value.</param>
|
||||
public void InitializeLevelProbabilityPanel(int levelName, float probability)
|
||||
{
|
||||
Debug.Log(levelName + " " + probability);
|
||||
SetLevelName(levelName);
|
||||
SetProbability(probability);
|
||||
InitializeButton();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -56,15 +67,107 @@ public class SingleLevelProbabilityPanel : MonoBehaviour
|
||||
/// <param name="prob">The probability value.</param>
|
||||
public void SetProbability(float prob)
|
||||
{
|
||||
probabilitySlider.value = prob;
|
||||
probabilityValue = prob;
|
||||
UpdateProbabilityText();
|
||||
UpdateProbabilitySlider();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update the probability text display.
|
||||
/// Updates the probability text display and can optionally synchronize the probability slider update.
|
||||
/// </summary>
|
||||
public void UpdateProbabilityText()
|
||||
/// <param name="value">The probability value (optional).</param>
|
||||
/// <param name="syncToSlider">Whether to synchronize the probability slider update (default is false).</param>
|
||||
/// <remarks>
|
||||
/// 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.
|
||||
/// </remarks>
|
||||
public void UpdateProbabilityText(float? value = null)
|
||||
{
|
||||
probabilityText.text = (probabilitySlider.value * 100).ToString("0.00") + "%";
|
||||
if (value != null)
|
||||
{
|
||||
probabilityValue = (float)value;
|
||||
}
|
||||
inputField.text = (probabilityValue * 100f).ToString("F1") + "%";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the value of the probability slider and can optionally synchronize the probability text update.
|
||||
/// </summary>
|
||||
/// <param name="value">The probability value (optional).</param>
|
||||
/// <param name="syncToText">Whether to synchronize the probability text update (default is false).</param>
|
||||
/// <remarks>
|
||||
/// 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.
|
||||
/// </remarks>
|
||||
public void UpdateProbabilitySlider(float? value = null)
|
||||
{
|
||||
if (value != null)
|
||||
{
|
||||
probabilityValue = (float)value;
|
||||
}
|
||||
probabilitySlider.value = probabilityValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the current probability value.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This property allows external code to read the current probability value.
|
||||
/// </remarks>
|
||||
public float ProbabilityValue
|
||||
{
|
||||
get { return probabilityValue; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the object is locked.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Returns true if the object is locked; otherwise, returns false.
|
||||
/// </remarks>
|
||||
public bool IsLocked
|
||||
{
|
||||
get { return isLocked; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the object is unlocked.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Returns true if the object is unlocked; otherwise, returns false.
|
||||
/// </remarks>
|
||||
public bool UnLocked
|
||||
{
|
||||
get { return !isLocked; }
|
||||
}
|
||||
|
||||
private void InitializeButton()
|
||||
{
|
||||
EventTrigger eventTrigger = lockButton.GetComponent<EventTrigger>();
|
||||
if (eventTrigger == null)
|
||||
{
|
||||
eventTrigger = lockButton.gameObject.AddComponent<EventTrigger>();
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 650649b0f68385646ba7f21566945d1a
|
||||
guid: 2a8780a678491d04d9395d0243a3dab0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
|
||||
Reference in New Issue
Block a user