添加一个button控件,当用户点击该控件时,HL2开始拍摄场景,同时记录眼镜传感器相关参数并保存至眼镜中软件路径下的txt中。我这边是保存的相机的位置、三个轴的向量以及欧拉角。

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
using System;#if WINDOWS_UWP
using Windows.Storage;
using Windows.Foundation;
using Windows.System;
using System.Collections.Generic;
using System;
using System.IO;
#endif#if UNITY_EDITOR || UNITY_WSA#endif//https://docs.unity3d.com/ScriptReference/Windows.WebCam.PhotoCapture.html
public class HoloPhotoVideoCapture : MonoBehaviour {String Poslist = String.Empty;String Uplist = String.Empty;String Forwardlist = String.Empty;String Rightlist = String.Empty;String Anglelist = String.Empty;StreamWriter Poswriter;StreamWriter Upwriter;StreamWriter Forwardwriter;StreamWriter Rightwriter;StreamWriter Anglewriter;public Camera MyCamera;void Start () {MyCamera = GameObject.Find("Main Camera").GetComponent<Camera>();}// Update is called once per framevoid FixedUpdate () {Vector3 Cam_Pos = MyCamera.transform.position;String[] strPos = { Cam_Pos[0].ToString() + " ", Cam_Pos[1].ToString() + " ", Cam_Pos[2].ToString() + "\n"};Vector3 Cam_up = NormalVector(MyCamera.transform.up);String[] strUp = { Cam_up[0].ToString() + " ", Cam_up[1].ToString() + " ", Cam_up[2].ToString() + "\n" };Vector3 Cam_forward = NormalVector(MyCamera.transform.forward);String[] strForward = { Cam_forward[0].ToString() + " ", Cam_forward[1].ToString() + " ", Cam_forward[2].ToString() + "\n" };Vector3 Cam_right = NormalVector(MyCamera.transform.right);String[] strRight = { Cam_right[0].ToString() + " ", Cam_right[1].ToString() + " ", Cam_right[2].ToString() + "\n" };Vector3 Cam_Angle = NormalVector(MyCamera.transform.localEulerAngles);String[] strAngle = { Cam_Angle[0].ToString() + " ", Cam_Angle[1].ToString() + " ", Cam_Angle[2].ToString() + "\n" };Poslist = string.Concat(Poslist, strPos[0], strPos[1], strPos[2]);Uplist = string.Concat(Uplist, strUp[0], strUp[1], strUp[2]);Forwardlist = string.Concat(Forwardlist, strForward[0], strForward[1], strForward[2]);Rightlist = string.Concat(Rightlist, strRight[0], strRight[1], strRight[2]);Anglelist = string.Concat(Anglelist, strAngle[0], strAngle[1], strAngle[2]);//print(Uplist);      }Vector3 NormalVector(Vector3 vec){float sum = Mathf.Sqrt(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z);float Nor_X = vec.x / sum;float Nor_Y = vec.y / sum;float Nor_Z = vec.z / sum;Vector3 nor = new Vector3(Nor_X, Nor_Y, Nor_Z);return nor;}public void SavePhoto(){//Texture2D tex = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, true);//tex.Apply();DateTime dateTime = DateTime.Now.ToLocalTime();string strNowTime = string.Format("{0:D}{1:D}{2:D}", dateTime.Hour, dateTime.Minute, dateTime.Second);SavenPic(targetTexture, strNowTime);}public void SavenPic(Texture2D tex, string filename){try{string path = Application.persistentDataPath + "/" + filename+ ".jpg";File.WriteAllBytes(path, tex.EncodeToJPG());print("保存成功!" + path);}catch (System.Exception e){print("保存失败!" + e.Message);}}private IEnumerator LoadPic(string picname){string path = Application.persistentDataPath + "/" + picname + ".jpg" ;if (File.Exists(path)){WWW www = new WWW("file:///" + path);yield return www;//获取TextureTexture2D dynaPic = www.texture;print("读取成功");}else{print("图片不存在!");}}#region 录像
#if UNITY_EDITOR || UNITY_WSAUnityEngine.Windows.WebCam.VideoCapture m_VideoCapture = null;string videofilename;string videofilepath;String TimeStr;public void StopVideo(){if (isRecording){isRecording = false;Poswriter.WriteLine(Poslist);Poswriter.Close();Upwriter.WriteLine(Uplist);Upwriter.Close();Forwardwriter.WriteLine(Forwardlist);Forwardwriter.Close();Rightwriter.WriteLine(Rightlist);Rightwriter.Close();Anglewriter.WriteLine(Anglelist);Anglewriter.Close();print("停止录像...");if(Application.platform==RuntimePlatform.WSAPlayerX86|| Application.platform == RuntimePlatform.WSAPlayerARM)m_VideoCapture.StopRecordingAsync(OnStoppedRecordingVideo);   }#if !UNITY_EDITOR && UNITY_WINRT_10_0string _filename = "video.mp4";var cameraRollFolder = Windows.Storage.KnownFolders.CameraRoll.Path;            File.Move(videofilepath, Path.Combine(cameraRollFolder, _filename));
#endif}public void TakeVideo(){#if UNITY_EDITOR || UNITY_WSAif (!isRecording){DateTime dateTime = DateTime.Now.ToLocalTime();TimeStr = string.Format("{0:D}{1:D}{2:D}", dateTime.Hour, dateTime.Minute, dateTime.Second);string Postxt = Path.Combine(Application.persistentDataPath, "Position_"+ TimeStr + ".txt");string Uptxt = Path.Combine(Application.persistentDataPath, "Updir_" + TimeStr + ".txt");string Forwardtxt = Path.Combine(Application.persistentDataPath, "Forwarddir_" + TimeStr + ".txt");string Righttxt = Path.Combine(Application.persistentDataPath, "Rightdir_" + TimeStr + ".txt");string Angletxt = Path.Combine(Application.persistentDataPath, "Angle_" + TimeStr + ".txt");FileInfo fi = new FileInfo(Postxt);FileInfo fi1 = new FileInfo(Uptxt);FileInfo fi2 = new FileInfo(Forwardtxt);FileInfo fi3 = new FileInfo(Righttxt);FileInfo fi4 = new FileInfo(Angletxt);Poswriter = fi.CreateText();Upwriter = fi1.CreateText();Forwardwriter = fi2.CreateText();Rightwriter = fi3.CreateText();Anglewriter = fi4.CreateText();isRecording = true;print("开始录像...");if (Application.platform == RuntimePlatform.WSAPlayerX86 || Application.platform == RuntimePlatform.WSAPlayerARM)UnityEngine.Windows.WebCam.VideoCapture.CreateAsync(ShowHologram, StartVideoCapture);}
#else#endif}void StartVideoCapture(UnityEngine.Windows.WebCam.VideoCapture videoCapture){if (videoCapture != null){m_VideoCapture = videoCapture;Debug.Log("Created VideoCapture Instance!");Resolution cameraResolution = UnityEngine.Windows.WebCam.VideoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First();float cameraFramerate = UnityEngine.Windows.WebCam.VideoCapture.GetSupportedFrameRatesForResolution(cameraResolution).OrderByDescending((fps) => fps).First();Debug.Log("刷新率:" + cameraFramerate);UnityEngine.Windows.WebCam.CameraParameters cameraParameters = new UnityEngine.Windows.WebCam.CameraParameters();cameraParameters.hologramOpacity = 0.9f;cameraParameters.frameRate = cameraFramerate;cameraParameters.cameraResolutionWidth = cameraResolution.width;cameraParameters.cameraResolutionHeight = cameraResolution.height;cameraParameters.pixelFormat = UnityEngine.Windows.WebCam.CapturePixelFormat.BGRA32;m_VideoCapture.StartVideoModeAsync(cameraParameters,UnityEngine.Windows.WebCam.VideoCapture.AudioState.ApplicationAndMicAudio,OnStartedVideoCaptureMode);}else{Debug.LogError("Failed to create VideoCapture Instance!");}}void OnStartedVideoCaptureMode(UnityEngine.Windows.WebCam.VideoCapture.VideoCaptureResult result){Debug.Log("开始录像模式!");//获取系统时间方法1//string timeStamp = Time.time.ToString().Replace(".", "").Replace(":", "");//string filename = string.Format("TestVideo_{0}.mp4", timeStamp);//方法2DateTime dateTime = DateTime.Now.ToLocalTime();videofilename = "Video" + TimeStr + ".mp4";//string filename = "TestVideo.mp4";videofilepath = Path.Combine(Application.persistentDataPath, videofilename );videofilepath = videofilepath.Replace("/", @"\");m_VideoCapture.StartRecordingAsync(videofilepath, OnStartedRecordingVideo);Debug.Log("videopath:" + videofilepath);}void OnStoppedVideoCaptureMode(UnityEngine.Windows.WebCam.VideoCapture.VideoCaptureResult result){m_VideoCapture.Dispose();m_VideoCapture = null;Debug.Log("停止录像模式!");}void OnStartedRecordingVideo(UnityEngine.Windows.WebCam.VideoCapture.VideoCaptureResult result){Debug.Log("开始录像!");}void OnStoppedRecordingVideo(UnityEngine.Windows.WebCam.VideoCapture.VideoCaptureResult result){Debug.Log("停止录像!");m_VideoCapture.StopVideoModeAsync(OnStoppedVideoCaptureMode);}
#endif#endregion
}

