Files
2026-03-04 09:37:33 +08:00

364 lines
9.1 KiB
C#

using System;
using System.Globalization;
using System.Reflection;
using UnityEngine;
namespace DebuggingEssentials
{
public static class Helper
{
public static Color colCommandResultFailed = new Color(1f, 0.549f, 0f);
public static Color colCommandResult = new Color(0.25f, 0.6f, 1f);
private static Vector2 lastMousePos;
private static GUIContent guiContent = new GUIContent();
private static MethodInfo findObjectFromInstanceIdMethod = null;
public static string GetConsoleLogPath()
{
string text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture).Replace(' ', '_').Replace(':', '-');
return Application.persistentDataPath + "/" + Application.productName + "-" + Application.version + "_(" + text + ").html";
}
public static string GetApplicationInfo()
{
return Application.productName + " " + Application.version + " (" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture) + ")";
}
public static GUIContent GetGUIContent(string text, Texture image = null, string tooltip = "")
{
guiContent.text = text;
guiContent.image = image;
guiContent.tooltip = tooltip;
return guiContent;
}
public static GUIContent GetGUIContent(Texture image = null, string tooltip = "")
{
guiContent.text = string.Empty;
guiContent.image = image;
guiContent.tooltip = tooltip;
return guiContent;
}
public static GUIContent GetGUIContent(string text, string tooltip)
{
guiContent.text = text;
guiContent.image = null;
guiContent.tooltip = tooltip;
return guiContent;
}
public static GUIContent GetGUIContent(Texture image)
{
GUIContent gUIContent = guiContent;
string text = (guiContent.tooltip = string.Empty);
gUIContent.text = text;
guiContent.image = image;
return guiContent;
}
public static bool IsShowKeyPressed(Event currentEvent, bool useSameEditorAsBuildShowKey, AdvancedKey showToggleKeyEditor, AdvancedKey showToggleKeyBuild)
{
if (useSameEditorAsBuildShowKey || Application.isEditor)
{
return showToggleKeyEditor.GetKeyUp(currentEvent);
}
return showToggleKeyBuild.GetKeyUp(currentEvent);
}
public static bool IsOnlyShowKeyPressed(Event currentEvent, bool useSameEditorAsBuildShowKey, AdvancedKey showToggleKeyEditor, AdvancedKey showToggleKeyBuild)
{
if (useSameEditorAsBuildShowKey || Application.isEditor)
{
return showToggleKeyEditor.OnlyGetKey(currentEvent);
}
return showToggleKeyBuild.OnlyGetKey(currentEvent);
}
public static float FastFrac(float v)
{
return (v - (float)(int)v) % 1f;
}
public static int Repeat(int v, int count)
{
if (count == 0)
{
return 0;
}
v %= count;
if (v < 0)
{
v = count + v;
}
return v;
}
public static string ToTimeFormat(float time)
{
string text = (FastFrac(time) * 100f).ToString("00");
string text2 = ((int)time % 60).ToString("00");
string text3 = ((int)(time / 60f) % 60).ToString("00");
string text4 = ((int)time / 60 / 60).ToString("00");
return text4 + ":" + text3 + ":" + text2 + "." + text;
}
public static int CalcStringLines(string text)
{
int num = 1;
for (int i = 0; i < text.Length; i++)
{
if (text[i] == '\n')
{
num++;
}
}
return num;
}
public static Color AnimateColor(Color col, float speed = 1f)
{
return Color.Lerp(Color.white, col, Mathf.Abs(Mathf.Sin(Time.time * speed)));
}
public static void DrawWindow(int windowId, WindowSettings window, GUI.WindowFunction func)
{
Rect rect = window.rect;
if (window.isMinimized)
{
rect.height = 30f;
}
GUILayout.Window(windowId, rect, func, GUIContent.none);
}
public static bool Drag(WindowSettings window, WindowSettings inspectorWindow, WindowSettings hierarchyWindow, bool titleDrag, bool onlyRightCorner = false)
{
Event current = Event.current;
if (current.type == EventType.Layout)
{
return false;
}
Rect rect = default(Rect);
if (titleDrag)
{
rect = GUILayoutUtility.GetLastRect();
rect.position += window.rect.position;
}
Vector2 mousePos = EventInput.mousePos;
if (current.type == EventType.MouseDown && !current.shift && current.button == 0)
{
if (titleDrag && rect.Contains(mousePos))
{
window.drag = 1;
}
else if (!titleDrag && window.rect.Contains(mousePos))
{
if (onlyRightCorner)
{
if (mousePos.x > window.rect.xMax - 15f && mousePos.y > window.rect.yMax - 15f)
{
window.drag |= 10;
}
}
else
{
if (mousePos.x > window.rect.xMax - 5f)
{
window.drag |= 2;
}
else if (mousePos.x < window.rect.xMin + 7.5f && !window.isDocked.Value)
{
window.drag |= 4;
}
if (mousePos.y > window.rect.yMax - 7.5f)
{
window.drag |= 8;
}
else if (mousePos.y < window.rect.yMin + 7.5f)
{
window.drag |= 16;
}
}
}
lastMousePos = mousePos;
}
if (titleDrag)
{
return false;
}
bool result = false;
if (window.drag != 0)
{
Vector2 vector = mousePos - lastMousePos;
lastMousePos = mousePos;
if (window.drag == 1)
{
window.position += new Vector2(vector.x / (float)Screen.width, vector.y / (float)Screen.height);
}
else
{
if ((window.drag & 2) != 0)
{
window.rect.xMax += vector.x * (float)((!onlyRightCorner || !window.isDocked.Value) ? 1 : 2);
if (vector.x != 0f)
{
result = true;
}
}
if ((window.drag & 4) != 0)
{
window.rect.xMin += vector.x;
window.position.x += vector.x / (float)Screen.width;
if (vector.x != 0f)
{
result = true;
}
}
if ((window.drag & 8) != 0)
{
if (inspectorWindow != null && inspectorWindow.isDocked.Value)
{
window = hierarchyWindow;
}
window.rect.yMax += vector.y;
if (vector.y != 0f)
{
result = true;
}
}
if ((window.drag & 0x10) != 0)
{
if (inspectorWindow != null && inspectorWindow.isDocked.Value)
{
window = hierarchyWindow;
}
window.rect.yMin += vector.y;
window.position.y += vector.y / (float)Screen.height;
if (vector.y != 0f)
{
result = true;
}
}
}
}
return result;
}
public static bool DrawShowButton(SO_BaseWindow windowData, GUIContent content, GUIChangeBool show, Color color, float width = -1f, bool onlyActivate = false)
{
if (show.Value)
{
GUI.backgroundColor = GetColor(windowData, color);
}
else
{
GUI.backgroundColor = GetColor(windowData, Color.grey);
}
bool flag = ((width != -1f) ? GUILayout.Button(content, GUILayout.Width(width), GUILayout.Height(20f)) : GUILayout.Button(content, GUILayout.Height(20f)));
if (flag)
{
if (onlyActivate)
{
show.Value = true;
}
else
{
show.Value = !show.Value;
}
}
GUI.backgroundColor = GetColor(windowData, Color.white);
GUI.color = Color.white;
return flag;
}
public static bool DrawButton(GUIContent guiContent, GUIStyle style, params GUILayoutOption[] options)
{
if ((bool)guiContent.image)
{
style.contentOffset = new Vector2(16f, 0f);
}
bool result = GUILayout.Button(guiContent.text, style, options);
if ((bool)guiContent.image)
{
style.contentOffset = new Vector2(0f, 0f);
Rect lastRect = GUILayoutUtility.GetLastRect();
lastRect.x += 6f;
lastRect.y += 3f;
lastRect.width = 15f;
lastRect.height = 15f;
GUI.DrawTexture(lastRect, guiContent.image);
}
return result;
}
public static Color GetColor(SO_BaseWindow windowData, Color color)
{
color.a *= windowData.backgroundAlpha;
return color;
}
public static bool IsCulled(ref ScrollViewCullData cull, float height = 25f)
{
cull.scrollWindowPosY += height;
float num = cull.windowHeight - cull.rectStartScrollY;
if (cull.scrollWindowPosY < cull.updatedScrollViewY || cull.scrollWindowPosY > cull.updatedScrollViewY + num)
{
cull.culledSpaceY += height;
return true;
}
if (cull.culledSpaceY > 0f)
{
GUILayout.Space(cull.culledSpaceY);
cull.culledSpaceY = 0f;
}
return false;
}
public static int BinarySearch<T, U>(T[] array, int length, U value) where T : IComparable<U>
{
int num = 0;
int num2 = length - 1;
int num3 = 0;
do
{
num3 = num + (num2 - num) / 2;
switch (array[num3].CompareTo(value))
{
case -1:
num = num3 + 1;
break;
case 1:
num2 = num3 - 1;
break;
default:
return num3;
}
}
while (num <= num2);
return num3;
}
public static bool IsPrefab(UnityEngine.Object obj)
{
return false;
}
public static UnityEngine.Object FindObjectFromInstanceID(int id)
{
if (findObjectFromInstanceIdMethod == null)
{
findObjectFromInstanceIdMethod = typeof(UnityEngine.Object).GetMethod("FindObjectFromInstanceID", BindingFlags.Static | BindingFlags.NonPublic);
}
if (findObjectFromInstanceIdMethod != null)
{
return (UnityEngine.Object)findObjectFromInstanceIdMethod.Invoke(null, new object[1] { id });
}
return null;
}
}
}