#游戏unity-音之国度#战斗系统中的实时显示状态

26 篇文章 1 订阅

#游戏unity-音之国度#战斗系统中的实时显示状态

在游戏中能够实时显示回合制次数的一般的做法就是调用GUI中的Label,还可以添加一些艺术字的效果。我也是那么做的,但是博主想到,能不能做一个类似控台的样子的界面实时输出角色状态呢。于是就有了这篇博客。
既然是模仿控台的输出,那么就是要用代码实现滚动条。
其实本质就是写一个面板,往上面写字,并且与代码实时连接起来就好啦。
原理很简单,上代码
这里设置了一个切换按键,就是可以控制是否显示这个面板的,按键为BackQuote,就是在ESC按键的下面就是这个“~”,你也可以修改。


using System.Collections.Generic;
using UnityEngine;

namespace Consolation
{

    class Test : MonoBehaviour
    {

        struct Log  
        {  
            public string message;  
            public string stackTrace;  
            public LogType type;  
        }  




        public KeyCode toggleKey = KeyCode.BackQuote;  


        public bool shakeToOpen = true;  


        public float shakeAcceleration = 3f;  


        public bool restrictLogCount = false;  


        public int maxLogs = 1000;  



        readonly List<Log> logs = new List<Log>();  
        Vector2 scrollPosition;  
        bool visible;  
        bool collapse;  



        static readonly Dictionary<LogType, Color> logTypeColors = new Dictionary<LogType, Color>  
        {  
            { LogType.Assert, Color.white },  
            { LogType.Error, Color.red },  
            { LogType.Exception, Color.red },  
            { LogType.Log, Color.white },  
            { LogType.Warning, Color.yellow },  
        };  

        const string windowTitle = "Console";  
        const int margin = 20;  
        static readonly GUIContent clearLabel = new GUIContent("Clear", "Clear the contents of the console.");  
        static readonly GUIContent collapseLabel = new GUIContent("Collapse", "Hide repeated messages.");  

        readonly Rect titleBarRect = new Rect(0, 0, 10000, 20);  
        Rect windowRect = new Rect(margin, margin, Screen.width - (margin * 2), Screen.height - (margin * 2));  

        void OnEnable()
        { 
            Application.logMessageReceived += HandleLog;  

        }  

        void OnDisable()  
        {  

            Application.logMessageReceived -= HandleLog;  


        void Update()  
        {  
            if (Input.GetKeyDown(toggleKey))  
            {  
                visible = !visible;  
            }  

            if (shakeToOpen && Input.acceleration.sqrMagnitude > shakeAcceleration)  
            {  
                visible = true;  
            }  
        }  

        void OnGUI()  
        {  
            if (!visible)  
            {  
                return;  
            }  

            windowRect = GUILayout.Window(123456, windowRect, DrawConsoleWindow, windowTitle);  
        }  


        void DrawConsoleWindow(int windowID)  
        {  
            DrawLogsList();  
            DrawToolbar();  

            // Allow the window to be dragged by its title bar.  
            GUI.DragWindow(titleBarRect);  
        }  


        void DrawLogsList()  
        {  
            scrollPosition = GUILayout.BeginScrollView(scrollPosition);  

            // Iterate through the recorded logs.  
            for (var i = 0; i < logs.Count; i++)  
            {  
                var log = logs[i];  

                // Combine identical messages if collapse option is chosen.  
                if (collapse && i > 0)  
                {  
                    var previousMessage = logs[i - 1].message;  

                    if (log.message == previousMessage)  
                    {  
                        continue;  
                    }  
                }  

                GUI.contentColor = logTypeColors[log.type];  
                GUILayout.Label(log.message);  
            }  

            GUILayout.EndScrollView();  

            // Ensure GUI colour is reset before drawing other components.  
            GUI.contentColor = Color.white;  
        }  

        /// <summary>  
        /// Displays options for filtering and changing the logs list.  
        /// </summary>  
        void DrawToolbar()  
        {  
            GUILayout.BeginHorizontal();  

            if (GUILayout.Button(clearLabel))  
            {  
                logs.Clear();  
            }  

            collapse = GUILayout.Toggle(collapse, collapseLabel, GUILayout.ExpandWidth(false));  

            GUILayout.EndHorizontal();  
        }  


        void HandleLog(string message, string stackTrace, LogType type)  
        {  
            logs.Add(new Log  
            {  
                message = message,  
                stackTrace = stackTrace,  
                type = type,  
            });  

            TrimExcessLogs();  
        }  


        void TrimExcessLogs()  
        {  
            if (!restrictLogCount)  
            {  
                return;  
            }  

            var amountToRemove = Mathf.Max(logs.Count - maxLogs, 0);  

            if (amountToRemove == 0)  
            {  
                return;  
            }  

            logs.RemoveRange(0, amountToRemove);  
        }  

    }

}

接下来是测试代码,新建一个脚本来写

// Update is called once per frame  
    void Update ()  
    {  

        Debug.Log("Debug log  test");  
        Console.WriteLine("this is Console Write line test ");  


    }  

效果图如下
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值