Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor;
- using UnityEditor.PostProcessing;
- [InitializeOnLoad]
- public class SelectionHandler
- {
- public static bool IsEnabled = true; // Flag to check if the script functionality is enabled
- public static bool DebugMessages = true; // Flag to check if debug messages should be displayed
- static SelectionHandler()
- {
- Selection.selectionChanged += OnSelectionChanged;
- Log("SelectionHandler initialized.");
- }
- static void OnSelectionChanged()
- {
- if (!IsEnabled)
- return;
- Log("Selection changed.");
- if (!MouseIsOverAnySceneView())
- {
- Log("Mouse is not over any SceneView.");
- return;
- }
- //singleUpdater();
- multipleUpdater();
- }
- static void multipleUpdater()
- {
- GameObject[] selectedGameObjects = Selection.gameObjects;
- if (selectedGameObjects == null || selectedGameObjects.Length == 0)
- return;
- int index = 0;
- bool shouldUpdate = false;
- foreach (GameObject selectedObject in selectedGameObjects)
- {
- Transform parentWithLOD = selectedObject.transform;
- GameObject newSelection = null;
- while (parentWithLOD != null)
- {
- if (parentWithLOD.GetComponent<LODGroup>() != null)
- {
- Log("Found parent with LOD. Scheduling a selection change.");
- newSelection = parentWithLOD.gameObject;
- break;
- }
- parentWithLOD = parentWithLOD.parent;
- }
- if (newSelection != null)
- {
- selectedGameObjects[index] = newSelection;
- shouldUpdate = true;
- }
- else
- {
- selectedGameObjects[index] = selectedObject;
- }
- index++;
- }
- if (shouldUpdate)
- {
- EditorApplication.delayCall += () =>
- {
- Selection.objects = selectedGameObjects;
- Log("objects were updated");
- };
- }
- }
- static void singleUpdater()
- {
- GameObject selectedObject = Selection.activeGameObject;
- if (selectedObject == null)
- return;
- Log("Checking for parent with LOD.");
- Transform parentWithLOD = selectedObject.transform;
- while (parentWithLOD != null)
- {
- if (parentWithLOD.GetComponent<LODGroup>() != null)
- {
- Log("Found parent with LOD. Scheduling a selection change.");
- GameObject newSelection = parentWithLOD.gameObject;
- EditorApplication.delayCall += () =>
- {
- Selection.activeGameObject = newSelection;
- Log("Selection changed to parent with LOD.");
- };
- break;
- }
- parentWithLOD = parentWithLOD.parent;
- }
- }
- static bool MouseIsOverAnySceneView()
- {
- foreach (SceneView view in SceneView.sceneViews)
- {
- if ((Object)view == SceneView.mouseOverWindow)
- return true;
- }
- return false;
- }
- // Helper function to display logs based on the DebugMessages flag
- static void Log(string message)
- {
- if (DebugMessages)
- Debug.Log(message);
- }
- [MenuItem("Tools/Toggle LOD Parent Selection")]
- public static void ToggleFunctionality()
- {
- IsEnabled = !IsEnabled;
- Log("LOD Parent Selection is " + (IsEnabled ? "enabled" : "disabled"));
- }
- [MenuItem("Tools/Toggle LOD Debug Messages")]
- public static void ToggleDebugMessages()
- {
- DebugMessages = !DebugMessages;
- Debug.Log("LOD Debug Messages are " + (DebugMessages ? "enabled" : "disabled"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement