前提・実現したいこと
Unity(C#)で作成したゲームをWebGLでサーバーに公開したいと考えています。
ゲームデータをjson形式で保存しようと試みています。
初回は、ファイルがなければファイルを生成してデータを書き込みたいです。
発生している問題・エラーメッセージ
UnityEditorでは、ファイルの生成・書き込み・読み取り・削除と問題なくできています。
IsolatedStorageException: Could not find a part of the path "/http:/~/JsonFiles/test.json". at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000] in <filename unknown>:0
該当のソースコード
入力されたIDを元にファイルパスを作成して、該当ファイルがなければ生成したい。
問題の箇所は OnRegister() FileStreamのところです。
抜粋
C#
1 //書き込み 2 try 3 { 4 var fileStream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite); 5 var streamWrinter = new StreamWriter(fileStream); 6 streamWrinter.Write(json); 7 streamWrinter.Close(); 8 } 9 catch (Exception e) 10 { 11 AddMessage(e.ToString());//テキストへ書き出し 12 return; 13 }
クラス全体
C#
1using System; 2using System.IO; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class Account : MonoBehaviour 7{ 8 [SerializeField] Text Message; 9 [SerializeField] InputField InputField_ID; 10 [SerializeField] InputField InputField_Pass; 11 string InputID; 12 string InputPass; 13 SaveData saveData; 14 15 public void OnEndEditID() 16 { 17 InputID = InputField_ID.text; 18 } 19 20 public void OnEndEditPass() 21 { 22 InputPass = InputField_Pass.text; 23 } 24 25 public void OnLogin() 26 { 27 if (CheckInput()) 28 { 29 return; 30 } 31 32 33 var path = Application.dataPath + "/JsonFiles/" + InputID + ".json"; 34 var info = new FileInfo(path); 35 36 //fileがなかった時 37 if (!File.Exists(path)) 38 { 39 AddMessage("ユーザー名が違います \n " + path); 40 return; 41 } 42 43 //fileがあった場合 44 var reader = new StreamReader(info.OpenRead()); 45 var jsonR = reader.ReadToEnd(); 46 47 //復号化 48 Encryption encryption = new Encryption(); 49 var str = encryption.DecryptString(jsonR, InputPass, InputID); 50 51 //パスワードが違う場合 52 if (str.IndexOf("Error : System.Security.Cryptography.CryptographicException: Bad PKCS7 padding.", System.StringComparison.Ordinal) == 0) 53 { 54 AddMessage("パスワードが違います"); 55 Debug.Log(str); 56 return; 57 } 58 59 //それ以外でエラーが出た場合 60 if (str.IndexOf("Error : ", System.StringComparison.Ordinal) == 0) 61 { 62 AddMessage(str); 63 return; 64 } 65 66 //パスワードが正しかった場合 67 saveData = JsonUtility.FromJson<SaveData>(str); 68 AddMessage("ログイン成功"); 69 } 70 71 public void OnRegister() 72 { 73 if (CheckInput()) 74 { 75 return; 76 } 77 78 var path = Application.dataPath + "/JsonFiles/" + InputID + ".json"; 79 var info = new FileInfo(path); 80 81 //fileがなかった時 82 if (!File.Exists(path)) 83 { 84 85 //データの割り当て 86 saveData = new SaveData(); 87 saveData.user.UserID = InputID; 88 saveData.user.Password = InputPass; 89 90 //シリアライズ化 91 var jsonW = JsonUtility.ToJson(saveData); 92 93 //暗号化 94 Encryption encryption = new Encryption(); 95 var json = encryption.EncryptString(jsonW, InputPass, InputID); 96 97 98 //書き込み 99 try 100 { 101 var fileStream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite); 102 var streamWrinter = new StreamWriter(fileStream); 103 streamWrinter.Write(json); 104 streamWrinter.Close(); 105 } 106 catch (Exception e) 107 { 108 AddMessage(e.ToString()); 109 return; 110 } 111 112 AddMessage("ご登録ありがとうございます!"); 113 return; 114 } 115 116 //fileがあった場合 117 AddMessage("そのユーザー名は、既に使われれています"); 118 119 } 120 121 public void OnDelAccount() 122 { 123 if (CheckInput()) 124 { 125 return; 126 } 127 128 var path = Application.dataPath + "/JsonFiles/" + InputID + ".json"; 129 var info = new FileInfo(path); 130 131 //fileがなかった時 132 if (!File.Exists(path)) 133 { 134 AddMessage("ユーザー名が違います"); 135 return; 136 } 137 138 //fileがあった場合 139 var reader = new StreamReader(info.OpenRead()); 140 var jsonR = reader.ReadToEnd(); 141 142 //復号化 143 Encryption encryption = new Encryption(); 144 var str = encryption.DecryptString(jsonR, InputPass, InputID); 145 146 //パスワードが違う場合 147 if (str.IndexOf("Error : System.Security.Cryptography.CryptographicException: Bad PKCS7 padding.", System.StringComparison.Ordinal) == 0) 148 { 149 AddMessage("パスワードが違います"); 150 Debug.Log(str); 151 return; 152 } 153 154 //それ以外でエラーが出た場合 155 if (str.IndexOf("Error : ", System.StringComparison.Ordinal) == 0) 156 { 157 AddMessage(str); 158 return; 159 } 160 161 //パスワードが正しかった場合 162 info.Delete(); 163 InputField_ID.text = ""; 164 InputField_Pass.text = ""; 165 AddMessage("アカウント及びゲームデータを削除しました。\n ご利用ありがとうございました。"); 166 } 167 168 bool CheckInput() 169 { 170 if (InputID == null || InputID == "") 171 { 172 AddMessage("ユーザー名を入力してください"); 173 return true; 174 } 175 if (InputPass == null || InputPass == "") 176 { 177 AddMessage("パスワードを入力してください"); 178 return true; 179 } 180 181 return false; 182 } 183 184 void AddMessage(string str) 185 { 186 Message.text = str; 187 } 188} 189
試したこと
ネットで検索したページを参照に、FileStreamの部分を書き換えてはサーバーにあげてを繰り返してみたのですが、いずれもUnityEditorではうまくいってもサーバーで同じエラーが出てしまいました。
補足情報(FW/ツールのバージョンなど)
Mac / Chrome Safari Firefox
Unity 2018.2.1f1 Personal(64bit)
サーバー:共有サーバー(Ablenet)をレンタルしてます。
フォルダは作成しています。FTPのスクショです。
アクセス権もいろいろ変更してみたのですが、ダメでした。
情報が不足しておりましたら申し訳ございません。
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/04 13:19
2019/07/04 13:39
2019/07/04 13:44