facebook twitter hatena line email

「Unity/Editor/プレイヤービルド」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(macコマンド実行)
(プリプロセッサ定義)
行60: 行60:
 
==プリプロセッサ定義==
 
==プリプロセッサ定義==
 
Unity 2020.1 以降の記述
 
Unity 2020.1 以降の記述
 +
 +
BuildEditor.cs
 
<pre>
 
<pre>
 
using UnityEngine;
 
using UnityEngine;
行79: 行81:
 
     {
 
     {
 
         //BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions();
 
         //BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions();
         buildPlayerOptions.scenes = new[] { "Assets/Scenes/BuildScene.unity" };
+
         buildPlayerOptions.scenes = new[] { "Assets/Scenes/MainScene.unity" };
 
         buildPlayerOptions.locationPathName = "Build"; // 生成されるビルドのディレクトリ名
 
         buildPlayerOptions.locationPathName = "Build"; // 生成されるビルドのディレクトリ名
 
         //buildPlayerOptions.target = BuildTarget.WebGL;
 
         //buildPlayerOptions.target = BuildTarget.WebGL;
行97: 行99:
 
}
 
}
 
</pre>
 
</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
 
参考: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
    }
}

参考:https://baba-s.hatenablog.com/entry/2020/06/29/090700