Unity在打bundle资源和出包的时候总是最烦人的,因为不知道进度条到底什么时候走完,所以得过会就去看下那坑爹的进度条,多次之后总算被耗到没耐心了,能不能发挥下程序员的能力解决下这烦人的问题呢,希望在Unity编辑器下当我们比如打完bundle或出包完时我们能第一时间收到消息,这样就不会在那漫长的等待过程中被消耗耐心。
结合钉钉的机器人,我们就能解决这个问题,具体开发文档可以前往钉钉官方查看,下面就是c#代码
using UnityEngine;
using System.IO;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
public class DingTalkHelper
{
private const string WEB_HOOK = @"https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxxxxxxxx"; //设置自己创建的机器人地址
/// <summary>
/// 钉钉机器人发送通知消息
/// </summary>
/// <param name="msg"></param>
public static void Notify(string msg)
{
string textMsg = "{ \"msgtype\": \"text\", \"text\": {\"content\": \"" + msg + "\"}}";
string s = Post(textMsg, null);
Debug.Log("Post return:" + s);
}
#region Post
/// <summary>
/// 以Post方式提交命令
/// </summary>
/// <param name="apiurl">请求的URL</param>
/// <param name="jsonstring">请求的json参数</param>
/// <param name="headers">请求头的key-value字典</param>
private static string Post(string jsonstring, Dictionary<string, string> headers = null)
{
//远程证书无效
ServicePointManager.ServerCertificateValidationCallback =
delegate (object s, X509Certificate certificate,
X509Chain chain, SslPolicyErrors sslPolicyErrors)
{ return true; };
WebRequest request = WebRequest.Create(WEB_HOOK);
request.Method = "POST";
request.ContentType = "application/json";
if (headers != null)
{
foreach (var keyValue in headers)
{
if (keyValue.Key == "Content-Type")
{
request.ContentType = keyValue.Value;
continue;
}
request.Headers.Add(keyValue.Key, keyValue.Value);
}
}
if (string.IsNullOrEmpty(jsonstring))
{
request.ContentLength = 0;
}
else
{
byte[] bs = Encoding.UTF8.GetBytes(jsonstring);
request.ContentLength = bs.Length;
Stream newStream = request.GetRequestStream();
newStream.Write(bs, 0, bs.Length);
newStream.Close();
}
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
Encoding encode = Encoding.UTF8;
StreamReader reader = new StreamReader(stream, encode);
string resultJson = reader.ReadToEnd();
return resultJson;
}
#endregion
}
在需要的地方调用 Notify 接口,最终效果如下