带有滑块的文本更新无法正常工作
Text update with sliders not working correctly
我有 10 个附加了文本组件的滑块,它们应该显示滑块值并将值保存到播放器偏好设置。这一切都完美无缺,只是某些文本框在再次播放场景时不会 update/display 它们的文本。一半的文本框使用他们从播放器偏好设置中保存的值填充它们的文本值,另一半 return 空值,即使它们的值被正确保存。
这是我保存值的代码(附在每个滑块上):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SaveSliderValue : MonoBehaviour {
public Slider Slider;
public float valueofslider;
void Start()
{
valueofslider = PlayerPrefs.GetFloat(gameObject.name + "valueofslider");
Slider.value = valueofslider;
}
void Update()
{
valueofslider = Slider.value;
if (Input.GetKeyDown(KeyCode.S))
{
PlayerPrefs.SetFloat(gameObject.name + "valueofslider", valueofslider);
Debug.Log("save");
}
}
}
并显示值(附加到每个文本组件)::
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class showvalue : MonoBehaviour {
Text percentageText;
// Use this for initialization
void Start () {
percentageText = GetComponent<Text>();
Debug.Log(percentageText);
}
// Update is called once per frame
public void textUpdate (float value)
{
if (percentageText != null)
percentageText.text = value.ToString();
else
Debug.Log("Variable percentagetext is not set.");
}
}
和错误:
Variable percentagetext is not set.
UnityEngine.Debug:Log(Object)
showvalue:textUpdate(Single) (at Assets/showvalue.cs:24)
UnityEngine.UI.Slider:set_value(Single)
SaveSliderValue:Start() (at Assets/SaveSliderValue.cs:19)
图片 - 便于理解
如果我删除 debug.log,我会得到一个空引用。
Start
函数的执行顺序不可靠。将 showvalue Start
函数重命名为 Awake
并查看是否有帮助。
基本上发生的事情是:
- 一些
showvalue
实例比它们对应的 SaveSliderValue
更早地执行它们的 Start
函数
- 因此他们正确设置了文本的值
- 有些顺序被破坏了(因为 Start 函数以任意顺序执行)=> 你的错误
Awake 始终在 Start 之前执行 - 充分利用它。
此处显示值脚本的工作代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class showvalue : MonoBehaviour {
Text percentageText;
// Use this for initialization
void Awake () {
percentageText = GetComponent<Text>();
//percentageText = GameObject.Find("ThePlayer").GetComponent<SaveSliderValue>().valueofslider;
}
// Update is called once per frame
public void textUpdate (float value)
{
if (percentageText != null)
percentageText.text = value.ToString();
else
Debug.Log("Variable percentagetext is not set.");
}
}
我有 10 个附加了文本组件的滑块,它们应该显示滑块值并将值保存到播放器偏好设置。这一切都完美无缺,只是某些文本框在再次播放场景时不会 update/display 它们的文本。一半的文本框使用他们从播放器偏好设置中保存的值填充它们的文本值,另一半 return 空值,即使它们的值被正确保存。
这是我保存值的代码(附在每个滑块上):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SaveSliderValue : MonoBehaviour {
public Slider Slider;
public float valueofslider;
void Start()
{
valueofslider = PlayerPrefs.GetFloat(gameObject.name + "valueofslider");
Slider.value = valueofslider;
}
void Update()
{
valueofslider = Slider.value;
if (Input.GetKeyDown(KeyCode.S))
{
PlayerPrefs.SetFloat(gameObject.name + "valueofslider", valueofslider);
Debug.Log("save");
}
}
}
并显示值(附加到每个文本组件)::
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class showvalue : MonoBehaviour {
Text percentageText;
// Use this for initialization
void Start () {
percentageText = GetComponent<Text>();
Debug.Log(percentageText);
}
// Update is called once per frame
public void textUpdate (float value)
{
if (percentageText != null)
percentageText.text = value.ToString();
else
Debug.Log("Variable percentagetext is not set.");
}
}
和错误:
Variable percentagetext is not set.
UnityEngine.Debug:Log(Object)
showvalue:textUpdate(Single) (at Assets/showvalue.cs:24)
UnityEngine.UI.Slider:set_value(Single)
SaveSliderValue:Start() (at Assets/SaveSliderValue.cs:19)
图片 - 便于理解
如果我删除 debug.log,我会得到一个空引用。
Start
函数的执行顺序不可靠。将 showvalue Start
函数重命名为 Awake
并查看是否有帮助。
基本上发生的事情是:
- 一些
showvalue
实例比它们对应的SaveSliderValue
更早地执行它们的 - 因此他们正确设置了文本的值
- 有些顺序被破坏了(因为 Start 函数以任意顺序执行)=> 你的错误
Start
函数
Awake 始终在 Start 之前执行 - 充分利用它。
此处显示值脚本的工作代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class showvalue : MonoBehaviour {
Text percentageText;
// Use this for initialization
void Awake () {
percentageText = GetComponent<Text>();
//percentageText = GameObject.Find("ThePlayer").GetComponent<SaveSliderValue>().valueofslider;
}
// Update is called once per frame
public void textUpdate (float value)
{
if (percentageText != null)
percentageText.text = value.ToString();
else
Debug.Log("Variable percentagetext is not set.");
}
}