学习(四):显示FPS,和自定义显示调试

显示FPS

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ShowFPS : MonoBehaviour {
    // 固定的一个时间间隔
    private float time_delta = 0.5f;
    // Time.realtimeSinceStartup: 指的是我们当前从启动开始到现在运行的时间,单位(s)
    private float prev_time = 0.0f; // 上一次统计FPS的时间;
    private float fps = 0.0f; // 计算出来的FPS的值;
    private int i_frames = 0; // 累计我们刷新的帧数;

    // GUI显示;
    private GUIStyle style;

    void Awake() {
        // 假设CPU 100% 工作的状态下FPS 300,
        // 当你设置了这个以后,他就维持在60FPS左右,不会继续冲高;
        // -1, 游戏引擎就会不段的刷新我们的画面,有多高,刷多高; 60FPS左右;
        Application.targetFrameRate = 60;
    }

	// Use this for initialization
	void Start () {
        this.prev_time = Time.realtimeSinceStartup;
        this.style = new GUIStyle();
        this.style.fontSize = 15;
        this.style.normal.textColor = new Color(255, 255, 255);
	}

    void OnGUI() {
        GUI.Label(new Rect(0, Screen.height - 20, 200, 200), "FPS:" + this.fps.ToString("f2"), this.style);
    }
	// Update is called once per frame
    // 每次游戏刷新的时候就会调用;
	void Update () {
        this.i_frames ++;

        if (Time.realtimeSinceStartup >= this.prev_time + this.time_delta) {
            this.fps = ((float)this.i_frames) / (Time.realtimeSinceStartup - this.prev_time);
            this.prev_time = Time.realtimeSinceStartup;
            this.i_frames = 0; // 重新累积我们的FPS
        }
	}
}

显示log,自定义logo,方便手机打印


#define USE_DEBUG 

using UnityEngine;
using System;
using System.Collections.Generic;



#if RELEASE_BUILD
//发布的时候重载默认Debug
public static class Debug
{
    public static void Log(object message) { }
    public static void Log(object message, object context) { }
    public static void LogError(object message) { }
    public static void LogError(object message, object context) { }
    public static void LogException(Exception exception) { }
    public static void LogException(Exception exception, object context) { }
    public static void LogWarning(object message) { }
    public static void LogWarning(object message, object context) { }
    public static void DrawLine(Vector3 start, Vector3 end) { }
    public static void DrawLine(Vector3 start, Vector3 end, Color color) { }
}
#endif


//重载默认Debug
public static class Debugger
{
    public static void Log(object message)
    {
        #if RELEASE_BUILD
        #else
            if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
                DebugManager.Log(0,message.ToString());
            else
                UnityEngine.Debug.Log(message.ToString());

         #endif
    }

    public static void LogError(object message)
    {
        #if RELEASE_BUILD
        #else
        if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
            DebugManager.Log(2,message.ToString());
        else
            UnityEngine.Debug.LogError(message.ToString());
        #endif
    }


    public static void LogWarning(object message)
    {
        #if RELEASE_BUILD
        #else
        if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
            DebugManager.Log(1,message.ToString());
        else
            UnityEngine.Debug.LogWarning(message.ToString());
        #endif
    }
}



//[yaz]调试管理器
public class DebugManager : MonoBehaviour 
{
    public class DebugMessage
    {
        public int type;
        public string message;
    }
    // public bool DebugEffect = false;
    static public bool DebugInfo = false;
    static public Int32 DebugCount = 0;

    //保存错误信息列表
    static public List<DebugMessage> errorInfoList = new List<DebugMessage>();

	// Use this for initialization
	void Start ()
    {
        Log(0, "Log");
        Log(1, "LogWarning");
        Log(2, "LogError");

    }

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

    public static void Switch()
    {
        DebugInfo = !DebugInfo;
        if (DebugInfo)
        {
            ++DebugCount;
            errorInfoList.Clear();
            //errorInfoList.Add(DebugCount.ToString());
        }
    }

    public static void Log(int type,string str)
    {
        DebugMessage debug = new DebugMessage();
        debug.type = type;
        debug.message = str;

        errorInfoList.Add(debug);
    }

    //错误信息窗口
    public Rect errorInfoWindowRect = new Rect(80, 20, 800, 2000);

    void OnGUI()
    {
        GUILayout.Space(40);
        if (GUILayout.Button("ShowError"))
        {
            DebugInfo = !DebugInfo;
        }
        else if (GUILayout.Button("Clear"))
        {
            errorInfoList.Clear();
        }

        //调试错误信息
        if (DebugInfo)
        {
            errorInfoWindowRect = GUILayout.Window(1, errorInfoWindowRect, DebugErrorWindow, "Debug Error Window");            
        }
    }

    //错误信息显示窗口
    private Vector2 errorInfoPos = new Vector2(0, 0);
    void DebugErrorWindow(int windowID)
    {      
        errorInfoPos = GUILayout.BeginScrollView(errorInfoPos, false, true, GUILayout.Width(800), GUILayout.Height(2000));

        GUILayout.Space(30);
        GUILayout.BeginVertical();
        
        foreach (DebugMessage debug in errorInfoList)
        {
            if (debug.type==2)
            {
                GUI.color = Color.red;
            }
            else if (debug.type == 1)
            {
                GUI.color = Color.yellow;
            }
            else
            {
                GUI.color = Color.white;
            }

            GUILayout.Label(debug.message, GUILayout.Width(800));
        }
    
        //GUI.color = Color.white;
        GUILayout.EndVertical();
        GUILayout.EndScrollView();
    }


}

猜你喜欢

转载自blog.csdn.net/weixin_41843959/article/details/126020067