forked from RonenNess/UnityUtils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTilemapEditor.cs
More file actions
78 lines (62 loc) · 2.02 KB
/
TilemapEditor.cs
File metadata and controls
78 lines (62 loc) · 2.02 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* Provide editor functionality for tilemaps (eg changing tiles at runtime using mouse input and main camera).
*
* Author: Ronen Ness.
* Since: 2017.
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace NesScripts.Tilemap
{
/// <summary>
/// Provide editor functionality to the tilemaps.
/// </summary>
public class TilemapEditor : MonoBehaviour {
/// <summary>
/// Tile to set on click.
/// </summary>
public GameObject TilePrefab;
/// <summary>
/// Which mouse button will set tile when clicked.
/// </summary>
public int MouseKeyToPlaceTiles = 0;
/// <summary>
/// If true, will "paint" tiles while mouse button is held down. If false, will only place a single tile when mouse button is release.
/// </summary>
public bool BrushMode = false;
/// <summary>
/// Layer ids to query for tiles (if you put all your tiles under a specific layer, use this to make sure you won't collide with other things in the way).
/// </summary>
public int TilesLayersFilter = int.MaxValue;
/// <summary>
/// If true, will render query ray in gizmos.
/// </summary>
public bool ShowDebugRay = true;
// Update is called once per frame
void Update () {
// get ray from camera to mouse position
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// debug draw ray
if (ShowDebugRay)
{
Debug.DrawRay(ray.origin, ray.direction, Color.green);
}
// for collision detection
RaycastHit hitInfo;
// if we hit a valid target we can place object on:
if (Physics.Raycast(ray, out hitInfo, 1000f, TilesLayersFilter))
{
// get tile component from collision object
Tile tile = hitInfo.collider.gameObject.GetComponent<Tile>();
// if not valid tile, skip
if (tile == null) return;
// if click, leave object where we placed it
if ((BrushMode && Input.GetMouseButton(MouseKeyToPlaceTiles)) || Input.GetMouseButtonUp(MouseKeyToPlaceTiles))
{
tile.Tilemap.SetTile (TilePrefab, tile.Index, true);
}
}
}
}
}