前提
ツイッターで映像をツイートする機能を作っています。
カメラで映っている映像をResourceフォルダに保存したいと思います。
(その後別のスクリプトで呼び出します)
エディタ上ではうまくいのですが、Android上でパスが違うと表示され なぜかうまくいきません。
//
Error Unity DirectoryNotFoundException: Could not find a part of the path "/data/app/unity-------
//
実現したいこと
Androidの実機でも画像が保存出来るようになりたい。
発生している問題・エラーメッセージ
2022/08/09 14:03:39.596 31253 31273 Error Unity DirectoryNotFoundException: Could not find a part of the path "/data/app/unity.●●●.▼▼▼/base.apk/Resources/twpng.png". 2022/08/09 14:03:39.596 31253 31273 Error Unity at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x00000] in <00000000000000000000000000000000>:0 2022/08/09 14:03:39.596 31253 31273 Error Unity at System.IO.File.Create (System.String path, System.Int32 bufferSize) [0x00000] in <00000000000000000000000000000000>:0 2022/08/09 14:03:39.596 31253 31273 Error Unity at System.IO.File.WriteAllBytes (System.String path, System.Byte[] bytes) [0x00000] in <00000000000000000000000000000000>:0 2022/08/09 14:03:39.596 31253 31273 Error Unity at Catch_画像_SC.Capture2 () [0x00000] in <00000000000000000000000000000000>:0 2022/08/09 14:03:39.596 31253 31273 Error Unity 2022/08/09 14:03:39.596 31253 31273 Error Unity (Filename: currently not available on il2cpp Line: -1)
該当のソースコード
c#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using System.IO; 5using UnityEditor; 6using UnityEngine.UI; 7 8public class Catch_画像_SC : MonoBehaviour 9{ 10 public Camera sub_camera; 11 12 //サブカメラの「ビューポート矩形」 0:0のまま 「クリアフラッグ」 深度のみにする 13 int カメラの外枠_横 = 1920; 14 int カメラの外枠_たて = 1080; 15 16 void Start() 17 { 18 Capture1_保存(); 19 } 20 public void Capture1_保存() 21 { 22 Invoke("Capture2", 0.0001f);//0.0001f 秒後に次の問題表示 23 } 24 public void Capture2() 25 { 26 27 var size = new Vector2Int(カメラの外枠_横, カメラの外枠_たて); //*サイズを設定 28 var render = new RenderTexture(size.x, size.y, 24); 29 30 var texture = new Texture2D(size.x, size.y, TextureFormat.RGB24, false); 31 32 33 try 34 { 35 36 sub_camera.targetTexture = render; // カメラ画像を RenderTexture に描画 37 sub_camera.Render(); 38 39 40 RenderTexture.active = render; // RenderTexture の画像を読み取る 41 texture.ReadPixels(new Rect(0, 0, size.x * 1f, size.y * 1f), 0, 0); 42 texture.Apply(); 43 } 44 finally 45 { 46 sub_camera.targetTexture = null; 47 RenderTexture.active = null; 48 } 49 50 // PNG 画像としてファイル保存 51 File.WriteAllBytes( 52 $"{Application.dataPath}/Resources/twpng.png", texture.EncodeToPNG()); 53 } 54}
試したこと
「.png」をつけたり外したり日本語スクリプトを変更してみたりしましたがうまくいかなかったです。
そもそもRecouceフォルダにAndroid上から保存できるかも疑問です。
またRecouceフォルダに保存出来ないならゲームの記録などの画像はどうやってツイート出来るのだろうなど考えておりました。
どのようなアプローチで進めれば良いかわからず困っております。
ご存知の方何卒ヒントなど頂けませんでしょうか?
補足情報(FW/ツールのバージョンなど)
Unity 2019.4.31f1
Windows
Windows10
プラットフォーム: Android
プラットフォーム: Android7.1
最終的な解決
c#
1 // PNG 画像としてファイル保存 2 // File.WriteAllBytes( 3 // $"{Application.dataPath}/Resources/twpng.png", texture.EncodeToPNG()); 4 5 File.WriteAllBytes( 6 $"{Application.persistentDataPath}/twpng.png", texture.EncodeToPNG());
最終的な解決の確認
下記コードでAndroid上でツイートができたのでこれでスマホに保存されていた事が証明されました。
Android上での保存と呼び出しの両方が初めてだったので完了の確認が遅くなり申し訳ありませんでした。
下記は保存ではなく画像を呼び出してツイートを行っています。
皆さんの回答それぞれについていろいろ試させて頂きました。
本当にありがとうございます。
c#
1//Unity Asset 「VoxelBusters.EssentialKitを使用」 2 3 4 public void Twitter_Go() 5 { 6 Debug.Log("=== Twi ツイート開始"); 7 8#if UNITY_ANDROID 9 10 string path = $"{Application.persistentDataPath}/twpng.png"; 11 byte[] bytes = File.ReadAllBytes(path); 12 Texture2D texture = new Texture2D(2, 2); 13 texture.LoadImage(bytes); 14 15 16#elif UNITY_IOS 17 18#else 19 Texture2D texture = Resources.Load<Texture2D>("twpng"); // エディター用 「 .png 」をつけるとエラー 20#endif 21 22 ShareSheet shareSheet = ShareSheet.CreateInstance(); 23 shareSheet.AddImage(texture); //画像追加 24 shareSheet.AddText("テスト"); //文章 25 shareSheet.AddURL(URLString.URLWithPath("https://www.google.co.jp/")); //URL 26 shareSheet.SetCompletionCallback((result, error) => 27 { 28 Debug.Log("Share Sheet was closed. Result code: " + result.ResultCode); 29 }); 30 shareSheet.Show(); 31 32 }

回答3件
あなたの回答
tips
プレビュー