记录:

  1. 获取当前时间作为文件命名:
DateTime dateTime = DateTime.Now.ToLocalTime();
string strNowTime = string.Format("{0:D}{1:D}{2:D}", dateTime.Hour, dateTime.Minute, dateTime.Second);
  1. 将string数据不断地添加到数组中
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
using System;public class Test : MonoBehaviour
{void Start(){string str1 = "Hello Hole  dsuhf sdfhuidff  fhdifh w";string str2 = "World";string newStr;List<String> myList = new List<String>();String stringfromlist = String.Empty;newStr = string.Concat(str1, str2);Debug.Log("Concat方法:" + newStr);newStr = string.Join("^^", str1, str2);Debug.Log("Join方法:" + newStr);newStr = str1 + str2;Debug.Log("连接运算符:" + newStr);myList.Add(str1);myList.Add(str2);for (int i = 0; i < myList.Count; i++){stringfromlist = stringfromlist + myList[i] + " ";}Debug.Log("List<String>方法:" + stringfromlist);}private void Update(){}
}

Hololens 2拍摄视频并同步记录眼镜参数的C#脚本相关推荐

  1. EEG-fMRI同步记录的伪影去除法

    大家好,这里是 "茗创科技" .茗创科技专注于脑科学数据处理,涵盖(EEG/ERP, fMRI,结构像,DTI,ASL, ,FNIRS)等,欢迎留言讨论及转发推荐,也欢迎了解茗创科 ...

  2. 小凡的Linux主机与时间服务器同步记录

    小凡的Linux主机与时间服务器同步记录 导读 我们新安装的Linux主机,如果没有做与互联网服务器时间同步的处理的话,当我们使用date命令的时候,我们就看不到当前的时间,只能看到过去的时间.在我们 ...

  3. 主流AR眼镜参数对比、AR SDK最全功能特性对比

    1.主流AR眼镜参数对比 最近有好多小伙伴分享了干货,成员LalaLand分享了一个目前市场主流AR眼镜参数对比(扫码可查看原文): 1.Hololens 2 2.Magic Leap 1 3.Nre ...

  4. MySQL半同步安装以及参数

    MySQL半同步安装以及参数 基于MySQL5.5 官档地址: Semisynchronous Replication Administrative Interface https://dev.mys ...

  5. git log 查看提交记录,参数:

    git log 查看提交记录,参数: -n (n是一个正整数),查看最近n次的提交信息 $ git log -2 查看最近2次的提交历史记录 -- fileName fileName为任意文件名,查看 ...

  6. 区块链项目/比特币/币圈管理后台/OTC系统管理后台/虚拟数字货币OTC场外交易平台/交易币种管理/币种配置/用户资产/充币记录/划转记录/币种参数/商家管理/rp原型/Axure后台管理原型

    区块链项目/比特币/币圈管理后台/OTC系统管理后台/虚拟数字货币OTC场外交易平台/交易币种管理/币种配置/用户资产/充币记录/划账记录/币种参数/商家管理/rp原型/Axure后台管理原型/币圈O ...

  7. 可以两人同步记录的家庭记账类app有哪些?

    随着人们生活水平的提高,物价的不断波动,家庭财务管理逐渐成为了家庭生活中不可忽视的一部分.有效的家庭财务管理可以帮助家庭成员更好地了解和掌握家庭财务状况,从而更好地规划和分配家庭收支,实现家庭财务健康 ...

  8. 【Groovy】Groovy 脚本调用 ( 命令行执行 Groovy 脚本并传入参数 | 获取 Groovy 脚本执行参数 )

    文章目录 前言 一. 命令行执行 Groovy 脚本并传入参数 二.获取 Groovy 脚本执行参数 前言 在 Groovy 脚本 , Groovy 类 , Java 类中 , 可以调用 Groovy ...

  9. 参数无效_Shell 脚本启动如何传递参数

    我们在日常的脚本开发中,经常会碰到当脚本需要输入参数,然后脚本依据传递进来的参数作为依据,判断执行接下来的脚本逻辑.今天就介绍一下给Shell脚本传递参数进行交互的几种方式. 命令行参数 向脚本传递参 ...

