之前初中的时候有通过iapp写过一些软件 那会也是使用蓝奏云进行更新与进行公告的 那会蓝奏云也提供下载API 不需要验证机器人便能够可以直接下载 然后忽然想到为什么不可以使用蓝奏云给自己的在unity中写的游戏添加自动更新和公告呢 然后就花掉了一上午时间成功实现了这个功能 那么下面开始我们的教程(。•̀ᴗ-)✧
首先需要注册一个蓝奏云的账号 并在其中添加一个你的游戏名的文件夹 在其中填写你的版本号与简介 我是比较直接酱紫
截取方式是使用split方法通过两个特殊字符快速截取简介 如果觉得会影响也可以通过网页内的标识符多截取内容也是可以的 那如果不想要改代码的话 可以使用与我一样的格式(π$"在此填写你的版本号"$"在此填写你的公告"π)填入你的简介
接下来晒出脚本 你需要修改url变量为你自己的蓝奏云地址 之后等待8秒会打印出获取网页中的版本号与网页 你可以再修改一下getHtmlStr方法 以用于自定义你的游戏
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Diagnostics.Tracing;
using UnityEngine.Rendering.Universal;
using System.Web;
using System.IO;
using System.Net;
using System;
using System.Threading;
//储存动画集
public class SelfUpdata : MonoBehaviour
{
[Header("服务器更新")]
public string url = "https://2035851934.lanzoub.com/b00t775xc";
public GameObject Loading_Text; //修改加载的文本 (不重要)
[SerializeField]
private Thread GetUrlThread; //获取网页的线程
public string UrlC; //当加载网页完成后的存储内容
public string UrlVesion; //截取获得的版本号
public string UrlContent; //截取获得的网页
private void Start()
{
GetUrlThread = new Thread(UrlThread);
GetUrlThread.Start();
Invoke("GetHtmlStr", 8.5f);
}
#region 获取在线服务器
public void UrlThread()
{
string encoding = "Default";
string htmlStr = "";
try
{
if (!String.IsNullOrEmpty(url))
{
WebRequest request = WebRequest.Create(url); //实例化WebRequest对象
WebResponse response = request.GetResponse(); //创建WebResponse对象
Stream datastream = response.GetResponseStream(); //创建流对象
Encoding ec = Encoding.Default;
if (encoding == "UTF8")
{
ec = Encoding.UTF8;
}
else if (encoding == "Default")
{
ec = Encoding.Default;
}
StreamReader reader = new StreamReader(datastream, ec);
htmlStr = reader.ReadToEnd(); //读取网页内容
UrlC = htmlStr;
reader.Close();
datastream.Close();
response.Close();
}
}
catch { }
}
public void GetHtmlStr()
{
string htmlStr = UrlC;
if (Application.internetReachability == NetworkReachability.NotReachable) //如果没网则执行
{
Debug.Log("没有网络");
}
else if(htmlStr == ""||htmlStr == null) //如果有网 但是内容为空
{
Debug.Log("连接超时");
}
else
{
string[] sArray = htmlStr.Split(new char[2] { 'π', 'π' }); //获取简介
string[] arrat2 = sArray[1].Split(new char[2] { '$', '$' }); //分离简介 [1]为版本 [2]为公告
string[] Edition = arrat2[1].Split(new char[1] { '.' });
string[] CurEdition = Application.version.Split(new char[1] { '.' });
if (int.Parse(Edition[1]) > int.Parse(CurEdition[1]))
{
Debug.Log("发现最新的版本"+arrat2[1]+"公告:"+arrat[2]);
}
else if (int.Parse(Edition[1]) == int.Parse(CurEdition[1]))
{
Debug.Log("当前为最新的版本"+"公告:"+arrat[2]);
}
else
{
Debug.Log("你的版本比最新版本更新");
}
GetUrlThread.Abort();
}
}
public void Gotoupdata() //按下更新
{
Application.OpenURL(url);
}
#endregion
}
觉得有用的话请关注我的bilibili账号猫先生不是鱼先生喔 超谢谢各位小伙伴喔(。•̀ᴗ-)✧
下一期《 超多功能的适用android和ios的RTS游戏上帝视角相机 》