C# 文件。不在统一中工作
我正在尝试编写一个脚本,以便保存按钮的文本在该插槽中找到保存文件时发生更改。但是,无论我做什么,我的脚本似乎都找不到该文件。这是我现在拥有的C# 文件。不在统一中工作,c#,unity3d,C#,Unity3d,我正在尝试编写一个脚本,以便保存按钮的文本在该插槽中找到保存文件时发生更改。但是,无论我做什么,我的脚本似乎都找不到该文件。这是我现在拥有的 if (File.Exists ("save1.dat")) { //set button text } else { Debug.Log ("Failure"); } 我试过多种不同的方法我使用Resources.Load对不同的文件、不同的文件扩展名(包括Application.dataPath
if (File.Exists ("save1.dat")) {
//set button text
} else {
Debug.Log ("Failure");
}
我试过多种不同的方法我使用Resources.Load对不同的文件、不同的文件扩展名(包括Application.dataPath)进行了尝试,但没有任何效果。出于某种原因,这些函数似乎无法在我的unity项目中找到任何文件,尽管我可以在unity编辑器和文件资源管理器中清楚地看到它们。发生这种情况的原因有哪些?有没有办法避免这种情况?您要求的文件路径不是有效的文件路径。您需要使用
应用程序.dataPath
作为根目录,并确保在追加文件之前,它以/
结尾。您可能还需要将\
替换为/
(查看我自己的代码)
这是一个大杂烩,但我使用它来确定文件IO的应用程序目录:
public static class Configuration {
public static string currentBaseDirectory = "";
public static string currentDirectory = "";
public static void loadCurrentDirectory ()
{
currentDirectory = Application.dataPath;
currentDirectory = currentDirectory.Replace( @"\", "/" );
bool hasFoundMatch = false;
if ( !currentDirectory.EndsWith( "/" ) )
currentDirectory += "/";
switch (Application.platform) {
case RuntimePlatform.OSXEditor: //<path to project folder>/Assets
case RuntimePlatform.WindowsEditor:
if(currentDirectory.EndsWith("Assets/")) {
currentDirectory = currentDirectory.Substring(0, currentDirectory.LastIndexOf( "Assets/" ) );
currentDirectory += "RuntimeData/";
hasFoundMatch = true;
}
break;
case RuntimePlatform.WindowsPlayer: //<path to executablename_Data folder>
break;
case RuntimePlatform.OSXPlayer: //<path to player app bundle>/Contents
if(currentDirectory.EndsWith(".app/Contents/")) {
currentDirectory = currentDirectory.Substring(0, currentDirectory.LastIndexOf( ".app/Contents/" ) );
currentDirectory += "RuntimeData/";
hasFoundMatch = true;
}
break;
case RuntimePlatform.OSXDashboardPlayer: //<path to the dashboard widget bundle>
case RuntimePlatform.WindowsWebPlayer: //not supported
case RuntimePlatform.OSXWebPlayer:
default:
hasFoundMatch = false;
break;
}
if (!hasFoundMatch) {
currentDirectory = Path.GetFullPath("RuntimeData/");
currentDirectory = currentDirectory.Replace(@"\", "/");
}
if (!Directory.Exists( currentDirectory)) {
for (int i = 0; i < 3; i++)
currentDirectory = currentDirectory.Substring( 0, currentDirectory.LastIndexOf( "/" ) );
currentDirectory += "/RuntimeData/";
}
currentBaseDirectory = currentDirectory.Replace("/RuntimeData", "");
}
}
公共静态类配置{
公共静态字符串currentBaseDirectory=“”;
公共静态字符串currentDirectory=“”;
公共静态无效loadCurrentDirectory()
{
currentDirectory=Application.dataPath;
currentDirectory=currentDirectory.Replace(@“\”,“/”);
bool hasFoundMatch=false;
如果(!currentDirectory.EndsWith(“/”)
currentDirectory+=“/”;
交换机(应用程序平台){
case RuntimePlatform.OSXEditor:///资产
案例RuntimePlatform.WindowsEditor:
if(currentDirectory.EndsWith(“Assets/”){
currentDirectory=currentDirectory.Substring(0,currentDirectory.LastIndexOf(“资产/”);
currentDirectory+=“RuntimeData/”;
hasFoundMatch=true;
}
打破
案例RuntimePlatform.WindowsPlayer://
打破
case RuntimePlatform.OSXPlayer:///内容
if(currentDirectory.EndsWith(“.app/Contents/”){
currentDirectory=currentDirectory.Substring(0,currentDirectory.LastIndexOf(“.app/Contents/”);
currentDirectory+=“RuntimeData/”;
hasFoundMatch=true;
}
打破
case RuntimePlatform.OSXDashboardPlayer://
案例RuntimePlatform.WindowsWebPlayer://不受支持
案例RuntimePlatform.OSXWebPlayer:
违约:
hasFoundMatch=false;
打破
}
如果(!hasFoundMatch){
currentDirectory=Path.GetFullPath(“RuntimeData/”);
currentDirectory=currentDirectory.Replace(@“\”,“/”);
}
如果(!Directory.Exists(currentDirectory)){
对于(int i=0;i<3;i++)
currentDirectory=currentDirectory.Substring(0,currentDirectory.LastIndexOf(“/”);
currentDirectory+=“/RuntimeData/”;
}
currentBaseDirectory=currentDirectory.Replace(“/RuntimeData”,”);
}
}
这允许我在资产旁边有一个RuntimeData目录,我可以将文件保存在其中。该文件夹在发布时随可执行文件一起发布(尽管您可能需要一个干净的副本,没有任何dev测试保存;)
通过将字符串与
System.IO.Path.Combine(string,string)
组合,而不是使用+=
,您可以在将来通过所有“它有/吗,我应该使用/还是\”以及所有这些来为自己省去很多麻烦,Path.Combine
函数为您完成了所有这些工作。谢谢@ScottChamberlain我从别人几年前编写的代码开始,为了满足我自己的需要而修改了一些代码。。。我只知道我所做的一切。:)