If you are on the iOS platform, it is strongly recommended that the script call optimization inside the playersetting be set to slow and Safe, such as errors such as a null pointer or an array out of bounds, the program will not flash directly. (not set on Android) Here I create an array of errors that are out of bounds.
void Start () {
int []test = new int[1];
TEST[2] = 0;
}
Using Unityengine;
Using System.Collections;
Using System.Collections.Generic;
Using System.IO;
Using System.Text;
public class Zyoutlog:monobehaviour
{
static list<string> Mlines = new list<string> ();
static list<string> Mwritetxt = new list<string> ();
private string Outpath;
void Start ()
{
Application.persistentdatapath Unity is the only path that can be read or written.
Outpath = Application.persistentdatapath + "/outlog.txt";
Log saved before each startup client is deleted
if (System.IO.File.Exists (Outpath))
{
File.delete (Outpath);
}
To do a log monitor here.
Application.registerlogcallback (Handlelog);
An output
Debug.Log ("Yufei");
}
void Update ()
{
Because the operation to write to the file must be done in the main thread, in the update, you write the file.
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);
}
}
Here I save the wrong information for output on the phone screen
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]);
}
}
}
IOS and Android--------outlog