我正在创建一个使用从Random.org提取的数据的脚本。我无法弄清楚如何在不重复脚本的文件创建部分的情况下使其工作。我想要做的是创建一个新文件,如果文件不存在或如果文件> 24小时。检查文件的存在和修改日期
using UnityEngine;
using System.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using TrueRNG;
public class Player : MonoBehaviour {
private Transform TS;
public int pspeed = 20;
public static int playerLives = 3;
public static int score = 0;
float timer = 0f;
string trgd = "R:\\trrnd.txt";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
public GameObject LaserFab; //variable to reference prefab.
// Use this for initialization
void Start() {
TS = transform;
TS.position = new Vector3(0, -14.78957f, 0); //Spawn point
if (File.Exists(trgd))
{
print("The file exists! Checking time since creation...");
//Get date from existing file.
string[] lines = File.ReadAllLines(trgd);
print(lines[0]);
DateTime genDate = Convert.ToDateTime(lines[0]);
//Is existing data more than 1 day old?
if (genDate.AddDays(1) > DateTime.UtcNow)
{
print("Random data is old, creating new file...");
}
//Append the current date and time to the beginning of a new file
File.AppendAllText(trgd, DateTime.UtcNow.ToString("G") + Environment.NewLine);
while (!TrueRandom.RandInternet.IsAvailableUniform())
{
print("Waiting for True RNG/Random.org data...");
} //End while loop
for (int i = 0; i < 10; i++)
{
double rn = TrueRandom.RandInternet.Uniform();
//Append all the Random.org-pulled data to a single String
sb.AppendLine(rn.ToString());
} //End for writing loop
//Write the "truly" random String to a file
File.WriteAllText(trgd, sb.ToString());
} //End date check if
} //End Start
// Update is called once per frame
void Update() {
TS.Translate(Vector3.right * Input.GetAxis("Horizontal") * pspeed * Time.deltaTime);
if (TS.position.x>11.25f || TS.position.x<-11.25f) {
TS.position = new Vector3 (TS.position.x*-1, TS.position.y, TS.position.z);
}
//Fire a laser with the Spacebar.
if (Input.GetKeyDown("space")) {
//Get pseudo-randomized laser position
//This works but is not "truly" random. The random data should be pulled from the trgd file
//float laspo = TrueRandom.RandInternet.Uniform() > 0.5f ? -1.917277f : 1.917277f;
//Set Laser position
Vector3 position = new Vector3(TS.position.x/*+laspo*/, TS.position.y+3.50f, TS.position.z);
//Fire projectile.
Instantiate(Resources.Load("LaserFab"), position, Quaternion.identity);
}
if (Time.time - timer > 0.4f) {
renderer.enabled = true;
}
print ("Lives remaining: " + playerLives + " Score: " + score + " Current playing time: " + "0");
}
void OnTriggerEnter(Collider collider)
{
if (collider.CompareTag ("Enemy")) { //Tag is name of prefab.
//When the enemy hits the player, destroy the player and the enemy.
playerLives--;
renderer.enabled = false;
timer = Time.time;
}
if (playerLives < 1)
Destroy(this.gameObject);
}
}
你应该更好地解释你的意图。您在该方法的中间追加该文件,然后将其重写在下面几行。这是打算? – Groo 2014-09-01 05:51:07
在这种情况下创建可能被限制为一次。这里你没有显示那部分。首先请回答上面提出的问题。 – Hassan 2014-09-01 05:53:16
我修改了示例以包含脚本的其余部分。脚本应检查现有文件,如果找不到它或者脚本内的日期超过24小时,它将创建一个新的文件。随机数据稍后将用于随机化激光产生的地方(左侧或右侧)。 – PopSmith 2014-09-01 19:22:37