繁体   English   中英

在 HoloLens 1.MRTK 2.3 和 unity 2018 上将数据写入文本文件以进行构建的 Microsoft 标准方法是什么

[英]What is Microsoft standard approach for writing data into text files on HoloLens 1.MRTK 2.3 and unity 2018 to make a build

    using System.Collections;
using UnityEngine;
using System.IO;
using System.Text;
using UnityEngine.UI;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System;



//#if WINDOWS_UWP
//using Windows.Storage;
//using Windows.System;
//using System.Threading.Tasks;
//using Windows.Storage.Streams;
//#endif


public class StartButton : MonoBehaviour
{
    public Text secondsdisplay;
    public float displaytime = 5;
    public float nseconds = 1;
    public float timer = 0;
    public float percent;
    public int counter = 0;
    public StreamWriter sw1;
    public Text displaynseconds;
    public float startTime, stopTime;
    public GameObject myboard;
    public string timeData;
    public string ClickRespTime;
    //private static bool firstSave = true;
    //private string saveInformation = "";
    //private static string fileName = "/Responsetime.txt";



//#if WINDOWS_UWP
//        Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
//        Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
//#endif


    Vector3[] positionArray = new[] { new Vector3(0.6f, 0.009f, 0.5f), new Vector3(0.6f, 0.239f, 0.5f), new Vector3(1.027f, 0.239f, 0.5f), new Vector3(0.618f, 0.239f, 0.5f), new Vector3(0.6f, 0.009f, 0.5f), new Vector3(0.6f, 0.239f, 0.5f), new Vector3(1.027f, 0.239f, 0.5f), new Vector3(0.618f, 0.239f, 0.5f), new Vector3(0.6f, 0.009f, 0.5f), new Vector3(0.6f, 0.239f, 0.5f), new Vector3(0.6f, 0.009f, 0.5f), new Vector3(0.6f, 0.239f, 0.5f), new Vector3(1.027f, 0.239f, 0.5f), new Vector3(0.618f, 0.239f, 0.5f), new Vector3(0.6f, 0.009f, 0.5f), new Vector3(0.6f, 0.239f, 0.5f), new Vector3(1.027f, 0.239f, 0.5f), new Vector3(0.618f, 0.239f, 0.5f), new Vector3(0.6f, 0.009f, 0.5f), new Vector3(0.6f, 0.239f, 0.5f), new Vector3(0.6f, 0.239f, 0.5f), new Vector3(1.027f, 0.239f, 0.5f), new Vector3(0.618f, 0.239f, 0.5f), new Vector3(0.6f, 0.009f, 0.5f), new Vector3(0.6f, 0.239f, 0.5f) };



    void Start()
    {

       // string path = Application.persistentDataPath + "/Responsetime.txt";
        
       //StreamWriter sw1 = new StreamWriter(path);
       // Debug.Log("file created" + File.Exists(path).ToString());
       // if (!File.Exists(path))
       // {
       //     sw1 = File.CreateText(path);
       // }

        //StringBuilder sb = new StringBuilder();
     
        timer = displaytime;
    }



    IEnumerator fivesecondtime()
    {


        //Debug.Log("Started Coroutine at timestamp : " + Time.time);
        yield return new WaitForSeconds(10.0f);
        //Debug.Log("Finished Coroutine at timestamp : " + Time.time);
        XButtonPressed();


    }

    public void hideboard()
    {

        myboard.SetActive(false);

    }
    public void textdisplay()
    {
        secondsdisplay.text =  ClickRespTime.ToString();
    }



    void Update()
    {
        //Debug.Log("upadte checking inside");
       
        if (counter == 20)
        {
           // sw1.WriteLine(timeData);
            //sw.Close();
            //sw.Flush();
            StopCoroutine(fivesecondtime());


#if UNITY_EDITOR
            UnityEditor.EditorApplication.isPlaying = false;
#else
         Application.Quit();
#endif

        }
    }


    public void starButtonPressed()
    {

        startTime = Time.time;
        StartCoroutine(fivesecondtime());

    }

//#if WINDOWS_UWP
//    async void WriteData()
//    {
//        if (firstSave){
//        StorageFile sampleFile = await localFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
//        await FileIO.AppendTextAsync(sampleFile, saveInformation + "\r\n");
//        firstSave = false;
//        }
//    else{
//        StorageFile sampleFile = await localFolder.CreateFileAsync(fileName, CreationCollisionOption.OpenIfExists);
//        await FileIO.AppendTextAsync(sampleFile, saveInformation + "\r\n");
//    }
//    }
//#endif

    public void XButtonPressed()
    {
      //  if (counter == 10)
           // counter = 0;
       // Debug.Log("time of the update \n" + timeData);
        StopCoroutine(fivesecondtime());
        stopTime = Time.time;

        //saveInformation = counter.ToString() + " " + (stopTime - startTime).ToString();

//#if WINDOWS_UWP
//        WriteData();
//#endif

        transform.position = positionArray[counter];
      //  Debug.Log("counter before : " + counter);

        counter++;
        Debug.Log("new counter  : " + counter.ToString());
        //timeData = "counter after," + counter.ToString();

        //timeData = timeData + "Time elapsed = " + (stopTime - startTime) + "\n";
        //ClickRespTime = timeData;

        //   Debug.Log("Time elapsed = " + (stopTime - startTime));

        Debug.Log("time of the update \n" + timeData);
        startTime = Time.time;
        // write lines 
       // sw1.WriteLine(timeData);

//#if WINDOWS_UWP
//        WriteData();
//#endif

        StartCoroutine(fivesecondtime());

    }


}

将数据写入和保存文件到 hololens 设备的标准程序是什么? 我使用了两种不同的方法,但它不起作用。 此外,我在 StackOverflow 上尝试了 2 3 种不同的推荐方法,但在我的情况下它不起作用。 当我在 HL1 上运行应用程序时,只创建了文件,但没有将我的数据写入其中。

我想知道是否有任何其他方法可以使用 MRTK 2.3 将数据存储到 unity2018 文件中。 在不同的论坛上,我检查了没有专门针对 HL1 的标准信息。

FileIO.AppendTextAsync在 HoloLens 上应该可以正常工作,但我发现您的代码中有一个错误:变量“fileName”的字符串无效,因为它不能包含字符“/”。

实际上,在使用 UWP StorageFile API 时,不需要手动组合路径字符串,因此我们不需要关心路径组件分隔符。 查看此文档: 使用文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM