Unity 实现Log实时输出到屏幕或控制台上<二>
本文章由cartzhang编写,转载请注明出处。 所有权利保留。
文章链接:http://blog.csdn.net/cartzhang/article/details/49884507
作者:cartzhang
第一部分博客链接: http://blog.csdn.net/cartzhang/article/details/49818953
Github 地址:https://github.com/cartzhang/TestConsoleWindow github console window
一、你还是想要一个控制台来显示信息?
为什么呢?这样就不会占用Unity本身的GUI的显示,不去调用Unity的渲染,转而该为Windows的渲染了。
是不是很惬意,花费少了,还更灵活了。
好极了。
二、你都需要些什么?
using UnityEngine;
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.IO; namespace ConsoleTestWindows
{
public class ConsoleInput
{
//public delegate void InputText( string strInput );
public event System.Action<string> OnInputText;
public string inputString; public void ClearLine()
{
//System.Text.Encoding test = Console.InputEncoding;
Console.CursorLeft = 0;
Console.Write( new String( ' ', Console.BufferWidth ) );
Console.CursorTop--;
Console.CursorLeft = 0;
} public void RedrawInputLine()
{
if ( inputString.Length == 0 ) return; if ( Console.CursorLeft > 0 )
ClearLine(); System.Console.ForegroundColor = ConsoleColor.Green;
System.Console.Write( inputString );
} internal void OnBackspace()
{
if ( inputString.Length < 1 ) return; inputString = inputString.Substring( 0, inputString.Length - 1 );
RedrawInputLine();
} internal void OnEscape()
{
ClearLine();
inputString = "";
} internal void OnEnter()
{
ClearLine();
System.Console.ForegroundColor = ConsoleColor.Green;
System.Console.WriteLine( "> " + inputString ); var strtext = inputString;
inputString = ""; if ( OnInputText != null )
{
OnInputText( strtext );
}
} public void Update()
{
if ( !Console.KeyAvailable ) return;
var key = Console.ReadKey(); if ( key.Key == ConsoleKey.Enter )
{
OnEnter();
return;
} if ( key.Key == ConsoleKey.Backspace )
{
OnBackspace();
return;
} if ( key.Key == ConsoleKey.Escape )
{
OnEscape();
return;
} if ( key.KeyChar != '\u0000' )
{
inputString += key.KeyChar;
RedrawInputLine();
return;
}
}
}
}
然后,你还需要一个控制台窗口
using UnityEngine;
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.IO; namespace ConsoleTestWindows
{
/// <summary>
/// Creates a console window that actually works in Unity
/// You should add a script that redirects output using Console.Write to write to it.
/// </summary>
public class ConsoleWindow
{
TextWriter oldOutput; public void Initialize()
{
//
// Attach to any existing consoles we have
// failing that, create a new one.
//
if ( !AttachConsole( 0x0ffffffff ) )
{
AllocConsole();
} oldOutput = Console.Out; try
{
IntPtr stdHandle = GetStdHandle( STD_OUTPUT_HANDLE );
Microsoft.Win32.SafeHandles.SafeFileHandle safeFileHandle = new Microsoft.Win32.SafeHandles.SafeFileHandle( stdHandle, true );
FileStream fileStream = new FileStream(safeFileHandle, FileAccess.Write);
System.Text.Encoding encoding = System.Text.Encoding.ASCII;
StreamWriter standardOutput = new StreamWriter( fileStream, encoding );
standardOutput.AutoFlush = true;
Console.SetOut( standardOutput );
}
catch ( System.Exception e )
{
Debug.Log( "Couldn't redirect output: " + e.Message );
}
} public void Shutdown()
{
Console.SetOut( oldOutput );
FreeConsole();
} public void SetTitle( string strName )
{
SetConsoleTitle( strName );
} private const int STD_OUTPUT_HANDLE = -11; [DllImport( "kernel32.dll", SetLastError = true )]
static extern bool AttachConsole( uint dwProcessId ); [DllImport( "kernel32.dll", SetLastError = true )]
static extern bool AllocConsole(); [DllImport( "kernel32.dll", SetLastError = true )]
static extern bool FreeConsole(); [DllImport( "kernel32.dll", EntryPoint = "GetStdHandle", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall )]
private static extern IntPtr GetStdHandle( int nStdHandle ); [DllImport( "kernel32.dll" )]
static extern bool SetConsoleTitle( string lpConsoleTitle );
}
}
三、输入和窗口准备齐全了,问题来了。
四、那么问题来了?怎么修改编译环境的目标框架呢?
//#define USE_UPGRADEVS
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
using System.Text.RegularExpressions; class UpgradeVSProject : AssetPostprocessor
{
#if USE_UPGRADEVS
private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
{
string currentDir = Directory.GetCurrentDirectory();
string[] slnFile = Directory.GetFiles(currentDir, "*.sln");
string[] csprojFile = Directory.GetFiles(currentDir, "*.csproj"); bool hasChanged = false;
if (slnFile != null)
{
for (int i = 0; i < slnFile.Length; i++)
{
if (ReplaceInFile(slnFile[i], "Format Version 10.00", "Format Version 11.00"))
hasChanged = true;
}
} if (csprojFile != null)
{
for (int i = 0; i < csprojFile.Length; i++)
{
if (ReplaceInFile(csprojFile[i], "ToolsVersion=\"3.5\"", "ToolsVersion=\"4.0\""))
hasChanged = true; if (ReplaceInFile(csprojFile[i], "<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>", "<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>"))
hasChanged = true;
}
} if (hasChanged)
{
Debug.LogWarning("Project is now upgraded to Visual Studio 2010 Solution!");
}
else
{
Debug.Log("Project-version has not changed...");
}
} static private bool ReplaceInFile(string filePath, string searchText, string replaceText)
{
StreamReader reader = new StreamReader(filePath);
string content = reader.ReadToEnd();
reader.Close(); if (content.IndexOf(searchText) != -1)
{
content = Regex.Replace(content, searchText, replaceText);
StreamWriter writer = new StreamWriter(filePath);
writer.Write(content);
writer.Close(); return true;
} return false;
}
#endif
}
同样,我写了代码屏蔽的宏定义。使用的时候启用就可以了。
五、测试结果
using UnityEngine;
using System.Collections; public class Tes : MonoBehaviour { // Use this for initialization
void Start () { } // Update is called once per frame
void Update ()
{
if (Input.GetKey(KeyCode.A))
{
Debug.Log("this is debug log");
System.Console.WriteLine("this is system console write line");
} }
}
结果就出来了:
Unity 实现Log实时输出到屏幕或控制台上<二>的更多相关文章
- Unity 实现Log实时输出到屏幕或控制台上<一>
本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/49818953 作者:car ...
- unity收集log工具
参考 yusong:http://www.xuanyusong.com/archives/2477 凉鞋 :https://www.cnblogs.com/liangxiegame/p/Uni ...
- [Linux]屏幕输出控制
专门的术语叫做ANSI Escape sequences(ANSI Escape codes),题目并不恰当,与其说是屏幕输出控制,不如说是通过bash在兼容VT100的终端上进行输出. 主要有以下类 ...
- .NET Core下的日志(3):如何将日志消息输出到控制台上
当我们利用LoggerFactory创建一个Logger对象并利用它来实现日志记录,这个过程会产生一个日志消息,日志消息的流向取决于注册到LoggerFactory之上的LoggerProvider. ...
- java 在控制台上输入密码时,密码不显示在控制台上
用下面的方法可以实现在控制台上输入密码时,密码不显示在控制台上:Console cons=System.console(); System.out.print(" 密码:"); c ...
- 大数据调错系列之hadoop在开发工具控制台上打印不出日志的解决方法
(1)在windows环境上配置HADOOP_HOME环境变量 (2)在eclipse上运行程序 (3)注意:如果eclipse打印不出日志,在控制台上只显示 1.log4j:WARN No appe ...
- eclipse:eclipse for java EE环境下如何配置tomcat服务器,并让tomcat服务器显示在控制台上,将Web应用部署到tomcat中
eclipse环境下如何配置tomcat 打开Eclipse,单击"Window"菜单,选择下方的"Preferences". 单击"Server& ...
- Ubuntu上运行Blender,在控制台上查看运行结果
1.首先在控制台打开Blender. 具体操作:找到Blender的安装路径,我的是:/home/lcx/下载/blender-2.78c-linux-glibc219-x86_64 $cd /hom ...
- Java:IO流的综合用法(从键盘录入数据并打印在控制台上)
import java.io.*; public class IOTestDouble { public static void main(String[] args)throws Exception ...
随机推荐
- js闭包的用途详解
js闭包可以用在许多地方.它的最大用处有两个,一个是前面提到的可以读取函数内部的变量,另一个就是让这些变量的值始终保持在内存中 我们来看看闭包的用途.事实上,通过使用闭包,我们可以做很多事情.比如模拟 ...
- shell脚本执行的三种方式
(1) bash script_name 或 sh script_name 推荐使用此方法,script_name 不需要执行权限亦可执行. (2) path/script_name 或 ...
- c++类模板初探
#include <iostream> #include <string> using namespace std; // 你提交的代码将嵌入到这里 ; template &l ...
- Reactor Cooling ZOJ - 2314 上下界网络流
Code: #include<cstdio> #include<algorithm> #include<vector> #include<queue> ...
- DataTable相关操作,筛选,取前N条数据,去重复行,获取指定列数据
#region DataTable筛选,排序返回符合条件行组成的新DataTable或直接用DefaultView按条件返回 /// <summary> /// Dat ...
- thinkphp 5.0整合phpsocketio完整攻略,绕坑
使用环境: thinkphp5.0 项目需求 前端下单,后台接受,并立即做出提示.例如:美团外卖,客户端下单成功后,商家端就会立即有接单语音提示. 开发环境 thinkphp5.0 phpsocket ...
- 【codeforces 816A】Karen and Morning
[题目链接]:http://codeforces.com/contest/816/problem/A [题意] 让你一分钟一分钟地累加时间; 问多长时间以后是个回文串; [题解] reverse之后如 ...
- Docker和虚拟机的区别
这是docker官网的图,可以看到虚拟化技术通过Hypervisor(虚拟机管理系统)为每个app启动一个Guest OS(客户机操作系统),也就是为每个app启动一个虚拟机.比较直观地说,vm通过H ...
- 集团公司(嵌入ETL工具)財务报表系统解决方式
集团公司(嵌入ETL工具)財务报表系统解决方式 一.项目背景: 某集团公司是一家拥有100多家子公司的大型集团公司,旗下子公司涉及各行各业,包含:金矿.铜矿.房产.化纤等.因为子公司在业务上的差异.子 ...
- 如何用一次性密码通过 SSH 安全登录 Linux
有人说,安全不是一个产品,而是一个过程.虽然 SSH 协议被设计成使用加密技术来确保安全,但如果使用不当,别人还是能够破坏你的系统:比如弱密码.密钥泄露.使用过时的 SSH 客户端等,都能引发安全问题 ...