A question about grid snapping. I watched some tutorials and make a little search but didn't found what I was looking for. I have a 3d grid and I want the level to be created using my own tool, only in the editor, not using any code at all for easier level designs. I have a script for placing the tiles in the grid just like this below. The problem is: it doesn't work at all. I have an offset variable that I set in another script called GridManager which is responsible for get connections between tiles and where I have a variable offset to control distance between tiles and I would like to use in this GridPlacer. Reason: I don't want to repeat the offset in each TilePlacer component...
#Grid System with ExecuteInEditMode
1 messages · Page 1 of 1 (latest)
[ExecuteInEditMode]
public class GridPlacer : MonoBehaviour {
#if UNITY_EDITOR
// Update is called once per frame
protected virtual void Update() {
if (!Application.isPlaying) {
SnapToGrid();
}
}
#endif
// convert current position in grid position
void SnapToGrid() {
// get the offset from grid manager
Vector3 _offset = GridManager.instance.GetOffset();
if (GridManager.instance == null) {
Debug.Log("Grid Manager instance is null."); return;
}
// get each axis
float _x = Mathf.Round(transform.position.x / _offset.x);
float _y = Mathf.Round(transform.position.y / _offset.y);
float _z = Mathf.Round(transform.position.z / _offset.z);
// calculate position based on grid offset
Vector3 _pos = new Vector3(_x * _offset.x, _y * _offset.y, _z * _offset.z);
// apply the position
transform.position = _pos;
}
}
@humble canopy moved the conversation here
But just FYI - I won't be around for too much longer (10 minutes or so)
grid manager is null
the instance of grid manager is null I mean
I already put the [ExecuteInEditMode] in the GridManager but didn't work at all
and what's GridManager? An instance of GridManager?
paste your whole code from GridPlacer and GridManager on pastebin
GridManager: https://pastebin.com/X6z00Bx7
Pastebin
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
GridPlacer: https://pastebin.com/idL2iNde
Pastebin
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
https://docs.unity3d.com/ScriptReference/ExecuteInEditMode.html
You're setting the instance in Awake, which isn't called until the script is instanced (ie, you create one somewhere) - so again, I don't think you want to use a public static instance variable
(and if GridManager isn't in your scene anywhere, then it won't be called at all in a prefab)
I'm trying to make a custom editor, so I'll keep this in mind and create a reference to the script to create it when using the tool
unfortunately, I've never really done any editor stuff so you're sorta on your own there 🙂
maybe see if anyone in #↕️┃editor-extensions can help