Skip to main content
didn't compile
Source Link
Vaillancourt
  • 16.4k
  • 17
  • 56
  • 61

A few notes:

First, FindGameObjectsWithTag only finds active GameObjects. You would need every GameObject you wanted to possibly have enabled be active in the scene when saving it.

Second, that build preprocessor does not run with a specific scene loaded. You perhaps want to use a per-scene processor:

https://docs.unity3d.com/ScriptReference/Build.IProcessSceneWithReport.OnProcessScene.htmlIProcessSceneWithReport.OnProcessScene

This processor runs in play mode in the editor as well, so that should be taken into account (by checking if (report == null)), unless it is desirable to have this behavior in the editor during testing.

Third, this script should go into an Editor folder so you don’t need to include #if UNITY_EDITOR.

So here’s my take (warning: untested):

using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
using UnityEditor;
using System.Linq;

public class PreBuild : IProcessSceneWithReport
{
    public int callbackOrder => 0;

    public void OnProcessScene(UnityEngine.SceneManagement.Scene scene, BuildReport report)
    {
        if (report == null) return;
        foreach (var root in scene.GetRootGameObjects())
        {
            ProcessHierarchy(root.transform, report);
        }
    }

    private void ProcessHierarchy(Transform root, BuildReport report)
    {
        var allTransforms = root.GetComponentsInChildren<Transform>(true);
        var mobileGameObjects = allTransforms.Where(t=>t.CompareTag("Mobile")).Select(t=>t.gameObject);
        BuildTarget bt = report.summary.platform;
        bool isMobile = bt == BuildTarget.Android || bt == BuildTarget.iOS;
        foreach (var mobileGameObject in mobileGameObjects)
        {
            mobileGameObject.SetActive(isMobile);
        }
    }
}

A few notes:

First, FindGameObjectsWithTag only finds active GameObjects. You would need every GameObject you wanted to possibly have enabled be active in the scene when saving it.

Second, that build preprocessor does not run with a specific scene loaded. You perhaps want to use a per-scene processor:

https://docs.unity3d.com/ScriptReference/Build.IProcessSceneWithReport.OnProcessScene.html

This processor runs in play mode in the editor as well, so that should be taken into account (by checking if (report == null)), unless it is desirable to have this behavior in the editor during testing.

Third, this script should go into an Editor folder so you don’t need to include #if UNITY_EDITOR.

So here’s my take (warning: untested):

using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
using UnityEditor;
using System.Linq;

public class PreBuild : IProcessSceneWithReport
{
    public int callbackOrder => 0;

    public void OnProcessScene(UnityEngine.SceneManagement.Scene scene, BuildReport report)
    {
        if (report == null) return;
        foreach (var root in scene.GetRootGameObjects())
        {
            ProcessHierarchy(root.transform, report);
        }
    }

    private void ProcessHierarchy(Transform root, BuildReport report)
    {
        var allTransforms = root.GetComponentsInChildren<Transform>(true);
        var mobileGameObjects = allTransforms.Where(t=>t.CompareTag("Mobile")).Select(t=>t.gameObject);
        BuildTarget bt = report.summary.platform;
        bool isMobile = bt == BuildTarget.Android || bt == BuildTarget.iOS;
        foreach (var mobileGameObject in mobileGameObjects)
        {
            mobileGameObject.SetActive(isMobile);
        }
    }
}

A few notes:

First, FindGameObjectsWithTag only finds active GameObjects. You would need every GameObject you wanted to possibly have enabled be active in the scene when saving it.

Second, that build preprocessor does not run with a specific scene loaded. You perhaps want to use a per-scene processor:

IProcessSceneWithReport.OnProcessScene

This processor runs in play mode in the editor as well, so that should be taken into account (by checking if (report == null)), unless it is desirable to have this behavior in the editor during testing.

Third, this script should go into an Editor folder so you don’t need to include #if UNITY_EDITOR.

So here’s my take (warning: untested):

using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
using UnityEditor;
using System.Linq;

public class PreBuild : IProcessSceneWithReport
{
    public int callbackOrder => 0;

    public void OnProcessScene(UnityEngine.SceneManagement.Scene scene, BuildReport report)
    {
        if (report == null) return;
        foreach (var root in scene.GetRootGameObjects())
        {
            ProcessHierarchy(root.transform, report);
        }
    }

    private void ProcessHierarchy(Transform root, BuildReport report)
    {
        var allTransforms = root.GetComponentsInChildren<Transform>(true);
        var mobileGameObjects = allTransforms.Where(t=>t.CompareTag("Mobile")).Select(t=>t.gameObject);
        BuildTarget bt = report.summary.platform;
        bool isMobile = bt == BuildTarget.Android || bt == BuildTarget.iOS;
        foreach (var mobileGameObject in mobileGameObjects)
        {
            mobileGameObject.SetActive(isMobile);
        }
    }
}

A few notes:

First, FindGameObjectsWithTag only finds active GameObjects. You would need every GameObject you wanted to possibly have enabled be active in the scene when saving it.

Second, that build preprocessor does not run with a specific scene loaded. You perhaps want to use a per-scene processor:

https://docs.unity3d.com/ScriptReference/Build.IProcessSceneWithReport.OnProcessScene.html

This processor runs in play mode in the editor as well, so that should be taken into account (by checking if (report == null)), unless it is desirable to have this behavior in the editor during testing.

Third, this script should go into an Editor folder so you don’t need to include #if UNITY_EDITOR.

So here’s my take (warning: untested):

using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
using UnityEditor;
using System.Linq;

public class PreBuild : IProcessSceneWithReport
{
    public int callbackOrder => 0;

