LoginSignup
0
1

More than 5 years have passed since last update.

[Unity] ファイル置き場、各場所からのファイルの入力

Posted at

ファイルの置き場所(Unity PCとスマホ実機の違い)

Resourceフォルダならいいんだけど、Resourceフォルダ外でやる場合はたぶん場所が違うので不都合が起きる。

#if UNITY_EDITOR
    private static string custom_csv_path_ = "C:/default.csv";
#elif UNITY_ANDROID || UNITY_IOS
    private static string custom_csv_path_ = Application.persistentDataPath + "/default.csv";
#endif

ファイルの入力(読み込み)

Resourceフォルダから

string file_name = "default";
TextAsset ta = Resources.Load(file_name, typeof(TextAsset)) as TextAsset;
string read_data = ta.text;

外部フォルダから

        string read_data = "";

        FileInfo file_info = new FileInfo("C:/default.csv");

        try
        {
            using (StreamReader sr = new StreamReader(file_info.OpenRead(), Encoding.UTF8))
            {
                // 最初から最後まで全部読み込む
                read_data = sr.ReadToEnd();
            }
        }
        catch (Exception e)
        {
            //ここにエラーメッセージを書く?
        }
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1