#Grid System with ExecuteInEditMode

1 messages · Page 1 of 1 (latest)

junior wolf
#

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...

#
[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

humble canopy
#

But just FYI - I won't be around for too much longer (10 minutes or so)

junior wolf
#

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

humble canopy
#

and what's GridManager? An instance of GridManager?

#

paste your whole code from GridPlacer and GridManager on pastebin

junior wolf
humble canopy
#

(and if GridManager isn't in your scene anywhere, then it won't be called at all in a prefab)

junior wolf
#

oh

#

I get it

#

do u have any ideas how could I work around this?

junior wolf
humble canopy
#

unfortunately, I've never really done any editor stuff so you're sorta on your own there 🙂