コード例 #1
0
    public override void OnInspectorGUI()
    {
        TerrainConstructor terrain = (TerrainConstructor)target;

        DrawDefaultInspector();

        if (GUILayout.Button("Generate Terrain"))
        {
            terrain.GenerateTerrain();
            EditorUtility.SetDirty(terrain);
        }
        if (GUILayout.Button("Generate Flora"))
        {
            terrain.GenerateFlora();
            EditorUtility.SetDirty(terrain);
        }
        if (GUILayout.Button("Construct Terrain"))
        {
            terrain.ClearTerrain();
            terrain.ConstructTerrain();
            EditorUtility.SetDirty(terrain);
        }
        if (GUILayout.Button("Clear All"))
        {
            terrain.ClearTerrain();
            EditorUtility.SetDirty(terrain);
        }
        if (GUILayout.Button("Save Terrain"))
        {
            TerrainSaver.SaveTerrain(terrain.terrainData, terrain.terrainFileName);
        }
        if (GUILayout.Button("Load Terrain"))
        {
            TerrainData data = TerrainSaver.LoadTerrain(terrain.terrainFileName);
            terrain.terrainData = data;
            terrain.ConstructTerrain();
        }
        serializedObject.Update();
        serializedObject.ApplyModifiedProperties();
    }
コード例 #2
0
    // Method that loads a terrain
    public IEnumerator Load(string fileName, bool instantiateCamera)
    {
        // Create the load path
        string path = "Saved Maps/" + fileName;

        // Create a binaryFormatter
        BinaryFormatter binaryFormatter = new BinaryFormatter();

        // load the map at path as a textasset
        ResourceRequest request = Resources.LoadAsync(path);

        yield return(request);

        TextAsset loaded = (TextAsset)request.asset;
        // create a memory stream of the bytes in the loaded textasset
        MemoryStream stream = new MemoryStream(loaded.bytes);
        float        timeS  = Time.realtimeSinceStartup;
        // Deserialize to a world terrainsaver objects
        TerrainSaver world = (TerrainSaver)binaryFormatter.Deserialize(stream);

        Debug.Log(Time.realtimeSinceStartup - timeS);
        // get all terrain parameters
        length    = world.length;
        width     = world.width;
        maxHeight = world.maxHeight;
        seed      = world.seed;

        terrainOctaves     = world.terrainOctaves;
        terrainPersistance = world.terrainPersistance;
        terrainFrequency   = world.terrainFrequency;
        biomeOctaves       = world.biomeOctaves;
        biomePersistance   = world.biomePersistance;
        biomeFrequency     = world.biomeFrequency;

        chunkSize = world.chunkSize;
        tileSize  = world.tileSize;
        tileSlope = world.tileSlope;

        waterChunkSize   = world.waterChunkSize;
        waterTileSize    = world.waterTileSize;
        waterOctaves     = world.waterOctaves;
        waterPersistance = world.waterPersistance;
        waterFrequency   = world.waterFrequency;
        waterLevel       = world.waterLevel;
        maxWaveHeight    = world.maxWaveHeight;

        // Get the list of all chunks
        List <TerrainSaveObject> chunkList = world.terrainSaveList;

        // Initialize the code so the map can be created
        Initialize();

        // Loop through all chunks and recreate them with their respective parameters
        float timeSinceUpdate = Time.realtimeSinceStartup;
        int   index           = 0;

        foreach (TerrainSaveObject obj in chunkList)
        {
            GameObject curChunk    = (GameObject)Instantiate(chunkPrefab, obj.chunkLoc.GetVec3(), Quaternion.identity);
            Chunk      chunkScript = curChunk.GetComponent <Chunk>();
            chunkScript.map      = obj.GetVec3Map();
            chunkScript.biomeMap = obj.biomeMap;
            curChunk.transform.SetParent(transform);
            // try to save ram by deleting the chunks from the chunklist
            chunkList[index] = null;
            System.GC.Collect();

            // Show an update if a certain ammount of chunks has been built
            if (Time.realtimeSinceStartup - timeSinceUpdate > 1f / 10f)
            {
                timeSinceUpdate = Time.realtimeSinceStartup;
                yield return(null);
            }

            index++;
        }

        // Create the water
        BuildWater();

        // Create all biome objects
        StartCoroutine(GenerateBiomeAttributes());
    }