简体   繁体   English

如何在Unity中更新var

[英]How can I update a var in Unity

I want my var depth for example everytime find a new value, I did this but I don't if is the best way to update this var. 例如,我希望我的var深度每次找到一个新值时就这样做了,但是我不是更新此var的最佳方法。

void Update()
{
    Terrain terrain = GetComponent<Terrain>();
    terrain.terrainData = GenerateTerrain(terrain.terrainData);

    offsetX += Time.deltaTime * 1f;

    StreamReader reader = new StreamReader(@"C:\Users\----\documents\BrainGrapher\posiciones.txt");
    char[] delimitar = { ':' };
    string s = reader.ReadLine();
    string[] value = s.Split(delimitar);

    while (s != null)
    {
        for (int move = 0; move < value .Length; move ++)
        {
            save= vlues[recorrer];
            Debug.Log(save);
        }
        depth = Convert.ToInt32(save);
        Debug.Log(depth);
        s = reader.ReadLine();
    }

}

The idea is to take a value from the file and everytime this value is updated, the var depth is changed and so the graphic changed too, the variable depth is using just here. 想法是从文件中获取一个值,并且每次更新该值时,var深度都会更改,因此图形也随之更改,可变深度仅在此处使用。

    TerrainData GenerateTerrain(TerrainData terrainData)
{
    terrainData.heightmapResolution = width + 1;
    terrainData.size = new Vector3(width, depth, height);
    terrainData.SetHeights(0, 0, GenerateHeights());
    return terrainData;
}

Thank you. 谢谢。

In C#, there is a class called FileSystemWatcher . 在C#中,有一个名为FileSystemWatcher的类。

This class will allow you to register to an event so you don't have to check yourself by reading the file in each update. 此类允许您注册事件,因此您无需在每次更新时都通过读取文件来检查自己。

You should create a FileSystemWatcher in the Start method and register to it. 您应该在Start方法中创建FileSystemWatcher并注册。 Then you only read the depth value if the file has changed, and you don't have to do anything in the update method. 然后,仅在文件已更改的情况下读取深度值,并且不必在update方法中执行任何操作。 You can even check these events 您甚至可以检查这些事件

        watcher.Changed += new FileSystemEventHandler(OnChanged);
        watcher.Created += new FileSystemEventHandler(OnChanged);
        watcher.Deleted += new FileSystemEventHandler(OnChanged);
        watcher.Renamed += new RenamedEventHandler(OnRenamed);

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

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