因此,我正在为蛇游戏使用定制器,并且正在使用PlayerPrefs保存蛇的偏好外观。现在,每次我试运行游戏时,playerprefs中保存的字符串不会加载,只是空的。
我已经尝试在Start(),Awake()和Update()方法上获取字符串数据,但它们均未加载数据。另外,我在保存数据的每一行中都添加了PlayerPrefs.Save()。
这是我的代码,用于将所有网格更新为与保存的字符串相同。我已将其放入“更新方法”中。
import './pages/landing.dart';
void main() {
runApp(LandingHome(),
);
}
每次运行此代码时,我都会收到错误消息,并且不将字符串加载或保存在playerprefs中。
答案 0 :(得分:0)
找不到游戏对象“ leMesh”。第一步是确保找到它。因此,请确保我们已获悉其状态以进行调试:
if (PlayerPrefs.HasKey("meshName"))
{
Debug.Log("PlayerPrefs does have the key we are looking for and it is " + PlayerPrefs.GetString("meshName"));
/// This is redundant you just set objName to the string earlier
/// objName = PlayerPrefs.GetString("meshName");
if (objName == PlayerPrefs.GetString("meshName"))
{
leMesh = Resources.Load<GameObject>(@"Models\" + objName);
}
else
{
/// And here you do it again, constantly trying to get that string
/// In order to find the object, of course if the string doesnt exist
/// youll simply get an unassigned referenceException on this line:
/// change.GetComponent<MeshFilter>().mesh = leMesh.GetComponent<MeshFilter>().sharedMesh
objName = PlayerPrefs.GetString("meshName");
leMesh = Resources.Load<GameObject>(@"Models\" + objName);
}
}
现在,如果您的控制台日志告诉您meshName返回了一个值,那么您访问的资源是错误的,在这种情况下,请尝试从“模型”的前面删除“ @”。否则就没有名为meshName的playerPrefs字符串。
答案 1 :(得分:0)
这里有一些问题,您的空引用错误是主要原因。
我将注释您的代码以向您显示错误的地方,并提供替代解决方案。
// If possible it would be better to assign this object in the inspect rather then
// using a GameObject.Find method, Also this is error prone due to the timing of
// the call, for example if this is in a start this Object may not exist yet.
// Another solution would to be create an event and when you load the skin
// Have that event fire off that its been loaded and everything that needs
// to listen to it will get the change.
lesnek = GameObject.FindGameObjectsWithTag("LeWorm");
// GetString has an alternative method where you can pass a default value
// I would recommend using this method instead.
objName = PlayerPrefs.GetString("meshName"); // POINT A
// Honestly this check seems a tad bit redundant since you already
// attempted to load this preference.
// POINT B
if (PlayerPrefs.HasKey("meshName")) // Since you have said this is empty playerPrefs is empty.
{
// At this point it should equal this since you already set it to this in Point A
if (objName == PlayerPrefs.GetString("meshName"))
{
leMesh = Resources.Load<GameObject>(@"Models\" + objName);
}
else
{
// This case should never get called, because of Point A and your if condition.
objName = PlayerPrefs.GetString("meshName");
leMesh = Resources.Load<GameObject>(@"Models\" + objName);
}
}
if (objName != null || objName != "") // This completely nullifies all the work in Point B
{
objName = "Sphere";
leMesh = Resources.Load<GameObject>(@"Models\" + objName);
}
foreach (GameObject change in lesnek)
{
if (objName != null || objName != "")
{
if (change.GetComponent<MeshFilter>() == null)
{
change.AddComponent<MeshFilter>();
change.GetComponent<MeshFilter>().mesh = leMesh.GetComponent<MeshFilter>().sharedMesh;
PlayerPrefs.SetString("meshName", objName);
PlayerPrefs.Save();
}
else
{
change.GetComponent<MeshFilter>().mesh = leMesh.GetComponent<MeshFilter>().sharedMesh;
PlayerPrefs.SetString("meshName", objName);
PlayerPrefs.Save();
}
}
}
在不做过多说明的情况下,假设这是以Initialize方法的形式,而不是从头开始,这就是我要进行的更改
// This would be better as a class variable rather then a local variable
string meshName = PlayerPrefs.GetString("meshName", "Sphere");
// Skipping point B because it is redundant
leMesh = Resources.Load<GameObject>(@"Models\" + meshName); // Is this a GameObject or is it a Mesh???
MeshFilter leMeshFilter = leMesh.GetComponent<MeshFilter>();
if(leMeshFilter != null)
{
foreach (GameObject change in lesnek)
{
MeshFilter meshFilter = change.GetComponent<MeshFilter>();
if(meshFilter != null)
{
meshFilter.mesh = leMeshFilter.sharedMesh;
}
else
{
meshFilter = change.AddComponent<MeshFilter>();
meshFilter.mesh = leMeshFilter.sharedMesh;
}
}
}
此示例唯一不做的就是保存您的PlayerPreference。仅当您更换皮肤时才应这样做。因此,无论您做什么,都可以保存首选项。