「Unity/Editor/プレイヤービルド」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→=Editorのシーンを設定) |
(→プリプロセッサ定義) |
||
| (同じ利用者による、間の6版が非表示) | |||
| 行20: | 行20: | ||
Debug.Log("Build succeeded: " + summary.totalSize + " bytes"); | Debug.Log("Build succeeded: " + summary.totalSize + " bytes"); | ||
} | } | ||
| − | if (summary.result == BuildResult.Failed) | + | else if (summary.result == BuildResult.Failed) |
{ | { | ||
Debug.Log("Build failed"); | Debug.Log("Build failed"); | ||
| 行34: | 行34: | ||
上記例では、実行すると、WebGLBuildのディレクトリに、ビルドファイルが、生成される | 上記例では、実行すると、WebGLBuildのディレクトリに、ビルドファイルが、生成される | ||
| − | === | + | ===BuildSettingsのScenesInBuildに設定したものを設定する方法=== |
上のbuildPlayerOptions.scenesを、下のscenesに置き換える | 上のbuildPlayerOptions.scenesを、下のscenesに置き換える | ||
<pre> | <pre> | ||
| + | using System.Collections.Generic; | ||
| + | |||
buildPlayerOptions.scenes = GetScenes(); | buildPlayerOptions.scenes = GetScenes(); | ||
| + | |||
static string[] GetScenes() | static string[] GetScenes() | ||
{ | { | ||
| 行51: | 行54: | ||
参考:https://kido0617.github.io/unity/2020-03-29-build-script/ | 参考:https://kido0617.github.io/unity/2020-03-29-build-script/ | ||
| + | |||
| + | ==macコマンド実行== | ||
| + | /Applications/Unity/Hub/Editor/2021.3.4f1/Unity.app/Contents/MacOS/Unity -quit -batchmode -executeMethod BuildPlayerExample.ProjectBuild | ||
| + | |||
| + | ==プリプロセッサ定義== | ||
| + | Unity 2020.1 以降の記述 | ||
| + | |||
| + | BuildEditor.cs | ||
| + | <pre> | ||
| + | using UnityEngine; | ||
| + | using UnityEditor; | ||
| + | using UnityEditor.Build.Reporting; | ||
| + | |||
| + | public class BuildEditor : MonoBehaviour | ||
| + | { | ||
| + | [MenuItem("Window/Build")] | ||
| + | public static void BuildMain() | ||
| + | { | ||
| + | var buildPlayerOptions = new BuildPlayerOptions | ||
| + | { | ||
| + | extraScriptingDefines = new[] { "CUSTOM_LOG" }, | ||
| + | }; | ||
| + | Build(buildPlayerOptions); | ||
| + | } | ||
| + | public static void Build(BuildPlayerOptions buildPlayerOptions) | ||
| + | { | ||
| + | //BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions(); | ||
| + | buildPlayerOptions.scenes = new[] { "Assets/Scenes/MainScene.unity" }; | ||
| + | buildPlayerOptions.locationPathName = "Build"; // 生成されるビルドのディレクトリ名 | ||
| + | //buildPlayerOptions.target = BuildTarget.WebGL; | ||
| + | buildPlayerOptions.target = BuildTarget.iOS; | ||
| + | buildPlayerOptions.options = BuildOptions.None; | ||
| + | BuildReport report = BuildPipeline.BuildPlayer(buildPlayerOptions); | ||
| + | BuildSummary summary = report.summary; | ||
| + | if (summary.result == BuildResult.Succeeded) | ||
| + | { | ||
| + | Debug.Log("Build succeeded: " + summary.totalSize + " bytes"); | ||
| + | } | ||
| + | else if (summary.result == BuildResult.Failed) | ||
| + | { | ||
| + | Debug.Log("Build failed"); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </pre> | ||
| + | |||
| + | MainScene.cs | ||
| + | <pre> | ||
| + | using UnityEngine; | ||
| + | using UnityEngine.UI; | ||
| + | |||
| + | public class MainScene : MonoBehaviour | ||
| + | { | ||
| + | [SerializeField] Text text; | ||
| + | void Start() | ||
| + | { | ||
| + | #if CUSTOM_LOG | ||
| + | Debug.Log("custom_log"); | ||
| + | text.text = "custom log"; | ||
| + | #else | ||
| + | text.text = "normal"; | ||
| + | #endif | ||
| + | } | ||
| + | } | ||
| + | </pre> | ||
| + | |||
| + | 参考:https://baba-s.hatenablog.com/entry/2020/06/29/090700 | ||
2023年9月10日 (日) 15:25時点における最新版
Assets/Scripts/Editor/BuildPlayerExample.cs
using UnityEditor;
using UnityEngine;
using UnityEditor.Build.Reporting;
public class BuildPlayerExample : MonoBehaviour
{
[MenuItem("Tools/ProjectBuild WebGL")]
public static void ProjectBuild()
{
BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions();
buildPlayerOptions.scenes = new[] { "Assets/Scenes/SampleScene.unity" };
buildPlayerOptions.locationPathName = "WebGLBuild"; // 生成されるビルドのディレクトリ名
buildPlayerOptions.target = BuildTarget.WebGL;
buildPlayerOptions.options = BuildOptions.None;
BuildReport report = BuildPipeline.BuildPlayer(buildPlayerOptions);
BuildSummary summary = report.summary;
if (summary.result == BuildResult.Succeeded)
{
Debug.Log("Build succeeded: " + summary.totalSize + " bytes");
}
else if (summary.result == BuildResult.Failed)
{
Debug.Log("Build failed");
}
}
}
BuildPipelineの公式マニュアル参考:http://docs.unity3d.com/ja/current/ScriptReference/BuildPipeline.BuildPlayer.html
Unityメインメニュー/Build/Build WebGLで実行できる。
上記例では、実行すると、WebGLBuildのディレクトリに、ビルドファイルが、生成される
BuildSettingsのScenesInBuildに設定したものを設定する方法
上のbuildPlayerOptions.scenesを、下のscenesに置き換える
using System.Collections.Generic;
buildPlayerOptions.scenes = GetScenes();
static string[] GetScenes()
{
List<string> sceneList = new List<string>();
EditorBuildSettingsScene[] scenes = EditorBuildSettings.scenes;
foreach (EditorBuildSettingsScene scene in scenes)
{
sceneList.Add(scene.path);
}
return sceneList.ToArray();
}
参考:https://kido0617.github.io/unity/2020-03-29-build-script/
macコマンド実行
/Applications/Unity/Hub/Editor/2021.3.4f1/Unity.app/Contents/MacOS/Unity -quit -batchmode -executeMethod BuildPlayerExample.ProjectBuild
プリプロセッサ定義
Unity 2020.1 以降の記述
BuildEditor.cs
using UnityEngine;
using UnityEditor;
using UnityEditor.Build.Reporting;
public class BuildEditor : MonoBehaviour
{
[MenuItem("Window/Build")]
public static void BuildMain()
{
var buildPlayerOptions = new BuildPlayerOptions
{
extraScriptingDefines = new[] { "CUSTOM_LOG" },
};
Build(buildPlayerOptions);
}
public static void Build(BuildPlayerOptions buildPlayerOptions)
{
//BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions();
buildPlayerOptions.scenes = new[] { "Assets/Scenes/MainScene.unity" };
buildPlayerOptions.locationPathName = "Build"; // 生成されるビルドのディレクトリ名
//buildPlayerOptions.target = BuildTarget.WebGL;
buildPlayerOptions.target = BuildTarget.iOS;
buildPlayerOptions.options = BuildOptions.None;
BuildReport report = BuildPipeline.BuildPlayer(buildPlayerOptions);
BuildSummary summary = report.summary;
if (summary.result == BuildResult.Succeeded)
{
Debug.Log("Build succeeded: " + summary.totalSize + " bytes");
}
else if (summary.result == BuildResult.Failed)
{
Debug.Log("Build failed");
}
}
}
MainScene.cs
using UnityEngine;
using UnityEngine.UI;
public class MainScene : MonoBehaviour
{
[SerializeField] Text text;
void Start()
{
#if CUSTOM_LOG
Debug.Log("custom_log");
text.text = "custom log";
#else
text.text = "normal";
#endif
}
}
