「Unity/Editor/プレイヤービルド」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→プリプロセッサ定義) |
|||
(同じ利用者による、間の13版が非表示) | |||
行1: | 行1: | ||
− | Assets/Editor/BuildPlayerExample.cs | + | Assets/Scripts/Editor/BuildPlayerExample.cs |
<pre> | <pre> | ||
using UnityEditor; | using UnityEditor; | ||
行6: | 行6: | ||
public class BuildPlayerExample : MonoBehaviour | public class BuildPlayerExample : MonoBehaviour | ||
{ | { | ||
− | [MenuItem(" | + | [MenuItem("Tools/ProjectBuild WebGL")] |
− | public static void | + | public static void ProjectBuild() |
{ | { | ||
BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions(); | BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions(); | ||
行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"); | ||
行27: | 行27: | ||
} | } | ||
</pre> | </pre> | ||
+ | |||
+ | BuildPipelineの公式マニュアル参考:http://docs.unity3d.com/ja/current/ScriptReference/BuildPipeline.BuildPlayer.html | ||
+ | |||
+ | Unityメインメニュー/Build/Build WebGLで実行できる。 | ||
+ | |||
+ | 上記例では、実行すると、WebGLBuildのディレクトリに、ビルドファイルが、生成される | ||
+ | |||
+ | ===BuildSettingsのScenesInBuildに設定したものを設定する方法=== | ||
+ | 上のbuildPlayerOptions.scenesを、下のscenesに置き換える | ||
+ | <pre> | ||
+ | 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(); | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | 参考: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 } }