<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                本文章由cartzhang編寫,轉載請注明出處。 所有權利保留。? 文章鏈接:http://blog.csdn.net/cartzhang/article/details/49884507 作者:cartzhang 第一部分博客鏈接: http://blog.csdn.net/cartzhang/article/details/49818953 [第一部分博客鏈接](http://blog.csdn.net/cartzhang/article/details/49818953) Github 地址:https://github.com/cartzhang/TestConsoleWindow ?[github console window](https://github.com/cartzhang/TestConsoleWindow) ### 一、你還是想要一個控制臺來顯示信息? 為什么呢?這樣就不會占用Unity本身的GUI的顯示,不去調用Unity的渲染,轉而該為Windows的渲染了。 是不是很愜意,花費少了,還更靈活了。 好極了。 ### 二、你都需要些什么? 當然是一個控制臺窗口和寫到控制臺的輸入了。 代碼是歪果仁寫的,但是很好用。 首先,輸入的代碼: 名字為ConsoleInput.cs ~~~ 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 ); } } ~~~ ### 三、輸入和窗口準備齊全了,問題來了。 當你試圖編譯代碼時候,你會發現居然編譯報錯,各種找不到。 Console.CursorLeft等大量的方法和變量都找不到。 這是因為Untiy5 默認的目標框架Unity3.5 .net sbu base class Libraries.。在Player Setting中,可設置為.Net 2.0。 ![](https://box.kancloud.cn/2016-03-09_56dfda863241b.jpg) 這就可以改變了VS下的編譯環境了。 但是要是你想要修改編譯環境為你想要的其他呢? ### 四、那么問題來了?怎么修改編譯環境的目標框架呢? ![](https://box.kancloud.cn/2016-03-09_56dfda8642948.jpg) 你肯定不會是想出這個問題的第一人?所以那就有人來解決問題; 動態的修改你的FrameWork,就可以解答這個問題。 代碼:UpgradeVSProject.cs ~~~ //#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 } ~~~ 同樣,我寫了代碼屏蔽的宏定義。使用的時候啟用就可以了。 就可以自動升高版本到4.0上,當然你也可以修改代碼來升高到其他版本的。 注意:這個代碼很明顯,需要放置在Editor文件夾下。 ### 五、測試結果 我寫了個幾行的測試代碼: ~~~ 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"); } } } ~~~ 結果就出來了: ![](https://box.kancloud.cn/2016-03-09_56dfda8656f48.jpg) 別告訴我,這不是你想要的控制臺窗口。 你看看,在寫Log時,會不會造成你的游戲卡頓了呢?? ---------THE END------------------------ 若有問題,請隨時聯系! 非常感謝!!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看