Last active
April 14, 2024 13:25
-
-
Save atori708/d6bccd763ba27207a168bfa200aef378 to your computer and use it in GitHub Desktop.
Gizmoの表示状態をスクリプトから制御する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using UnityEditor; | |
using UnityEngine; | |
using UnityEngine.SceneManagement; | |
public class GizmoUtilitySample : MonoBehaviour | |
{ | |
GameObject[] gameObjects; | |
List<GizmoInfo> gizmoInfos = new List<GizmoInfo>(); | |
void Start() | |
{ | |
gameObjects = SceneManager.GetActiveScene().GetRootGameObjects(); | |
foreach (GameObject go in gameObjects) | |
{ | |
foreach (var component in go.GetComponents<Component>()) | |
{ | |
if (GizmoUtility.TryGetGizmoInfo(component.GetType(), out GizmoInfo info)) | |
{ | |
gizmoInfos.Add(info); | |
} | |
} | |
} | |
} | |
public void EnableAllGizmo() | |
{ | |
foreach (GizmoInfo info in gizmoInfos) | |
{ | |
info.gizmoEnabled = true; | |
GizmoUtility.ApplyGizmoInfo(info); | |
} | |
} | |
public void DisableAllGizmo() | |
{ | |
foreach (GizmoInfo info in gizmoInfos) | |
{ | |
info.gizmoEnabled = false; | |
GizmoUtility.ApplyGizmoInfo(info); | |
} | |
} | |
public void DisableLightAndCamera() | |
{ | |
foreach (GizmoInfo info in gizmoInfos) | |
{ | |
if (info.name == nameof(Camera) || info.name == nameof(Light)) | |
{ | |
info.gizmoEnabled = false; | |
GizmoUtility.ApplyGizmoInfo(info); | |
} | |
} | |
} | |
} | |
[CustomEditor(typeof(GizmoUtilitySample))] | |
public class GizmoUtiltySampleEditor : Editor | |
{ | |
public override void OnInspectorGUI() | |
{ | |
base.OnInspectorGUI(); | |
var gizmoUtilitySample = (GizmoUtilitySample)target; | |
if(GUILayout.Button("Enable All Gizmos")) | |
{ | |
gizmoUtilitySample.EnableAllGizmo(); | |
} | |
if(GUILayout.Button("Disable All Gizmos")) | |
{ | |
gizmoUtilitySample.DisableAllGizmo(); | |
} | |
if (GUILayout.Button("Disable Light And Camera Gizmos")) | |
{ | |
gizmoUtilitySample.DisableLightAndCamera(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment