- Home /
Making a tilemap editor within the Unity Editor
Is it possible to customize the Unity Editor so that I could "paint" objects into my scene, like in a tilemap editor such as Mappy or TileStudio? Basically I want to be able to click on my scene and have an object added at that position on a specified plane and grid. Something a bit like the terrain editor's foliage paintbrush.
Can I use OnSceneGUI for this perhaps?
Answer by Pekuja · May 13, 2010 at 02:24 PM
Ok, so here's what I've come up with: Make a custom editor for a "Tilemap" MonoBehaviour, and in it have an OnSceneGUI. In the OnSceneGUI, generate a controlID with GUIUtility.GetControlID and add the control as default with HandleUtility.AddDefaultControl. This way, the Unity Editor won't use your mouse clicks on the scene for selecting things, but instead you can do whatever you like with the clicks. Here's a simple example I wrote in Boo, that basically will move any part of the map that you click on by five units:
import UnityEditor
import UnityEngine
[CustomEditor(Tilemap)]
class TilemapEditor (Editor):
def OnSceneGUI():
controlID as int = GUIUtility.GetControlID(FocusType.Passive)
if Event.current.type == EventType.mouseDown:
hit as RaycastHit
if Physics.Raycast(Event.current.mouseRay, \
hit, Mathf.Infinity, 1 << (target as Tilemap).terrainLayer):
Debug.Log("Hit a part of the terrain")
hit.transform.position.y += 5
if Event.current.type == EventType.layout:
HandleUtility.AddDefaultControl(controlID)
Obviously it still needs a lot of work to be a real editor; this is just a proof of concept.
Thank you very much for figuring this stuff out, it was exactly what I needed.
Yep. Good solution. But how to avoid seletion. On left click Unity editor will select a gameobject under the cursor.
kirpigiller, the AddDefaultControl voodoo should prevent left-click selection. Worked for me, thanks Pekka!
I had to remove "if Event.current.type == EventType.layout:" to make it work in Unity5
Answer by duck · May 12, 2010 at 12:31 PM
Yes, I think technically it's possible, because the terrain editor system itself is written using Unity's own Editor Scripting API, so anything done there - you can do.
Unfortunately this is an area that is not well documented at all in the manual - for example, the OnInspectorGUI manual page contains just two short sentences as a description, and no example at all! - so it may be tough going, but good luck with it!
It does seem like the documentation is pretty bad. Hopefully the situation will improve. Currently I'm going by random code snippets and guesswork.
Your answer
Follow this Question
Related Questions
How can I interacte with Sprites from a Texture with Sprite Mode multi? 1 Answer
Tilemap focus problem 0 Answers
Editor Shared Material 1 Answer
Tilemap Grid Broken? 1 Answer