Skip to content

Instantly share code, notes, and snippets.

@MaZyGer
Last active July 4, 2024 11:41
Show Gist options
  • Save MaZyGer/ea3f32f0d4eebcd66aa3d6f08b5c20aa to your computer and use it in GitHub Desktop.
Save MaZyGer/ea3f32f0d4eebcd66aa3d6f08b5c20aa to your computer and use it in GitHub Desktop.
The default EditorOnly of Unity Engine will only remove objects from standalone player and not in the editor. EditorModeOnly will remove also objects if you start playmode in the editor. Its really nice for things such as UI List tests, to remove the list in play mode. You can remove or change the namespace if you want. Drop it in an Editor fold…
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;
namespace Maz.Unity.EditorExtensions
{
[InitializeOnLoad]
public class EditorModeOnly
{
public static readonly string EditorModeOnlyTag = "EditorModeOnly";
public static readonly string DeactivateOnStartTag = "DeactivateInPlaymode";
static EditorModeOnly()
{
UnityEngine.Object[] asset = AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset");
if ((asset != null) && (asset.Length > 0))
{
SerializedObject so = new SerializedObject(asset[0]);
SerializedProperty tags = so.FindProperty("tags");
bool hasEditorModeOnlyTag = false;
bool hasDeactivateOnStartTag = false;
for (int i = 0; i < tags.arraySize; ++i)
{
SerializedProperty element = tags.GetArrayElementAtIndex(i);
if (element.stringValue == EditorModeOnlyTag)
{
hasEditorModeOnlyTag = true;
}
if (element.stringValue == DeactivateOnStartTag)
{
hasDeactivateOnStartTag = true;
}
}
if(!hasEditorModeOnlyTag)
{
var nextIndex = tags.arraySize;
tags.InsertArrayElementAtIndex(nextIndex);
tags.GetArrayElementAtIndex(nextIndex).stringValue = EditorModeOnlyTag;
so.ApplyModifiedProperties();
so.Update();
}
if (!hasDeactivateOnStartTag)
{
var nextIndex = tags.arraySize;
tags.InsertArrayElementAtIndex(nextIndex);
tags.GetArrayElementAtIndex(nextIndex).stringValue = DeactivateOnStartTag;
so.ApplyModifiedProperties();
so.Update();
}
}
}
static GameObject[] FindGameObjectsByTag(string tag)
{
List<GameObject> validTransforms = new List<GameObject>();
Transform[] objs = Resources.FindObjectsOfTypeAll<Transform>();
for (int i = 0; i < objs.Length; i++)
{
var t = objs[i];
if (t.hideFlags == HideFlags.None && t.gameObject.CompareTag(tag))
{
validTransforms.Add(t.gameObject);
}
}
return validTransforms.ToArray();
}
[PostProcessScene(0)]
public static void OnPostprocessScene()
{
var editorModeTaggedObjects = FindGameObjectsByTag(EditorModeOnlyTag);
foreach (GameObject go in editorModeTaggedObjects)
{
// Filter out gameobjects in resources folder if we are just entering playmode and not building the game
if (!go.scene.IsValid())
continue;
Object.DestroyImmediate(go, false);
}
var DeactivateOnStartTaggedObjects = FindGameObjectsByTag(DeactivateOnStartTag);
foreach (GameObject go in DeactivateOnStartTaggedObjects)
{
// Deactivate only objects in scene
if (!go.scene.IsValid())
continue;
go.SetActive(false);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment