ui基本完毕,修了一大把的bug

This commit is contained in:
FloatGaming
2026-07-13 02:28:39 +08:00
parent 1e20d73e90
commit fd22501f71
958 changed files with 378289 additions and 41038 deletions
+254 -8
View File
@@ -3,11 +3,17 @@ using UnityEngine.UI;
using System.IO;
using System.Text.RegularExpressions;
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
public class readDeviceInfo : MonoBehaviour
{
private const string LabelColorHex = "#2457c6";
private const string ValueColorHex = "#6b8acd";
private const float ColumnPaddingPixels = 32f;
private const int MinimumGapSpaces = 2;
public Button reread_button;
public Button export_button;
public Text deviceInfo_text;
@@ -25,7 +31,9 @@ public class readDeviceInfo : MonoBehaviour
}
if (deviceInfo_text != null)
deviceInfo_text.supportRichText = true;
{
ConfigureDeviceInfoText();
}
read_deviceInfo();
}
@@ -45,7 +53,8 @@ public class readDeviceInfo : MonoBehaviour
private void read_deviceInfo()
{
StringBuilder sb = new StringBuilder();
ConfigureDeviceInfoText();
var entries = new System.Collections.Generic.List<KeyValuePair<string, string>>();
void AddEntry(string title, object value)
{
@@ -54,9 +63,7 @@ public class readDeviceInfo : MonoBehaviour
else if (value is bool b) str = b ? "是" : "否";
else str = value.ToString();
sb.AppendLine($"<b>{title}</b>");
sb.AppendLine(str);
sb.AppendLine();
entries.Add(new KeyValuePair<string, string>(title ?? string.Empty, str ?? string.Empty));
}
AddEntry("平台", Application.platform.ToString());
@@ -101,7 +108,7 @@ public class readDeviceInfo : MonoBehaviour
AddEntry("启用后台运行", Application.runInBackground);
AddEntry("启用多线程渲染", SystemInfo.graphicsMultiThreaded);
string output = sb.ToString();
string output = BuildTwoColumnOutput(entries);
if (deviceInfo_text != null)
{
@@ -125,8 +132,7 @@ public class readDeviceInfo : MonoBehaviour
try
{
string raw = deviceInfo_text.text;
// Remove <b> tags and add colon after title
string cleaned = Regex.Replace(raw, "<b>(.*?)</b>", "$1:");
string cleaned = Regex.Replace(raw, "<.*?>", string.Empty);
File.WriteAllText(path, cleaned);
}
catch (System.Exception)
@@ -134,6 +140,246 @@ public class readDeviceInfo : MonoBehaviour
}
}
private string BuildTwoColumnOutput(System.Collections.Generic.IReadOnlyList<KeyValuePair<string, string>> entries)
{
if (entries == null || entries.Count == 0)
{
return string.Empty;
}
float maxLabelWidth = 0f;
for (int i = 0; i < entries.Count; i++)
{
maxLabelWidth = Mathf.Max(maxLabelWidth, MeasurePlainTextWidth(entries[i].Key));
}
float availableWidth = GetAvailableTextWidth();
float targetLabelWidth = maxLabelWidth + ColumnPaddingPixels;
if (availableWidth > 0f)
{
targetLabelWidth = Mathf.Min(targetLabelWidth, availableWidth * 0.38f);
}
float spaceWidth = Mathf.Max(1f, MeasurePlainTextWidth(" "));
int continuationIndentSpaces = Mathf.Max(MinimumGapSpaces, Mathf.CeilToInt(targetLabelWidth / spaceWidth) + MinimumGapSpaces);
string continuationIndent = new string(' ', continuationIndentSpaces);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < entries.Count; i++)
{
string label = entries[i].Key ?? string.Empty;
string value = entries[i].Value ?? string.Empty;
int gapSpaces = Mathf.Max(
MinimumGapSpaces,
Mathf.CeilToInt((targetLabelWidth - MeasurePlainTextWidth(label)) / spaceWidth) + MinimumGapSpaces);
string padding = new string(' ', gapSpaces);
float firstLineValueWidth = availableWidth > 0f
? Mathf.Max(32f, availableWidth - MeasurePlainTextWidth(label) - MeasurePlainTextWidth(padding))
: 0f;
float continuationValueWidth = availableWidth > 0f
? Mathf.Max(32f, availableWidth - MeasurePlainTextWidth(continuationIndent))
: 0f;
List<string> wrappedLines = WrapValueLines(value, firstLineValueWidth, continuationValueWidth);
sb.Append("<color=").Append(LabelColorHex).Append("><b>")
.Append(EscapeRichText(label))
.Append("</b></color>")
.Append(padding)
.Append("<color=").Append(ValueColorHex).Append(">")
.Append(EscapeRichText(wrappedLines[0]))
.Append("</color>");
for (int lineIndex = 1; lineIndex < wrappedLines.Count; lineIndex++)
{
sb.AppendLine();
sb.Append(continuationIndent)
.Append("<color=").Append(ValueColorHex).Append(">")
.Append(EscapeRichText(wrappedLines[lineIndex]))
.Append("</color>");
}
if (i < entries.Count - 1)
{
sb.AppendLine();
}
}
return sb.ToString();
}
private void ConfigureDeviceInfoText()
{
if (deviceInfo_text == null)
{
return;
}
deviceInfo_text.supportRichText = true;
deviceInfo_text.alignment = TextAnchor.UpperLeft;
deviceInfo_text.horizontalOverflow = HorizontalWrapMode.Wrap;
deviceInfo_text.verticalOverflow = VerticalWrapMode.Overflow;
deviceInfo_text.resizeTextForBestFit = false;
}
private float GetAvailableTextWidth()
{
if (deviceInfo_text == null)
{
return 0f;
}
RectTransform rectTransform = deviceInfo_text.rectTransform;
if (rectTransform == null)
{
return 0f;
}
float width = rectTransform.rect.width;
return width > 0f ? width : 0f;
}
private float MeasurePlainTextWidth(string text)
{
if (deviceInfo_text == null || string.IsNullOrEmpty(text))
{
return 0f;
}
TextGenerationSettings settings = deviceInfo_text.GetGenerationSettings(Vector2.zero);
settings.richText = false;
settings.generateOutOfBounds = true;
TextGenerator generator = new TextGenerator();
return generator.GetPreferredWidth(text, settings) / deviceInfo_text.pixelsPerUnit;
}
private List<string> WrapValueLines(string value, float firstLineWidth, float continuationLineWidth)
{
string normalized = NormalizeLineBreaks(value);
if (string.IsNullOrEmpty(normalized))
{
return new List<string> { string.Empty };
}
string[] rawLines = normalized.Split('\n');
List<string> wrapped = new List<string>();
for (int i = 0; i < rawLines.Length; i++)
{
string rawLine = rawLines[i];
List<string> lineParts = WrapSingleLine(rawLine, wrapped.Count == 0 ? firstLineWidth : continuationLineWidth);
if (lineParts.Count == 0)
{
lineParts.Add(string.Empty);
}
wrapped.Add(lineParts[0]);
for (int j = 1; j < lineParts.Count; j++)
{
wrapped.Add(lineParts[j]);
}
}
return wrapped.Count > 0 ? wrapped : new List<string> { string.Empty };
}
private List<string> WrapSingleLine(string text, float maxWidth)
{
List<string> lines = new List<string>();
if (string.IsNullOrEmpty(text) || maxWidth <= 0f)
{
lines.Add(text ?? string.Empty);
return lines;
}
string remaining = text;
while (!string.IsNullOrEmpty(remaining))
{
if (MeasurePlainTextWidth(remaining) <= maxWidth)
{
lines.Add(remaining);
break;
}
int splitIndex = FindSplitIndex(remaining, maxWidth);
if (splitIndex <= 0 || splitIndex >= remaining.Length)
{
lines.Add(remaining);
break;
}
string current = remaining.Substring(0, splitIndex).TrimEnd();
if (current.Length == 0)
{
current = remaining.Substring(0, Mathf.Min(1, remaining.Length));
splitIndex = current.Length;
}
lines.Add(current);
remaining = remaining.Substring(splitIndex).TrimStart();
}
return lines;
}
private int FindSplitIndex(string text, float maxWidth)
{
int lastBreakableIndex = -1;
int fallbackIndex = -1;
for (int i = 1; i <= text.Length; i++)
{
string candidate = text.Substring(0, i);
if (MeasurePlainTextWidth(candidate) <= maxWidth)
{
fallbackIndex = i;
if (IsBreakableCharacter(text[i - 1]))
{
lastBreakableIndex = i;
}
continue;
}
break;
}
if (lastBreakableIndex > 0)
{
return lastBreakableIndex;
}
return fallbackIndex;
}
private static bool IsBreakableCharacter(char c)
{
return char.IsWhiteSpace(c) || c == '/' || c == '\\' || c == '_' || c == '-' || c == '.' || c == ':' || c == ')' || c == ']';
}
private static string NormalizeLineBreaks(string input)
{
if (string.IsNullOrEmpty(input))
{
return string.Empty;
}
return input.Replace("\r\n", "\n").Replace('\r', '\n');
}
private static string EscapeRichText(string input)
{
if (string.IsNullOrEmpty(input))
{
return string.Empty;
}
return input.Replace("&", "&amp;")
.Replace("<", "&lt;")
.Replace(">", "&gt;");
}
string ShowSaveFileDialog()
{
#if UNITY_EDITOR