Ary*_*Dev 1 c# build unity-game-engine
我是 Unity 的新手。我最近创建了一个脚本,该脚本可以禁用场景中的按钮,直到满足条件(对于关卡选择菜单,禁用所有关卡,直到玩家完成之前的关卡)。但问题是 - 当我在Editor
中运行游戏时,我的脚本工作正常,但是当我构建然后运行游戏时,脚本无法 *正确工作* 。
这是该脚本 -
// Import required Modules
public class ButtonGatherer : MonoBehaviour
{
public List<Button> buttons;
public static int OpenLevels;
void Start()
{ if (OpenLevels == 0){
OpenLevels = int.Parse(File.ReadAllText(Application.persistentDataPath + "Data.gokartgo"));} //Reads Data from a file
GameObject[] btn = GameObject.FindGameObjectsWithTag("button");
for (int i = 0; i < btn.Length; i++){
buttons.Add(btn[i].GetComponent<Button>());
}
Debug.Log("Variable OpenLevels : "+OpenLevels);
for(int i = OpenLevels; i < buttons.Count; i++) {
buttons[i].interactable = false;
Debug.Log("Var i : " + i +", So Locked Level : " + buttons[i]); //Used This To debug and get values for variable i
}
}
}
Run Code Online (Sandbox Code Playgroud)
我还附上了我收到的调试消息的屏幕截图 -
在编辑器中运行时 -
在编辑器中运行时调试跟踪的屏幕截图
构建后运行时(开发构建) -
构建后运行时调试跟踪的屏幕截图
请帮忙 ....
编辑:我搜索了 Unity 文档和论坛,但什么也没找到
你的问题可能出在这一行
OpenLevels = int.Parse(File.ReadAllText(Application.persistentDataPath + "Data.gokartgo"));}
Run Code Online (Sandbox Code Playgroud)
缺少路径分隔符,因此这会导致路径看起来像例如
| This is now a file that doesn't even
| lie within your apps data!
V
\root\applications\yourapp\persistentDataData.gokartgo
Run Code Online (Sandbox Code Playgroud)
在大多数设备上不允许这样的访问!
他们之间需要一个/or 。\
但不要使用,+ "/"见下文!
长话短说:切勿+ ""将或用于+ "/"系统文件路径,因为根据目标平台(例如 Windows \、 Unix /),它们可能不正确。
而是使用Path.Combine它根据您的平台自动插入正确的路径分隔符:
OpenLevels = int.Parse(File.ReadAllText(Path.Combine(Application.persistentDataPath, "Data.gokartgo"));
Run Code Online (Sandbox Code Playgroud)
如果您讨论的问题是您收到的按钮编号,请注意,FindGameObjectsWithTag可能无法保证结果始终以相同的顺序返回。