最新文章

  1. Python 四大基本语法
  2. c++后台开发项目_[c/c++后台开发面经系列]4 Zoom面经(含答案)
  3. python装饰器-装饰器
  4. web前端数组塌陷的解决办法
  5. 嵌入式linux系统移植的四大步骤_如何移植开源软件到嵌入式Linux系统
  6. codeforces 1038a(找最长的前k个字母出现相同次数的字符串)水题
  7. 有没有什么方法快速能找到导致软件崩溃的进程_崩溃!电脑突然黑屏无法启动...
  8. 华为荣耀七能升级鸿蒙系统吗,华为鸿蒙系统来了,你知道哪些华为手机荣耀手机可以升级吗?...
  9. 使用spring最简单地读取properties文件中的内容
  10. npm打包项目报错:npm ERR! A complete log of this run can be found in:npm ERR! C:\Users\AppData\Roa...
  11. cad 万能字体_好东西!相见恨晚的50个CAD技巧
  12. 移动Web UI库(H5框架)有哪些,看这里就够了
  13. 「LSTM 之父」亲笔万字长文,只为向世人证明:深度学习不是在母语为英语的地方被发明的...
  14. java处理excel,将xlsx转xls
  15. 微信小程序订阅消息开发教程及代码(java后端实现)
  16. 自然语言处理NLP简介
  17. 百度可观测系列 | 如何构建亿级指标的高可用 TSDB 存储集群?
  18. android 统一推送平台,工信部实验室成立安卓统一推送联盟:推送服务将实现统一...
  19. 微信开发者工具:代码更新后页面未刷新
  20. 【硬件模块】NFC介绍

热门文章

  1. 百择电商:抖音超级福袋怎么使用?
  2. ICCV 2021 放榜!一文看尽10篇论文的开源项目(检测/分割/Transformer等)
  3. 定制开发迅捷进销存圆满完成!
  4. 关于显卡驱动引起的IE异常停止工作解决办法
  5. 3D游戏模型 | 8猴这个功能,即刻让你拥有海飞丝的感觉
  6. 骁龙888发烧根源找到了,三星和台积电的工艺虚标严重所致
  7. uniapp 小程序授权登录
  8. strings.Split
  9. CrashSight异常崩溃管理解决方案
  10. 尝试Houdini的Maya插件