A system for sort scripts using logs
unknown
csharp
13 hours ago
5.8 kB
30
Indexable
in a month
using System.Collections.Generic; using System.Diagnostics; using System.Runtime.CompilerServices; using Debug = UnityEngine.Debug; using Object = UnityEngine.Object; namespace Needle.Console { /// <summary> /// Class to log only in Unity editor, double clicking console logs produced by this class still open the calling source file) /// NOTE: Implement your own version of this is supported. Just implement a class named "DebugEditor" and any method inside this class starting with "Log" will, when double clicked, open the file of the calling method. Use [Conditional] attributes to control when any of these methods should be included. /// </summary> public static class DebugEditor { // List of script names for which logging is disabled public static List<string> EnabledScripts = new List<string>(); // Helper method to get the calling script's name private static string GetCallingScriptName([CallerFilePath] string filePath = "") { return System.IO.Path.GetFileNameWithoutExtension(filePath); } [Conditional("UNITY_EDITOR")] public static void Log(object message, Object context = null, [CallerFilePath] string filePath = "") { if (EnabledScripts.Contains(GetCallingScriptName(filePath))) Debug.Log(message, context); } [Conditional("UNITY_EDITOR")] public static void LogWarning(object message, Object context = null, [CallerFilePath] string filePath = "") { if (EnabledScripts.Contains(GetCallingScriptName(filePath))) Debug.LogWarning(message, context); } [Conditional("UNITY_EDITOR")] public static void LogError(object message, Object context = null, [CallerFilePath] string filePath = "") { if (EnabledScripts.Contains(GetCallingScriptName(filePath))) Debug.LogError(message, context); } } } //////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////// #if UNITY_EDITOR using Needle.Console; using System.Collections.Generic; using System.IO; using System.Text.RegularExpressions; using UnityEngine; [System.Serializable] public class ScriptsEditor { public string name; public bool isChecked; } public class SortDebugScripts : MonoBehaviour { [SerializeField] List<ScriptsEditor> scripts = new List<ScriptsEditor>(); readonly string className = "DebugEditor"; [ContextMenu("Update references")] private void SearchClassReference() { string scriptsDirectory = Path.Combine(Application.dataPath, "Scripts"); string[] files = Directory.GetFiles(scriptsDirectory, "*.cs", SearchOption.AllDirectories); var pattern = $@"\b{className}\b"; //Search for the class name in all the files var regex = new Regex(pattern); foreach (var file in files) { string content = File.ReadAllText(file); if (regex.IsMatch(content)) { string result = file.Replace(scriptsDirectory, "Scripts"); if (result != "Scripts\\Others\\DebugEditor.cs") { string scriptName = Path.GetFileNameWithoutExtension(result); //Check if the script is already in the list //null is return if .Find() doesn't find the script if (scripts.Find(s => s.name == scriptName) == null) { scripts.Add(new ScriptsEditor { name = scriptName, isChecked = true }); DebugEditor.LogWarning("found reference in " + scriptName); } } } } } private void Start() { InvokeRepeating("SortLogScripts",0,1); } private void SortLogScripts() { DebugEditor.Log("Sorting scripts"); DebugEditor.EnabledScripts.Clear(); // Enable or disable scripts based on the isChecked property foreach (var script in scripts) { if (script.isChecked) { DebugEditor.EnabledScripts.Add(script.name); } } } } #endif //////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////// #if UNITY_EDITOR using UnityEditor; using UnityEngine; [CustomPropertyDrawer(typeof(ScriptsEditor))] public class SortScriptsDrawer : PropertyDrawer { public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { // Commencer une nouvelle ligne horizontale EditorGUI.BeginProperty(position, label, property); // Diviser la ligne en deux parties : une pour le nom et une pour la checkbox Rect nameRect = new Rect(position.x, position.y, position.width * 0.7f, position.height); Rect checkboxRect = new Rect(position.x + position.width * 0.75f, position.y, position.width * 0.25f, position.height); // Afficher le champ de texte pour le nom EditorGUI.PropertyField(nameRect, property.FindPropertyRelative("name"), GUIContent.none); // Afficher la checkbox EditorGUI.PropertyField(checkboxRect, property.FindPropertyRelative("isChecked"), GUIContent.none); // Terminer la ligne horizontale EditorGUI.EndProperty(); } } #endif
Editor is loading...
Leave a Comment