环境
unity版本:2022.3.6f1
vuforia版本:2023年8月下载版本,该程序PC端使用,但vuforia不支持PC端,只能发布UWP让PC端打开
visual stadio 2019;
一、Unity发布设置
1.打开unity平台设置
左上角导航栏File——Build settings...
2. 发布平台设置
3.设置发布软件名称及图标
4.设置UWP设备和权限
capabilities是权限,因为程序中有http服务端程序所以需要通信权限,由于不知道是哪一个所以将全部都勾上
Supported Device Famillies够需要的就好,设置完成后关闭该页面,可以再打开,发现设置已经更改成功,打开Build settings,点击Build,输出文件。
二、 Visual studio部署
1.打开生成文件
2.设置
图示红框中的本地计算机绿色箭头运行,等待一段时间,可能比较久,看项目大小。显示发布成功
3.打开主菜单,生成桌面快捷方式
选择固定到“开始”,将开始菜单栏的图标拖动即可生成桌面快捷方式
三、 unity搭建http服务器代码示例
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Net;
using System.Text;
public class HttpServer : MonoBehaviour
{
private static HttpServer _instance;
private HttpServer() { }
public static HttpServer Instance
{
get
{
if(_instance == null)
{
_instance = FindObjectOfType<HttpServer>();
if(_instance == null)
{
GameObject httpserverObject = new GameObject();
_instance = httpserverObject.AddComponent<HttpServer>();
DontDestroyOnLoad(httpserverObject);
}
}
return _instance; ;
}
}
static HttpListener httpobj;
// Start is called before the first frame update
void Start()
{
//提供一个简单的、可通过编程方式控制的 HTTP 协议侦听器。此类不能被继承。
if (GlobalVariables.instance.httpseverenable == false)
{
httpobj = new HttpListener();
//定义url及端口号,通常设置为配置文件
httpobj.Prefixes.Add("http://169.254.238.107:8082/"/*"http://+:8080/"*/);
//启动监听器
httpobj.Start();
//异步监听客户端请求,当客户端的网络请求到来时会自动执行Result委托
//该委托没有返回值,有一个IAsyncResult接口的参数,可通过该参数获取context对象
httpobj.BeginGetContext(Result, null);
Debug.Log($"服务端初始化完毕,正在等待客户端请求,时间:{DateTime.Now.ToString()}\r\n");
DontDestroyOnLoad(gameObject);
GlobalVariables.instance.httpseverenable = true;
}
}
private static void Result(IAsyncResult ar)
{
//当接收到请求后程序流会走到这里
//继续异步监听
httpobj.BeginGetContext(Result, null);
var guid = Guid.NewGuid().ToString();
// Debug.Log($"接到新的请求:{guid},时间:{DateTime.Now.ToString()}");
//获得context对象
var context = httpobj.EndGetContext(ar);
var request = context.Request;
var response = context.Response;
//s..s 如果是js的ajax请求,还可以设置跨域的ip地址与参数
//context.Response.AppendHeader("Access-Control-Allow-Origin", "*");//后台跨域请求,通常设置为配置文件
//context.Response.AppendHeader("Access-Control-Allow-Headers", "ID,PW");//后台跨域参数设置,通常设置为配置文件
//context.Response.AppendHeader("Access-Control-Allow-Method", "post");//后台跨域请求设置,通常设置为配置文件
context.Response.ContentType = "text/plain;charset=UTF-8";//告诉客户端返回的ContentType类型为纯文本格式,编码为UTF-8
context.Response.AddHeader("Content-type", "text/plain");//添加响应头信息
context.Response.ContentEncoding = Encoding.UTF8;
string returnObj = null;//定义返回客户端的信息
if (request.HttpMethod == "POST" && request.InputStream != null)
{
//处理客户端发送的请求并返回处理信息
returnObj = HandleRequest(request, response);
}
else
{
returnObj = $"不是post请求或者传过来的数据为空";
}
Debug.Log(returnObj);
var returnByteArr = Encoding.UTF8.GetBytes(returnObj);//设置客户端返回信息的编码
try
{
using (var stream = response.OutputStream)
{
//把处理信息返回到客户端
stream.Write(returnByteArr, 0, returnByteArr.Length);
}
}
catch (Exception ex)
{
Debug.Log($"网络蹦了:{ex.ToString()}");
}
// Debug.Log($"请求处理完成:sadjashd{guid},时间:{ DateTime.Now.ToString()}\r\n");
// Debug.Log($"总字符长度2为xZxXZxZxZxzx{GlobalVariables.instance.FBG1wavelist[0]}{ DateTime.Now.ToString()}\r\n");
}
private static string HandleRequest(HttpListenerRequest request, HttpListenerResponse response)
{
string data = null;
try
{
var byteList = new List<byte>();
var byteArr = new byte[2048];
int readLen = 0;
int len = 0;
//接收客户端传过来的数据并转成字符串类型
do
{
readLen = request.InputStream.Read(byteArr, 0, byteArr.Length);
len += readLen;
byteList.AddRange(byteArr);
} while (readLen != 0);
data = Encoding.UTF8.GetString(byteList.ToArray(), 0, len);
string[] FBGdatalist = data.Split('{');//{将data字符串分为频率1000,对应1000个时间对应的字符串
string fbgdata = FBGdatalist[1].Substring(21, 66);//取其中21到66为前三个传感器数据
// Debug.Log($"jasn数据为:{data}");
string[] fbgnumberswavelength = fbgdata.Split(',');
string[] fbg10data = new string[50];
string[] temperaturefbgdata = new string[50];
//Debug.Log($"{ FBGdatalist[1]}jasn数据为:{data}");
GlobalVariables.instance.straindisplay = 0;
for (int i = 0; i < 50; i++)
{
fbg10data[i] = FBGdatalist[i * 20 + 1];
string fbgfirstthreedata = fbg10data[i].Substring(21, 66);
string[] temperaturefbgdatasplit= fbgfirstthreedata.Split(',');
temperaturefbgdata[i] = temperaturefbgdatasplit[0];
if (double.TryParse(temperaturefbgdata[i].Substring(0, 10), out double inneresult))
{
if(inneresult >= 1545 && inneresult <= 1555)//该应变传感器取值应该在1545到1555之间
{
GlobalVariables.instance.FBG1wavelist[i] = inneresult;
}
//将转换后的double类型数值赋给全局变量
// Debug.Log(GlobalVariables.instance.FBG[0]);
// Debug.Log($"总字符长度为xZxXZxZxZxzx{FBGdatalist[0]}");
// Debug.Log($"数据为:{GlobalVariables.instance.FBG1wavelist[i]}\r\n");
Debug.Log($"TryParse数据为:{temperaturefbgdata[i]}");
}
else
{
Debug.Log($"转换失败");
}
}
if (double.TryParse(fbgnumberswavelength[0].Substring(0,10), out double result))
{
GlobalVariables.instance.FBG1 = result;//将转换后的double类型数值赋给全局变量
// Debug.Log(GlobalVariables.instance.FBG[0]);
//Debug.Log($"总字符长度为3{GlobalVariables.instance.FBG1 }");
}
else
{
Debug.Log($"转换失败");
}
if (double.TryParse(fbgnumberswavelength[1].Substring(0, 10), out double result2))
{
GlobalVariables.instance.FBG2 = result2;//将转换后的double类型数值赋给全局变量
// Debug.Log(GlobalVariables.instance.FBG[0]);
}
else
{
Debug.Log($"转换失败");
}
if (double.TryParse(fbgnumberswavelength[2].Substring(0, 10), out double result3))
{
GlobalVariables.instance.FBG3 = result3;//将转换后的double类型数值赋给全局变量
// Debug.Log(GlobalVariables.instance.FBG[0]);
}
else
{
Debug.Log($"转换失败");
}
//获取得到数据data可以进行其他操作
// Debug.Log($"jasn数据为:{fbgnumberswavelength[0],fbgnumberswavelength[1],fbgnumberswavelength[1]}");
// Debug.Log($"传感器数据为:{fbgnumberswavelength[0]},{fbgnumberswavelength[1]},{fbgnumberswavelength[2]}");
}
catch (Exception ex)
{
response.StatusDescription = "404";
response.StatusCode = 404;
Debug.Log($"在接收数据时发生错误:{ex.ToString()}");
return $"在接收数据时发生错误:{ex.ToString()}";//把服务端错误信息直接返回可能会导致信息不安全,此处仅供参考
}
response.StatusDescription = "200";//获取或设置返回给客户端的 HTTP 状态代码的文本说明。
response.StatusCode = 200;// 获取或设置返回给客户端的 HTTP 状态代码。
//Debug.Log($"接收数据完成:{data.Trim()},时间:{DateTime.Now.ToString()}");
GlobalVariables.instance.connect = true;
return $"接收数据完成";
}
// Update is called once per frame
void Update()
{
}
}