miepzerino
2023-12-22 569f8efeca94146d3f863408cedb80ce6683e793
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
48
using System.Collections;
using System.Collections.Generic;
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;
    // Start is called before the first frame update
    void Start()
    {
        tilemap = GetComponent<Tilemap>();
        GenerateTiles();
    }
 
 
    public void GenerateTiles()
    {
        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)
                {
                    if (!tilemap.HasTile(new Vector3Int(x,y+1))) {
                        tilemap.SetTile(new Vector3Int(x, y), ruleTile);
                    } 
                    else
                    {
                        tilemap.SetTile(new Vector3Int(x, y), ruleTile);
                    }
                }
 
            }
        }
    }
}