「Unity/AssetBundle/AssetBundle作り方」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→=) |
|||
| (同じ利用者による、間の21版が非表示) | |||
| 行1: | 行1: | ||
| − | == | + | ==スクリプトでのAssetBundleの作り方== |
| − | #適当なファイル( | + | #適当なファイル(例:user.csv)を作る |
| − | #" | + | #"user.csv"のファイルを選択し、inspector下のAssetBundleに"user.csv"を入れる。 |
| − | == | + | ==UnityEditorでビルド実行== |
公式:https://docs.unity3d.com/jp/460/ScriptReference/BuildPipeline.BuildAssetBundle.html | 公式:https://docs.unity3d.com/jp/460/ScriptReference/BuildPipeline.BuildAssetBundle.html | ||
| + | |||
| + | Assets/Scripts/Editor/ExportAssetBundles.cs | ||
| + | <pre> | ||
| + | using System.Collections; | ||
| + | using System.Collections.Generic; | ||
| + | using UnityEngine; | ||
| + | using UnityEditor; | ||
| + | |||
| + | public class ExportAssetBundles | ||
| + | { | ||
| + | [MenuItem("Tools/Build AssetBundle")] | ||
| + | static void ExportResource() | ||
| + | { | ||
| + | BuildPipeline.BuildAssetBundles( | ||
| + | "AssetBundleExport", | ||
| + | BuildAssetBundleOptions.None, | ||
| + | BuildTarget.WebGL | ||
| + | ); | ||
| + | } | ||
| + | } | ||
| + | </pre> | ||
| + | #AssetBundleExportのディレクトリを作成しておく | ||
| + | #Unityメインメニュー/Tools/Build AssetBundleを実行 | ||
| + | #AssetBundleExportの下にファイル(例:user、user.manifest)が出来る | ||
| + | |||
| + | ===doesn't existエラーが出るとき=== | ||
| + | 以下のようなエラーが出るとき | ||
| + | ArgumentException: The output path "AssetBundleExport" doesn't exist | ||
| + | 出力ディレクトリを、予め作っておく。 | ||
| + | |||
| + | ===コマンド実行で、Exceptionを吐く=== | ||
| + | dirが無いとか、かもしれないので確認。 | ||
| + | <pre> | ||
| + | Aborting batchmode due to failure: | ||
| + | executeMethod method ExportAssetBundles. ExportResource threw exception. | ||
| + | </pre> | ||
| + | |||
| + | ===Unrecognized assets cannot エラーが出る場合=== | ||
| + | 以下エラーが出る | ||
| + | Unrecognized assets cannot be included in AssetBundles: "Assets/user". | ||
| + | 追加したファイルに拡張子が、ないかもしれないので、拡張子をつける。userだったら、user.csvとかuser.txtとか | ||
| + | |||
| + | ==Dir内のファイルを削除したいときは以下実行== | ||
| + | FileUtil.DeleteDirFile("AssetBundleExport"); | ||
| + | |||
| + | FileUtil.cs | ||
| + | <pre> | ||
| + | using System.IO; | ||
| + | public class FileUtil | ||
| + | { | ||
| + | public static void DeleteDirFile(string dirPath) | ||
| + | { | ||
| + | if (!Directory.Exists(dirPath)) | ||
| + | { | ||
| + | return; | ||
| + | } | ||
| + | string[] filePaths = Directory.GetFiles(dirPath); | ||
| + | foreach (string filePath in filePaths) | ||
| + | { | ||
| + | File.SetAttributes(filePath, FileAttributes.Normal); | ||
| + | File.Delete(filePath); | ||
| + | } | ||
| + | string[] dirPaths = Directory.GetDirectories(dirPath); | ||
| + | foreach (string path in dirPaths) | ||
| + | { | ||
| + | DeleteDirFile(path); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </pre> | ||
| + | 参考:https://kan-kikuchi.hatenablog.com/entry/DirectoryProcessor | ||
==参考== | ==参考== | ||
https://kan-kikuchi.hatenablog.com/entry/AssetBundle | https://kan-kikuchi.hatenablog.com/entry/AssetBundle | ||
| + | |||
| + | https://orenda.co.jp/blog/1633/ | ||
2021年8月28日 (土) 01:40時点における最新版
目次
スクリプトでのAssetBundleの作り方
- 適当なファイル(例:user.csv)を作る
- "user.csv"のファイルを選択し、inspector下のAssetBundleに"user.csv"を入れる。
UnityEditorでビルド実行
公式:https://docs.unity3d.com/jp/460/ScriptReference/BuildPipeline.BuildAssetBundle.html
Assets/Scripts/Editor/ExportAssetBundles.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class ExportAssetBundles
{
[MenuItem("Tools/Build AssetBundle")]
static void ExportResource()
{
BuildPipeline.BuildAssetBundles(
"AssetBundleExport",
BuildAssetBundleOptions.None,
BuildTarget.WebGL
);
}
}
- AssetBundleExportのディレクトリを作成しておく
- Unityメインメニュー/Tools/Build AssetBundleを実行
- AssetBundleExportの下にファイル(例:user、user.manifest)が出来る
doesn't existエラーが出るとき
以下のようなエラーが出るとき
ArgumentException: The output path "AssetBundleExport" doesn't exist
出力ディレクトリを、予め作っておく。
コマンド実行で、Exceptionを吐く
dirが無いとか、かもしれないので確認。
Aborting batchmode due to failure: executeMethod method ExportAssetBundles. ExportResource threw exception.
Unrecognized assets cannot エラーが出る場合
以下エラーが出る
Unrecognized assets cannot be included in AssetBundles: "Assets/user".
追加したファイルに拡張子が、ないかもしれないので、拡張子をつける。userだったら、user.csvとかuser.txtとか
Dir内のファイルを削除したいときは以下実行
FileUtil.DeleteDirFile("AssetBundleExport");
FileUtil.cs
using System.IO;
public class FileUtil
{
public static void DeleteDirFile(string dirPath)
{
if (!Directory.Exists(dirPath))
{
return;
}
string[] filePaths = Directory.GetFiles(dirPath);
foreach (string filePath in filePaths)
{
File.SetAttributes(filePath, FileAttributes.Normal);
File.Delete(filePath);
}
string[] dirPaths = Directory.GetDirectories(dirPath);
foreach (string path in dirPaths)
{
DeleteDirFile(path);
}
}
}
参考:https://kan-kikuchi.hatenablog.com/entry/DirectoryProcessor
