当前位置: 首页 > news >正文

Unity Console 窗口输出对齐

起因:做了个工具在console窗口罗列一些信息,基本结构是 [ 文件名        :行号 ],因为文件,行号长度不一,想要做到如下效果。

初步尝试,用以下方法:

string format = "{0,-10} {1,5}";  // -10 表示左对齐,10个字符宽;5 表示右对齐,5个字符宽
Debug.Log(string.Format(format, "File", "Line"));
Debug.Log(string.Format(format, "UI/Titan/UI_Titan_Main", "1"));
Debug.Log(string.Format(format, "Utils/GlobalFunction", "8"));

实际效果如下:

竟然没对齐?拷贝到IntelliJ 里查看

Utils/GlobalFunction                              |        :8
Lua/HookReloadLuaFuc                              |       :35
Lua/HookReloadLuaFuc                              |     :1290

IntelliJ用了等宽字符,所以看起来是对齐的。

后续就简单了,拿到console里字体的宽度就行了。

这里贴一个大概的实现,宽度表放在最后

一个简单的lua实现

local _QuickJumpTab = {"Utils/GlobalFunction:8","Lua/HookReloadLuaFuc:35:print_table","Lua/HookReloadLuaFuc:1290:print_table","UI/Titan/TitanUtil:10:泰坦相关Gid",
}local function GetFileLine(str)local strTab = split(str,":")return unpack(strTab)
end-- 判断是否是中文字符 并且返回字符长度
local function IsChineseChar(char)local byte = string.byte(char)if byte >= 0x80 thenreturn true,2elsereturn false,1end
endlocal function Utf8StringLen(str)local len = 0local strTab = StringToUtf8Table(str)for i, v in ipairs(strTab) doif IsChineseChar(v) thenlen = len + 12elselen = len + _ConsoleCharWidth[v] or 3 -- 3 is width of spaceendendreturn len
endlocal longestStr = ""
for k, v in pairs(_QuickJumpTab) dolocal fileStr = GetFileLine(v)if Utf8StringLen(fileStr) > Utf8StringLen(longestStr) thenlongestStr = fileStrend
end
local longestStrLen = Utf8StringLen(longestStr)
--logGreen("longestStrLen\t" .. longestStrLen)local _wrapColor = function(color,str)return concat({"<color=",color,">",str,"</color>"})
end-- 自己实现一个往左边或者右边加空格补齐长度的函数
-- 用于打印的时候对齐
local FillLen = function(str,len,align,fillStr)align = align or "left"local strLen = Utf8StringLen(str)if strLen >= len thenreturn strendlocal spaceLen = len - strLenlocal needFillCount = max(spaceLen,1)if not fillStr thenfillStr = " "needFillCount = needFillCount/_ConsoleCharWidth[fillStr]endlocal spaceStr = string.rep(fillStr,ceil(needFillCount))if align == "right" thenreturn concat({str,spaceStr})elsereturn concat({spaceStr,str})end
endlocal exSymbolLen = _ConsoleCharWidth[":"] + _ConsoleCharWidth["["] + _ConsoleCharWidth["]"]local _WrapDebugStr = function(file,line,des,fileColor,lineColor)fileColor = fileColor or "#CA550C"--"cyan"lineColor = lineColor or "#00FFFF"local maxFileLen = longestStrLen+8local fileStr = FillLen(file,maxFileLen,"right")local lineStr = FillLen(":" .. line,36,"left")fileStr = _wrapColor(fileColor,fileStr)lineStr = _wrapColor(lineColor,lineStr)local desFillStr = "-"local fillCharWidth = _ConsoleCharWidth[desFillStr]maxFileLen = maxFileLen + 36 + exSymbolLenif des thenlocal colorLen = Utf8StringLen("<color=white></color>")des = _wrapColor("white",des)des = FillLen(des,(maxFileLen+colorLen)/fillCharWidth,"right",desFillStr)elsedes = FillLen("",maxFileLen/fillCharWidth,"left",desFillStr)endstr = format("[%s%s]\n%s",fileStr,lineStr,des)return str
end
_ConsoleCharWidth = {[" "] = 3,["!"] = 3,["\""] = 5,["#"] = 8,["$"] = 7,["%"] = 11,["&"] = 9,["'"] = 3,["("] = 4,[")"] = 4,["*"] = 6,["+"] = 8,[","] = 3,["-"] = 6, -- 4 original["."] = 3,["/"] = 4,[":"] = 3,[";"] = 3,["<"] = 8,["="] = 8,[">"] = 8,["?"] = 7,["@"] = 12,["["] = 4,["\\"] = 4,["]"] = 4,["^"] = 6,["_"] = 5,["`"] = 3,["{"] = 5,["|"] = 3,["}"] = 5,["~"] = 8,A = 8,B = 8,C = 9,D = 9,E = 7,F = 7,G = 9,H = 9,I = 3,J = 7,K = 8,L = 7,M = 11,N = 9,O = 9,P = 8,Q = 9,R = 8,S = 8,T = 8,U = 9,V = 8,W = 11,X = 8,Y = 8,Z = 8,a = 7,b = 7,c = 7,d = 7,e = 7,f = 4,g = 7,h = 7,i = 3,j = 3,k = 7,l = 3,m = 10,n = 7,o = 7,p = 7,q = 7,r = 4,s = 6,t = 4,u = 7,v = 7,w = 10,x = 6,y = 6,z = 6,["0"] = 8,["1"] = 6,["2"] = 7,["3"] = 8,["4"] = 8,["5"] = 7,["6"] = 7,["7"] = 7,["8"] = 7,["9"] = 7,
}
http://www.lryc.cn/news/418472.html

相关文章:

  • leetcode198_打家劫舍
  • C# 串口通讯怎么防止数据丢失
  • 【机器学习】BP神经网络中的链式法则:解开智能背后的数学奥秘
  • MyBatis 基本操作 - 注解版
  • 专业比例阀放大器配套选型
  • Springboot 多数据源整合的三种方式
  • 【科研笔记】中国知网高级检索与专业检索针对同一检索内容返回的结果对比
  • C#-了解IOC控制反转及相关框架的使用
  • CSDN机器人与僵shi粉测试(真人看看)
  • 【C/C++ 多态中的虚函数的虚函数表】详细的了解一下吧(要先知道有虚函数表
  • 基于树莓派4B设计的智能家居控制系统(阿里云IOT)(203)
  • 太阳能光伏气象站的功能优势
  • LVS(Linux Virtual Server)负载均衡详解
  • C语言 | Leetcode C语言题解之第329题矩阵中的最长递增路径
  • rabbitmq学习记录
  • MySQL数据库基础:约束
  • Java设计模式和AOP编程
  • 【扒代码】data.py
  • 【时时三省】unity test 测试框架 介绍(适用于C语言进行测试的)
  • 那些你应该掌握的linux命令
  • 系统出现高CPU可能风险因素整理
  • 前端技术 -- 动画效果之GSAP作用与使用示例
  • C口一拖二数据线:解锁数字生活的便捷新篇章LDR6020
  • CH07_数据绑定
  • 24.python基础(8.8)
  • 【论文阅读】MobileNetV4 - Universal Models for the Mobile Ecosystem
  • 大模型日报 2024-08-07
  • 区块链ddos防护怎么做
  • 在Linux中认识pthread库
  • LVS 负载均衡