miepzerino
2023-12-23 eab47305629d96d19626e10b649ba4247d1f55f5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Tilemaps;
 
public class GenerateTileMap : MonoBehaviour
{
    public int width = 256;
    public int height = 256;
    public float scale = 20f;
    public float offsetX = 0f;
    public float offsetY = 0f;
    Tilemap tilemap;
    public RuleTile ruleTile;
    //public List<TileBase> tiles;
 
    private void Awake()
    {
        tilemap = GetComponent<Tilemap>();
    }
 
    public IEnumerator GenerateTiles(Action finishedCallback)
    {
        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                float xPerlin = ((float)x / width) * scale + offsetX;
                float yPerlin = ((float)y / height) * scale + offsetY;
                float perlinNoise = Mathf.PerlinNoise(xPerlin, yPerlin);
 
                if (perlinNoise >= 0.3f)
                {
                    tilemap.SetTile(new Vector3Int(x, y), ruleTile);
                }
 
            }
 
            // Update UI every 8 lines
            if ((x % 8) == 0)
            {
                yield return null;
            }
        }
        finishedCallback();
    }
}