Unity 接收参数方法:变量、PlayerPrefs 和 运行时选项
您可以使用以下方法接收参数:
-
使用变量存储参数
在脚本中使用变量来存储参数值,例如:
public class MyScript : MonoBehaviour { public int myParam; void Start() { Debug.Log('My param value is: ' + myParam); } }在启动场景时,将参数值传递给脚本:
// 在启动场景时将参数值传递给脚本 MyScript script = FindObjectOfType<MyScript>(); script.myParam = myParamValue; -
使用 PlayerPrefs 存储参数
在脚本中读取 PlayerPrefs 中存储的参数值:
public class MyScript : MonoBehaviour { void Start() { int myParam = PlayerPrefs.GetInt('myParam'); Debug.Log('My param value is: ' + myParam); } }在启动场景时,将参数值存储到 PlayerPrefs 中:
// 在启动场景时将参数值存储到 PlayerPrefs 中 PlayerPrefs.SetInt('myParam', myParamValue); -
运行时接收参数
如果您需要在运行时接收参数,请考虑使用命令行参数或网络消息。
请注意,使用变量和 PlayerPrefs 的方法都需要在启动场景中进行设置。
原文地址: https://www.cveoy.top/t/topic/nDAI 著作权归作者所有。请勿转载和采集!