Unity3D研究院之IOS&Android收集Log文件

在开始这篇文章之前,我们先来Unity官网的日志文件的介绍。

There might be times during the development when you need to obtain information from the logs of the webplayer you've built, your standalone player, the target device or the editor. Usually you need to see these files when you have experienced a problem and you have to know where exactly the problem occurred.

有可能是在开发时,您需要从你所构建好的WebPlayer,单机版的播放器,目标设备或编辑器的日志获取信息。通常,当您遇到了问题,你需要看这些文件,你要知道什么地方出现问题。

On Mac the webplayer, player and editor logs can be accessed uniformly through the standard Console.app utility.

在Mac网页播放器,播放器和编辑器日志可以统一通过标准Console.app程序访问。

On Windows the webplayer and editor logs are place in folders there are not shown in the Windows Explorer by default. Please see the Accessing hidden folders page to resolve that situation.

在Windows 系统,默认情况下,WebPlayer和编辑器日志放置在在Windows资源管理器里没有显示的文件夹中。解决这种情况,请参阅访问隐藏的文件夹(Accessing hidden folders)的页面。

Editor 编辑器

Editor log can be brought up through the Open Editor Log button in Unity's Console window.

编辑器日志,可以通过Unity的控制台窗口中Open Editor Log 按钮打开。

Mac OS X~/Library/Logs/Unity/Editor.log
Windows XP *C:\Documents and Settings\username\Local Settings\Application Data\Unity\Editor\Editor.log
Windows Vista/7 *C:\Users\username\AppData\Local\Unity\Editor\Editor.log

(*) On Windows the Editor log file is stored in the local application data folder: %LOCALAPPDATA%\Unity\Editor\Editor.log, where LOCALAPPDATA is defined by CSIDL_LOCAL_APPDATA.

(*)在Windows的编辑器日志文件存储在本地应用程序数据文件夹:%LOCALAPPDATA%\Unity\Editor\Editor.log,LOCALAPPDATA 在CSIDL_LOCAL_APPDATA定义。

Desktop

On Mac all the logs can be accessed uniformly through the standard Console.app utility.

在Mac所有日志可以统一通过标准Console.app程序访问。

Webplayer

Mac OS X~/Library/Logs/Unity/WebPlayer.log
Windows XP *C:\Documents and Settings\username\Local Settings\Temp\UnityWebPlayer\log\log_UNIQUEID.txt
Windows Vista/7 *C:\Users\username\AppData\Local\Temp\UnityWebPlayer\log\log_UNIQUEID.txt
Windows Vista/7 + IE7 + UAC *C:\Users\username\AppData\Local\Temp\Low\UnityWebPlayer\log\log_UNIQUEID.txt

(*) On Windows the webplayer log is stored in a temporary folder: %TEMP%\UnityWebPlayer\log\log_UNIQUEID.txt, where TEMP is defined byGetTempPath.

(*)在Windows WebPlayer日志存储在一个临时文件夹:%TEMP%\UnityWebPlayer\log\log_UNIQUEID.txt,,TEMP在GetTempPath定义。

Player

Mac OS X~/Library/Logs/Unity/Player.log
Windows *EXECNAME_Data\output_log.txt

(*) EXECNAME_Data is a folder next to the executable with your game.

(*) EXECNAME_Data是你的游戏的可执行文件夹的目录。

iOS

The device log can be accessed in XCode via GDB console or the Organizer Console. The latter is useful for getting crashlogs when your application was not running through the XCode debugger.

在Xcode通过GDB控制台或Organizer Console可以访问设备的日志。后者是当您的应用程序没有通过Xcode调试器运行获得crashlogs。

Please see Debugging Applications in the iOS Development Guide. Also our Troubleshooting and Bugreporting guides may be useful for you.

在iOS开发指南,请参阅调试应用程序。此外,我们的故障排除(Troubleshooting)和Bugreporting 指导可能对您有用。

Android

The device log can be viewed by using the logcat console. Use the adb application found in Android SDK/platform-tools directory with a trailing logcat parameter:

使用logcat控制台( logcat console)可以查看设备的日志。使用在Android SDK/platform-tools directory 的adb应用程序,后面跟着一个logcat参数:

$ adb logcat 

Another way to inspect the LogCat is to use the Dalvik Debug Monitor Server (DDMS). DDMS can be started either from Eclipse or from inside theAndroid SDK/tools. DDMS also provides a number of other debug related tools.

查阅LogCat的另一种方式是使用Dalvik Debug Monitor Server (DDMS)。 DDMS可以从Eclipse 或在Android SDK/tools开始。 DDMS中还提供了一些其他相关的调试工具。



Unity3D研究院之IOS&Android收集Log文件



开发项目的时候尤其在处理与服务器交互这块,如果服务端程序看不到客户端请求的Log信息,那么无法修改BUG。在Windows上Unity会自动讲Log文件写入本地,但是在IOS和Android上确没有这个功能,所以我想了个办法,把Log信息写在手机的客户端里。把如下脚本挂在任意游戏对象上即可。

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;

public class OutLog : MonoBehaviour 
{
  static List<string> mLines = new List<string>();
  static List<string> mWriteTxt = new List<string>();
  private string outpath;
  void Start () {
    //Application.persistentDataPath Unity中只有这个路径是既可以读也可以写的。
    outpath = Application.persistentDataPath + "/outLog.txt";
    //每次启动客户端删除之前保存的Log
    if (System.IO.File.Exists (outpath)) {
      File.Delete (outpath);
    }
    //在这里做一个Log的监听
    Application.RegisterLogCallback(HandleLog);
    //一个输出
    Debug.Log("xuanyusong");
  }

  void Update () 
  {
    //因为写入文件的操作必须在主线程中完成,所以在Update中哦给你写入文件。
    if(mWriteTxt.Count > 0)
    {
      string[] temp = mWriteTxt.ToArray();
      foreach(string t in temp)
      {
        using(StreamWriter writer = new StreamWriter(outpath, true, Encoding.UTF8))
        {
          writer.WriteLine(t);
        }
        mWriteTxt.Remove(t);
      }
    }
  }

  void HandleLog(string logString, string stackTrace, LogType type)
  {
    mWriteTxt.Add(logString);
    if (type == LogType.Error || type == LogType.Exception) 
    {
      Log(logString);
      Log(stackTrace);
    }
  }

  //这里我把错误的信息保存起来,用来输出在手机屏幕上
  static public void Log (params object[] objs)
  {
    string text = "";
    for (int i = 0; i < objs.Length; ++i)
    {
      if (i == 0)
      {
        text += objs[i].ToString();
      }
      else
      {
        text += ", " + objs[i].ToString();
      }
    }
    if (Application.isPlaying)
    {
      if (mLines.Count > 20) 
      {
        mLines.RemoveAt(0);
      }
      mLines.Add(text);

    }
  }

  void OnGUI()
  {
    GUI.color = Color.red;
    for (int i = 0, imax = mLines.Count; i < imax; ++i)
    {
      GUILayout.Label(mLines[i]);
    }
  }
}

如果在Mac上,可以借助同步推类似的工具来把你的Log文件取出来。

110F8C5D-6F5F-4E5A-A19F-F1F1BD9D1CAC_meitu_1

Android上取法类似。

此时如果客户端报错了怎么办?如果你是在IOS平台,强烈建议把PlayerSetting里面的Script Call Optimization设置成Slow and Safe,这样比如遇到空指针 或者 数组越界这样的错误,程序是不会直接闪退的。(Android上不用设置)这里我创造一个数组越界的错误。

void Start () {
    int []test = new int[1];
    test[2] = 0;
  }

 那么在手机上报错以后,会自动将错误信息的堆栈打印在屏幕上。前提一定要设置Script Call Optimization设置成Slow and Safe,不然就直接闪退了。

屏幕快照 2014-04-23 上午12.16.14


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值