Unityのリソース保存パス

環境

Unity 5.1.1f1
xcode7.1
iosバージョン8くらい

プラットフォームによって取得できるパス

パスの取得APIは下記の4種類です。

  • Application.persistentDataPath
  • Application.streamingAssetsPath
  • Application.temporaryCachePath
  • Application.DataPath

Application.persistentDataPath

実行中に保存されるファイルがあるパス

端末 パス
iPhone /var/mobile/Applications/アプリ番号/Documents
Android /data/data/xxx.xxx.xxx/files
Windows C:/Users/xxxx/AppData/LocalLow/CompanyName/ProductName
Mac /Users/xxxx/Library/Caches/CompanyName/Product Name

※ iPhoneの場合Application.persistentDataPathは/Documents/以下に保存します
 ( Documents以下はiCloudへのバックアップ対象となる為、容量が大きいものリジェクト対象になる可能性があるそうです)

Application.streamingAssetsPath

ストリーミングアセットのパス

端末 パス
iPhone /var/mobile/Applications/アプリ番号/hoge.app/Data/Raw
Android jar:file:///data/app/xxx.xxx.xxx.apk/!/assets
Windows /Assets/StreamingAssets
Mac /Assets/StreamingAssets

Application.temporaryCachePath

一時領域のパス

端末 パス
iPhone /var/mobile/Applications/アプリ番号/Library/Caches
Android /data/data/xxx.xxx.xxx/cache
Windows C:/Users/xxxx/AppData/Local/Temp/CompanyName/ProductName
Mac /var/folders/57/6b4_9w8113x2fsmzx_yhrhvh0000gn/T/CompanyName/Product Name

Application.DataPath

Unityが利用するデータが保存されるパス

端末 パス
iPhone /var/mobile/Applications/アプリ番号/hoge.app/Data
Android /data/app/xxx.xxx.xxx.apk
Windows /Assets
Mac /Assets

Application.dataPathはios8から書き込みが禁止されている?

スクショ保存方法その①

Application.CaptureScreenshotはローカルにスクショを保存してくれます。
(※カメラロールには保存されません)

Application.CaptureScreenshot("image.png");

ファイル名だけ指定した場合はApplication.persistentDataPath/ へ保存されますが
リジェクトの可能性があるためアウトです( ˘ω˘)
(画像一枚だけや一時的に保存して削除であれば問題ないかもしれない…)

iOSの場合デフォルトだと /var/mobile/Applications/アプリ番号/Documentsに保存されますが
どうやらパス指定もできるらしいので試してみた

// ① Application.temporaryCachePathを指定して保存
Application.CaptureScreenshot(Application.temporaryCachePath + "/image.png");

// ② Application.dataPathを指定して保存
Application.CaptureScreenshot(Application.dataPath + "/image.png");

// ③ Application.streamingAssetsPathを指定して保存
Application.CaptureScreenshot(Application.streamingAssetsPath + "/image.png");

① 書き出されるが image.pngではない。
/var/mobile/Applications/アプリ番号/Library/Caches/Snapshots/jp.cloverlab.jp/UIApplicationAutomaticSnapshotDefault-LandscapeRight@2x.png

② ③ 生成されない。権限の問題っぽい。

バグ?バージョンの問題?
あきらめました。

スクショ保存方法その②

Fileクラスでオープンしてやればいけました!
パスは Application.temporaryCachePathです

念のため、起動時と一時停止時に画像があれば削除しています。
iPhone・Androidの実機で確認済み

public IEnumerator ScreenShot()
{
    // レンダリングが完了するまで待つ
    yield return new WaitForEndOfFrame();

    // スクリーン全体をtexに読み込む
    var tex = new Texture2D (Screen.width, Screen.height, TextureFormat.RGB24, false);
    tex.ReadPixels (new Rect(0, 0, Screen.width, Screen.height), 0, 0);

    // pngにして保存
    using (FileStream BinaryFile = new FileStream(Application.temporaryCachePath + "image.png", FileMode.Create, FileAccess.Write)) {
        using (BinaryWriter Writer = new BinaryWriter(BinaryFile)) {

            Writer.Write (tex.EncodeToPNG());
        }
    }
}

おわりに

認証まわりをよしなにやってくれるプラグインが無かったので (有料ならあるかも)
認証が必要な場合は自前で書いたほうが早いですね。SNS側の仕様もコロコロ変わるみたいですし。

ではではこの辺で
お疲れ様でした

enter image description here

0 件のコメント :

コメントを投稿