Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2023
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.04 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEditor;
  4. using UnityEditor.PostProcessing;
  5.  
  6. [InitializeOnLoad]
  7. public class SelectionHandler
  8. {
  9.     public static bool IsEnabled = true;        // Flag to check if the script functionality is enabled
  10.     public static bool DebugMessages = true;    // Flag to check if debug messages should be displayed
  11.  
  12.     static SelectionHandler()
  13.     {
  14.         Selection.selectionChanged += OnSelectionChanged;
  15.         Log("SelectionHandler initialized.");
  16.     }
  17.  
  18.     static void OnSelectionChanged()
  19.     {
  20.         if (!IsEnabled)
  21.             return;
  22.  
  23.         Log("Selection changed.");
  24.  
  25.         if (!MouseIsOverAnySceneView())
  26.         {
  27.             Log("Mouse is not over any SceneView.");
  28.             return;
  29.         }
  30.        
  31.         //singleUpdater();
  32.         multipleUpdater();
  33.     }
  34.  
  35.     static void multipleUpdater()
  36.     {
  37.         GameObject[] selectedGameObjects = Selection.gameObjects;
  38.         if (selectedGameObjects == null || selectedGameObjects.Length == 0)
  39.             return;
  40.        
  41.         int index = 0;
  42.  
  43.         bool shouldUpdate = false;
  44.        
  45.         foreach (GameObject selectedObject in selectedGameObjects)
  46.         {
  47.             Transform parentWithLOD = selectedObject.transform;
  48.             GameObject newSelection = null;
  49.             while (parentWithLOD != null)
  50.             {
  51.                 if (parentWithLOD.GetComponent<LODGroup>() != null)
  52.                 {
  53.                     Log("Found parent with LOD. Scheduling a selection change.");
  54.                     newSelection = parentWithLOD.gameObject;
  55.                     break;
  56.                 }
  57.                 parentWithLOD = parentWithLOD.parent;
  58.             }
  59.  
  60.             if (newSelection != null)
  61.             {
  62.                 selectedGameObjects[index] = newSelection;
  63.                 shouldUpdate = true;
  64.             }
  65.             else
  66.             {
  67.                 selectedGameObjects[index] = selectedObject;
  68.             }
  69.  
  70.             index++;
  71.         }
  72.  
  73.         if (shouldUpdate)
  74.         {
  75.             EditorApplication.delayCall += () =>
  76.             {
  77.                 Selection.objects = selectedGameObjects;
  78.                 Log("objects were updated");
  79.             };  
  80.         }
  81.  
  82.     }
  83.  
  84.     static void singleUpdater()
  85.     {
  86.         GameObject selectedObject = Selection.activeGameObject;
  87.  
  88.         if (selectedObject == null)
  89.             return;
  90.  
  91.         Log("Checking for parent with LOD.");
  92.  
  93.         Transform parentWithLOD = selectedObject.transform;
  94.  
  95.         while (parentWithLOD != null)
  96.         {
  97.             if (parentWithLOD.GetComponent<LODGroup>() != null)
  98.             {
  99.                 Log("Found parent with LOD. Scheduling a selection change.");
  100.                 GameObject newSelection = parentWithLOD.gameObject;
  101.                 EditorApplication.delayCall += () =>
  102.                 {
  103.                     Selection.activeGameObject = newSelection;
  104.                     Log("Selection changed to parent with LOD.");
  105.                 };
  106.                 break;
  107.             }
  108.             parentWithLOD = parentWithLOD.parent;
  109.         }
  110.     }
  111.  
  112.     static bool MouseIsOverAnySceneView()
  113.     {
  114.         foreach (SceneView view in SceneView.sceneViews)
  115.         {
  116.             if ((Object)view == SceneView.mouseOverWindow)
  117.                 return true;
  118.         }
  119.         return false;
  120.     }
  121.  
  122.     // Helper function to display logs based on the DebugMessages flag
  123.     static void Log(string message)
  124.     {
  125.         if (DebugMessages)
  126.             Debug.Log(message);
  127.     }
  128.  
  129.     [MenuItem("Tools/Toggle LOD Parent Selection")]
  130.     public static void ToggleFunctionality()
  131.     {
  132.         IsEnabled = !IsEnabled;
  133.         Log("LOD Parent Selection is " + (IsEnabled ? "enabled" : "disabled"));
  134.     }
  135.  
  136.     [MenuItem("Tools/Toggle LOD Debug Messages")]
  137.     public static void ToggleDebugMessages()
  138.     {
  139.         DebugMessages = !DebugMessages;
  140.         Debug.Log("LOD Debug Messages are " + (DebugMessages ? "enabled" : "disabled"));
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement