Alt*_*ion 2 c# android unity-game-engine samsung-galaxy-gear virtual-reality
我正在尝试为Samsung Gear VR(与Samsung Galaxy S8)开发一个应用程序,因为它要求我拔出USB电缆才能将手机插入Gear VR设备,所以我无法使用USB调试。
如何将错误和调试消息导出到我可以阅读的文件中,以找出问题所在?
到目前为止,研究表明,Android Unity Player不会保存到其他平台上的日志文件中,而adb是进行USB调试的方法……只有我不能为Gear做到这一点。
如何导出android Unity错误并调试日志到文件?
那还不存在。你必须自己做。
1。订阅Unity Log事件:
Application.logMessageReceived += LogCallback;
Run Code Online (Sandbox Code Playgroud)
2。将它们存储在列表中。当应用程序即将退出时,序列化为json并保存为File.WriteAllBytes。
将其保存到的路径是Application.persistentDataPath。请参阅这篇文章,该文章显示在Unity中的任何构建中可以找到路径的位置。
以下是一个示例脚本,该脚本读取日志并在应用程序即将退出时将其保存。此外,它还可以通过电子邮件向您发送日志数据。这需要这篇文章中的DataSaver课程。请注意,该电子邮件是在下次重新打开应用程序时发送的,而不是在退出时发送的。
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System;
using System.Text;
public class LogSaverAndSender : MonoBehaviour
{
public bool enableSave = true;
public bool enableMailing = true;
public string yourEmail = "fromemail@gmail.com";
public string yourEmailPassword = "password";
public string toEmail = "toemail@gmail.com";
[Serializable]
public struct Logs
{
public string condition;
public string stackTrace;
public LogType type;
public string dateTime;
public Logs(string condition, string stackTrace, LogType type, string dateTime)
{
this.condition = condition;
this.stackTrace = stackTrace;
this.type = type;
this.dateTime = dateTime;
}
}
[Serializable]
public class LogInfo
{
public List<Logs> logInfoList = new List<Logs>();
}
LogInfo logs = new LogInfo();
void OnEnable()
{
//Email last saved log
if (enableMailing)
{
mailLog();
}
//Subscribe to Log Event
Application.logMessageReceived += LogCallback;
}
//Called when there is an exception
void LogCallback(string condition, string stackTrace, LogType type)
{
//Create new Log
Logs logInfo = new Logs(condition, stackTrace, type, DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz"));
//Add it to the List
logs.logInfoList.Add(logInfo);
}
void mailLog()
{
//Read old/last saved log
LogInfo loadedData = DataSaver.loadData<LogInfo>("savelog");
string date = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz");
//Send only if there is something to actually send
if (loadedData != null && loadedData.logInfoList != null
&& loadedData.logInfoList.Count > 0)
{
Debug.Log("Found log to send!");
//Convert to json
string messageToSend = JsonUtility.ToJson(loadedData, true);
string attachmentPath = Path.Combine(Application.persistentDataPath, "data");
attachmentPath = Path.Combine(attachmentPath, "savelog.txt");
//Finally send email
sendMail(yourEmail, yourEmailPassword, toEmail, "Log: " + date, messageToSend, attachmentPath);
//Clear old log
DataSaver.deleteData("savelog");
}
}
void sendMail(string fromEmail, string emaiPassword, string toEmail, string eMailSubject, string eMailBody, string attachmentPath = null)
{
try
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress(fromEmail);
mail.To.Add(toEmail);
mail.Subject = eMailSubject;
mail.Body = eMailBody;
if (attachmentPath != null)
{
System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(attachmentPath);
mail.Attachments.Add(attachment);
}
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Credentials = new System.Net.NetworkCredential(fromEmail, emaiPassword) as ICredentialsByHost;
smtpClient.EnableSsl = true;
ServicePointManager.ServerCertificateValidationCallback =
delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{ return true; };
smtpClient.Send(mail);
}
catch (Exception e) { }
}
void OnDisable()
{
//Un-Subscribe from Log Event
Application.logMessageReceived -= LogCallback;
}
//Save log when focous is lost
void OnApplicationFocus(bool hasFocus)
{
if (!hasFocus)
{
//Save
if (enableSave)
DataSaver.saveData(logs, "savelog");
}
}
//Save log on exit
void OnApplicationPause(bool pauseStatus)
{
if (pauseStatus)
{
//Save
if (enableSave)
DataSaver.saveData(logs, "savelog");
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1629 次 |
| 最近记录: |