在Unity / C#中创建一个speedrun计时器

时间:2018-03-07 17:40:09

标签: c# unity3d

我想创建一个计数器。此计时器必须非常精确,因为它代表了速度调节器的当前级别计时器。

我测试了一些方法..

使用 Time.deltaTime

float currentTime = 0;

private void Update()
{
    currentTime += Time.deltaTime;
}

使用 Time.timeSinceLevelLoad

    float startTime;

    private void Start()
    {
        startTime = Time.timeSinceLevelLoad;
    }

    private void Update()
    {
        float currentTime = Time.timeSinceLevelLoad - startTime;
    }

使用 DateTime.Now

DateTime startTime;

private void Start()
{
    startTime = DateTime.Now;
}

private void Update()
{
    TimeSpan currentTime = DateTime.Now - startTime;
}

使用 System.Diagnostics.Stopwatch

private Stopwatch watch;

private void Start()
{
    watch = new Stopwatch();
    watch.Start();
}

private void Update()
{
    TimeSpan currentTime = watch.Elapsed;
}

哪一个可能是最好的?

1 个答案:

答案 0 :(得分:1)

最后一个解决方案似乎是最好的,但我会将mvn versions:set -DnewVersion=xx移动到类属性,以便您可以在其他位置访问它。

entry:{
    'dist/folder1/video.mp4': 'src/folder1/video.mp4',
    'dist/folder2/otherfolder/video2.mp4': 'src/folder2/otherfolder/video2.mp4'
}

或者您可以使用StopWatch课程。这为您提供了一些可供选择的选项。像这样:

      startRecording() {
    console.log("Started Recording");
    this.recording = true;
    this.file.createFile(this.file.tempDirectory, 'my_file.m4a', true).then(() => {
      const audio: MediaObject = this.media.create(this.file.tempDirectory.replace(/^file:\/\//, '') + 'my_file.m4a');
      console.log("Audio assigned to constant audio media object");
      console.log(audio);
      this.audio = audio;
      console.log("Audio assigned to this.audio media object");
      console.log(this.audio);
      this.audio.startRecord();
      this.listenToAudioEvents();
      window.setTimeout(() => {
        if (this.recording) this.stopRecording();
      }, 10000);
    });
  }

  listenToAudioEvents() {
    this.audio.onStatusUpdate.subscribe(status => {
      console.log("Status of this.audio updated");
      console.log(status);
      if (status == 4 && this.playingAudio) {
        console.log("Time to stop playback")
        this.stopPlayback();
      }
    });
  }

  stopRecording() {
    this.audio.stopRecord();
    console.log("Stopped Recording");
    console.log(this.audio);
    this.recording = false;
    this.audioReady = true;
    this.audio.getDuration();
    console.log("Audio Duration: " + this.duration);
    console.log("Audio Duration Property: " + this.audio.duration);
  }

  playAudio() {
    console.log("Playing Audio");
    this.playingAudio = true;
    this.audio.play();
  }

  stopPlayback() {
    console.log("Stopping Playback");
    this.playingAudio = false;
    this.audio.stop();
  }

  uploadAudio() {
    console.log("Uploading record");
    this.storeRecord().subscribe((downloadURL) => {
      console.log("Finished storing record");
      console.log("Download URL is " + downloadURL);
      this.audioURL = downloadURL;
      this.audioURLReady = true;
    });
  }

  storeRecord() {
    return Observable.create((observer) => {
      console.log('Saving record');
      const filePath = `${this.file.tempDirectory}my_file.m4a`;
      console.log("Path to record is " + filePath);
      const readFile: any = window['resolveLocalFileSystemURL'];
      return readFile(filePath, (fileEntry) => {
        return fileEntry.file((file) => {
          const fileReader = new FileReader();
          fileReader.onloadend = (result: any) => {
            let arrayBuffer = result.target.result;
            let blob = new Blob([new Uint8Array(arrayBuffer)], { type: 'audio/m4a' });
            console.log("Blob is ");
            console.log(blob);
            var storageRef = firebase.storage().ref('content/' + this.firebase.user.uid + '/my-file.m4a');
            console.log("Storage reference is " + storageRef);
            var uploadTask = storageRef.put(blob);
            console.log('Upload started:');
            uploadTask.on('state_changed', (snapshot) => {
              console.log("state changed");
              let percent = uploadTask.snapshot.bytesTransferred / uploadTask.snapshot.totalBytes * 100;
              console.log(percent + "% done");
            }, (e) => {
              console.error(e);
              observer.error(e);
            }, () => {
              var downloadURL = uploadTask.snapshot.downloadURL;
              console.log('Storage Download URL:' + downloadURL);
              observer.next(downloadURL);
            });
          };
          fileReader.onerror = (e: any) => {
            console.error(e);
            observer.error(e);
          };
          fileReader.readAsArrayBuffer(file);
        }, (e) => {
          console.error(e);
          observer.error(e);
        });
      }, (e) => {
        console.error(e);
        observer.error(e);
      });
    });
  }

  downloadAudio() {
    console.log("Downloading Audio")
    const fileTransfer: FileTransferObject = this.fileTransfer.create();
    var destPath = (cordova.file.externalDataDirectory || cordova.file.dataDirectory) + "my_file.m4a"
    fileTransfer.download(this.audioURL, destPath, ).then((entry) => {
      let rawAudioURI = entry.toURL();
      this.audioURI = rawAudioURI.replace(/^file:\/\//, '/private');
      this.audioURIReady = true;
      console.log("Audio URI: " + this.audioURI);
    }, (error) => {
      console.error(error);
    });
  }

  playAudioURI() {
    console.log("Playing AudioURI");
    let downloadedAudio: MediaObject = this.media.create(this.audioURI);
    console.log("Downloaded audio: " + downloadedAudio);
    downloadedAudio.play();
  }
}