共计 3674 个字符,预计需要花费 10 分钟才能阅读完成。
Unity IPostprocessBuildWithReport
Unity IPostprocessBuildWithReport 是 Unity 引擎中的一个十分有用的性能,它能够让开发者在构建我的项目后主动执行一些操作,并且能够获取构建报告。这个性能能够帮忙开发进步工作效率,缩小手动操作的工夫和错误率。在本文中,咱们将介绍 Unity IPostprocessBuildWithReport 的应用办法,并提供三个应用例子,帮忙读者更好地了解这个。
Unity IPostprocessBuildWithReport 的应用办法
Unity IPostprocessBuildWithReport 是一个接口,它蕴含一个名为 PostprocessBuild 办法。在这个办法中,咱们能够编写咱们须要执行的操作,并且能够获取构建报告。下是一个简略的例子,演示了如何在构建我的项目后主动关上目录,并获取构建报:
using UnityEditor.Build.Reporting; | |
using UnityEngine; | |
using System.Diagnostics; | |
public class BuildHandler : IPostprocessBuildWithReport | |
{public int callbackOrder { get { return 0;} } | |
public void OnPostprocessBuild(BuildReport report) | |
{ | |
string outputPath = report.summary.outputPath; | |
Process.Start(outputPath); | |
Debug.Log(report.summary.result.ToString()); | |
} | |
} |
在这个例子中,咱们实现了 IPostprocessBuildWithReport 接口,并重写了它的 OnPostprocessBuild 办法。在这个办法中,首先获取构建报告的门路,而后应用 Process.Start 办法关上这个输入目录。最初,咱们应用 Debug.Log 办法输入构建报告的后果。
应用例子
上面是三个应用 Unity IPostprocessBuildWithReport 的例子,每个例子都提供了具体的实现。
例子 1:主动上传构建后果
在这个例子中,咱们将演示如何在构建我的项目后主动上传构建后果,并获取构报告。
using UnityEditor.Build.Reporting; | |
using UnityEngine; | |
using System.Net; | |
using System.IO; | |
public class BuildHandler : IPostprocessBuildWithReport | |
{public int callbackOrder { get { return 0;} } | |
public void OnPostprocessBuild(BuildReport report) | |
{ | |
string outputPath = report.summary.outputPath; | |
string url = "http://myserver.com/upload.php"; | |
WebClient client = new WebClient(); | |
client.UploadFile(url, outputPath); | |
Debug.Log(report.summary.result.ToString()); | |
} | |
} |
在这个例子中,咱们实现了 IPostprocessBuildWithReport 接口,并重写了它的 OnPostprocessBuild 办法。在这个办法中,咱们首先获取构建报告的输入门路,而后指定上传的 URL,创立一个 WebClient 对象,并调用它的 UploadFile 办法,将构建后果上传到指定的 URL。最初,咱们应用 Debug.Log 办法输入建报告的后果。
子 2:主动发送邮件在这个例子中,咱们将演示如何在构建我的项目后主动发送邮件,并获取构建报告。
using UnityEditor.Build.Reporting; | |
using UnityEngine; | |
using System.Net; | |
using System.Net.Mail; | |
public class BuildHandler : IPostprocessBuildWithReport | |
{public int callbackOrder { get { return 0;} } | |
public void OnPostprocessBuild(BuildReport report) | |
{ | |
string outputPath = report.summary.outputPath; | |
string subject = "Build completed successfully!"; | |
string body = "The build has been completed successfully."; | |
string from = "myemail@gmail.com"; | |
string to = "youremail@gmail.com"; | |
string password = "mypassword"; | |
SmtpClient client = new SmtpClient("smtp.gmail.com", 587); | |
client.EnableSsl = true; | |
client.Credentials = new NetworkCredential(from, password); | |
MailMessage message = new MailMessage(from, to, subject, body); | |
Attachment attachment = new Attachment(outputPath); | |
message.Attachments.Add(attachment); | |
client.Send(message); | |
Debug.Log(report.summary.result.ToString()); | |
} | |
} |
在这个例子中,咱们实现了 IPostprocessBuildWithReport 接口,并重写了它的 OnPostprocessBuild 办法。在这个办法中,咱们首先获取构建报告的输入门路,而后指定邮件的主题和注释。接,咱们指定发件人和收件人的邮箱地址,以及发件人的明码。而后咱们创立一个 SmtpClient 对象,并设置它的服务器和端口号。接着,咱们创立一个 MailMessage 对象,并设置它的件人、收件、主题和注释。最初,咱们创立一个 Attachment 对象,并将构建后果作为附件增加到邮件中。而后,咱们调用 SmtpClient 的 Send 办法,将邮件进来。最初,咱们应用 Debug.Log 办法输入构建报告的后果。
例子 3:主动上传到 FTP 服务器
在这个子中,咱们将演示如何在构建我的项目后主动上传到 FTP 服务器,并获取构建报告。
using UnityEditor.Build.Reporting; | |
UnityEngine; | |
using System.Net; | |
public class BuildHandler : IPostprocessBuildWithReport | |
{public int callbackOrder { get { return 0;} } | |
public void OnPostprocessBuild(BuildReport report) | |
{ | |
string outputPath = report.summary.outputPath; | |
string ftpUrl = "ftp://myserver.com/"; | |
string ftpUser = "myusername"; | |
string ftpPassword = "mypassword"; | |
WebClient client = new WebClient(); | |
client.Credentials = new NetworkCredential(ftpUser, ftpPassword); | |
client.UploadFile(ftpUrl + "build.zip", outputPath); | |
Debug.Log(report.summary.result.ToString()); | |
} | |
} |
在这个例子中,咱们实现了 IPostprocessBuildReport 接口,并重写了它的 OnPostprocessBuild 办法。在这个办法中,咱们首先获取构建报告的输入门路,而后指定 FTP 服务器的、用户名和明码。接着,咱们创立一个 Client 对象设置它的 Credentials 属性为指定的用户名和明码。而后,咱们调 WebClientUploadFile 办法,将构后果上传到 FTP 服务器上。最初,咱们应用 Debug.Log 办法输入构建报告的后果。
论断
Unity IPostprocessBuildWithReport 是一个十分有用性能,能够助发者进步作效率,缩小手动操作的和错误率。在本文中,咱们介了 Unity IPostprocessBuildReport 的应用办法,并供了三个例子,帮忙读者更好地解这个性能。心愿这篇文章对读者有所帮忙。
本文由博客一文多发平台 OpenWrite 公布!