2017-10-01 7 views
1

私は、そのスロットに保存ファイルが見つかった場合、保存ボタンのテキストが変更されるようにスクリプトを作成しようとしています。しかし、私のスクリプトは、私が何をしていてもファイルを見つけることができません。ここに私が今持っているものがあります。File.Existsがユニティで動作しません

if (File.Exists ("save1.dat")) { 
     //set button text 
    } else { 
     Debug.Log ("Failure"); 
    } 

これについて複数のバリエーションを試しました。 私はResources.Loadを使用して、異なるファイル、異なるファイル拡張子、Application.dataPathなどを試しましたが、何も動作しません。何らかの理由で、これらの機能は、ユニティエディタとファイルエクスプローラの両方で明確に表示されているにもかかわらず、というファイルが私のユニティプロジェクトにあるとは限りません。これが起こっている理由は何ですか?これを回避する方法はありますか?

答えて

1

あなたが求めているファイルパスは、有効なファイルパスではありません。 Application.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", ""); 
    } 
} 

これは私は次の私は、ファイルの保存のようなものを置くことができる資産への可能RuntimeDataディレクトリを持つことができますこのフォルダには、公開時に実行可能ファイルが添付されています(ただし、クリーンコピーが必要な場合もありますが、開発者のテスト保存は無料です)。あなたは `System.IOで文字列を組み合わせることにより、すべての「それは/を持っていない、私は/または\使用する必要があります」と、将来的にあなたの自己トラブルの多くを保存し、すべてのことができ

RuntimeData directory location

+2

.Path.Combine(string、string) '' + = 'を使用する代わりに、' Path.Combine'関数はあなたのために働きます。 –

+0

ありがとう@ScottChamberlain私は、何年か前に他の人が書いたコードで始めました。自分のニーズに合うようにいくつか手書きしました。私が知っていたのは、私が働いていたものでした。 :) – Draco18s

関連する問題