首页 >Unity3D频道 >【Unity3D研究院之游戏开发】 > Unity3D研究院之手机上保存Profiler与提取查看(一百零二)
2018
12-10

Unity3D研究院之手机上保存Profiler与提取查看(一百零二)

手机上可以把一段时间的profiler日志保存在本地,接着在手机里把文件取出来就可以在电脑上查看了,很方便,上代码,在需要保存手机Profiler日志的时候调用  ProfilerUtils.BeginRecord();并且我已经封装好了。

using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Profiling;

public class ProfilerUtils {


    static private GameObject profilerGameObject;

    static ProfilerUtils(){
        profilerGameObject = new GameObject("#Profiler#");
        UnityEngine.Object.DontDestroyOnLoad(profilerGameObject);
    }


    static public void BeginRecord()
    {
        if(!profilerGameObject.GetComponent<InternalBehaviour>()){
            profilerGameObject.AddComponent<InternalBehaviour>();
        }
    }

    class InternalBehaviour : MonoBehaviour
    {
        private string m_DebugInfo = String.Empty;

        private void OnGUI()
        {
            GUILayout.Label(String.Format("<size=50>{0}</size>", m_DebugInfo));
        }

        IEnumerator Start()
        {

            for (int i = 5; i > 0; i--)
            {
                m_DebugInfo = string.Format("<color=blue>{0}s后开始保存Profiler日志</color>", i);
                yield return new WaitForSeconds(1);
            }
            string file = Application.persistentDataPath + "/profiler_" + DateTime.Now.ToString("yyyyMMddhhmmss") +".log";
            Profiler.logFile = file;
            Profiler.enabled = true;
            Profiler.enableBinaryLog = true;

            for (int i = 5; i > 0; i--)
            {
                m_DebugInfo = string.Format("<color=red>{0}s后结束保存Profiler日志</color>", i);
                yield return new WaitForSeconds(1);
            }

            Profiler.enableBinaryLog = false;
            m_DebugInfo = string.Format("保存完毕:{0}", file);
            yield return new WaitForSeconds(10);

            Destroy(this);
        }
    }

}

 

Snip20181210_1

Snip20181210_3

最后在Profier窗口中点击load载入即可,如果你用的unity版本比较老,并没有load按钮,调用Proilfer.AddframesFromeFile也可以载入profiler信息。

Snip20181210_4

 

最后编辑:
作者:雨松MOMO
专注移动互联网,Unity3D游戏开发
捐 赠写博客不易,如果您想请我喝一杯星巴克的话?就进来看吧!

Unity3D研究院之手机上保存Profiler与提取查看(一百零二)》有 5 条评论

  1. DrangKe说:

    Profiler.enableBinaryLog = true; 这个要设置为false,然后用程序setframe,不然内存很快就不够用了

  2. 葫芦说:

    没有看明白怎么使用啊

  3. congy说:

    真机上无法保存 profiler 日志,显示 Profiler is not supported in this build 错误。

留下一个回复

你的email不会被公开。 必填项已用 * 标注。