    public void OnProcessScene(UnityEngine.SceneManagement.Scene scene, BuildReport report)
    {
        if (report == null) return;
        foreach (var root in scene.GetRootGameObjects())
        {
            ProcessHierarchy(root.transform, report);
        }
    }

    private void ProcessHierarchy(Transform root, BuildReport report)
    {
        var allTransforms = root.GetComponentsInChildren<Transform>(true);
        var mobileGameObjects = allTransforms.Where(t=>t.CompareTag("Mobile")).Select(t=>t.gameObject);
        BuildTarget bt = report.summary.platform;
        bool isMobile = bt == BuildTarget.Android || bt == BuildTarget.iOS;
        foreach (var mobileGameObject in mobileGameObjects)
        {
            mobileGameObject.SetActive(isMobile);
        }
    }
}

A few notes:

First, FindGameObjectsWithTag only finds active GameObjects. You would need every GameObject you wanted to possibly have enabled be active in the scene when saving it.

Second, that build preprocessor does not run with a specific scene loaded. You perhaps want to use a per-scene processor:

https://docs.unity3d.com/ScriptReference/Build.IProcessSceneWithReport.OnProcessScene.html

This processor runs in play mode in the editor as well, so that should be taken into account (by checking if (report == null)), unless it is desirable to have this behavior in the editor during testing.

Third, this script should go into an Editor folder so you don’t need to include #if UNITY_EDITOR.

So here’s my take (warning: untested):

using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
using UnityEditor;
using System.Linq;

public class PreBuild : IProcessSceneWithReport
{
    public int callbackOrder => 0;

    public void OnProcessScene(UnityEngine.SceneManagement.Scene scene, BuildReport report)
    {
        if (report == null) return;
        foreach (var root in scene.GetRootGameObjects())
        {
            ProcessHierarchy(root, report);
        }
    }

    private void ProcessHierarchy(Transform root, BuildReport report)
    {
        var allTransforms = root.GetComponentsInChildren<Transform>(true);
        var mobileGameObjects = allTransforms.Where(t=>t.CompareTag("Mobile")).Select(t=>t.gameObject);
        BuildTarget bt = report.summary.platform;
        bool isMobile = bt == BuildTarget.Android || bt == BuildTarget.iOS;
        foreach (var mobileGameObject in mobileGameObjects)
        {
            mobileGameObject.SetActive(isMobile);
        }
    }
}

A few notes:

First, FindGameObjectsWithTag only finds active GameObjects. You would need every GameObject you wanted to possibly have enabled be active in the scene when saving it.

Second, that build preprocessor does not run with a specific scene loaded. You perhaps want to use a per-scene processor:

https://docs.unity3d.com/ScriptReference/Build.IProcessSceneWithReport.OnProcessScene.html

This processor runs in play mode in the editor as well, so that should be taken into account (by checking if (report == null)), unless it is desirable to have this behavior in the editor during testing.

Third, this script should go into an Editor folder so you don’t need to include #if UNITY_EDITOR.

So here’s my take (warning: untested):

using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
using UnityEditor;
using System.Linq;

public class PreBuild : IProcessSceneWithReport
{
    public int callbackOrder => 0;

    public void OnProcessScene(UnityEngine.SceneManagement.Scene scene, BuildReport report)
    {
        if (report == null) return;
        foreach (var root in scene.GetRootGameObjects())
        {
            ProcessHierarchy(root.transform, report);
        }
    }

    private void ProcessHierarchy(Transform root, BuildReport report)
    {
        var allTransforms = root.GetComponentsInChildren<Transform>(true);
        var mobileGameObjects = allTransforms.Where(t=>t.CompareTag("Mobile")).Select(t=>t.gameObject);
        BuildTarget bt = report.summary.platform;
        bool isMobile = bt == BuildTarget.Android || bt == BuildTarget.iOS;
        foreach (var mobileGameObject in mobileGameObjects)
        {
            mobileGameObject.SetActive(isMobile);
        }
    }
}
Source Link
Ed Marty
  • 5.3k
  • 1
  • 14
  • 15

A few notes:

First, FindGameObjectsWithTag only finds active GameObjects. You would need every GameObject you wanted to possibly have enabled be active in the scene when saving it.

Second, that build preprocessor does not run with a specific scene loaded. You perhaps want to use a per-scene processor:

https://docs.unity3d.com/ScriptReference/Build.IProcessSceneWithReport.OnProcessScene.html

This processor runs in play mode in the editor as well, so that should be taken into account (by checking if (report == null)), unless it is desirable to have this behavior in the editor during testing.

Third, this script should go into an Editor folder so you don’t need to include #if UNITY_EDITOR.

So here’s my take (warning: untested):

using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
using UnityEditor;
using System.Linq;

public class PreBuild : IProcessSceneWithReport
{
    public int callbackOrder => 0;

    public void OnProcessScene(UnityEngine.SceneManagement.Scene scene, BuildReport report)
    {
        if (report == null) return;
        foreach (var root in scene.GetRootGameObjects())
        {
            ProcessHierarchy(root, report);
        }
    }

    private void ProcessHierarchy(Transform root, BuildReport report)
    {
        var allTransforms = root.GetComponentsInChildren<Transform>(true);
        var mobileGameObjects = allTransforms.Where(t=>t.CompareTag("Mobile")).Select(t=>t.gameObject);
        BuildTarget bt = report.summary.platform;
        bool isMobile = bt == BuildTarget.Android || bt == BuildTarget.iOS;
        foreach (var mobileGameObject in mobileGameObjects)
        {
            mobileGameObject.SetActive(isMobile);
        }
    }
}