- Home /
Is it possible to raycast during edit mode?
So I have this custom made navmesh editor for 2D pathfinding used by enemies in the game. I can add nodes, connect them, disconnect them, move them around, whatever, and then assign them to an enemy for ingame use.
What I'm trying to do is make an option to automatically fill a space with nodes and connect them in a grid-like manner, BUT not connect them when a platform is in the way. For this purpose, each potential connection between two nodes is checked via raycast on my "World" layer which contains all the platforms. If there's a collider found, no connection is made and the algorithm moves on to the next connection. If there's no collider, the algorithm makes a connection and moves on.
The thing is, raycasting doesn't work in edit mode at all.
Expectation: Result:
When I hit Play and test the very same code, raycasting returns everything as needed, and gives the expected result.
It is worth mentioning that the editor script inherits from Editor (which, in turn, inherits from ScriptableObject), and the raycasting is performed under certain conditions during OnInspectorGUI. ExecuteInEditMode, ExecuteAlways, etc. didn't work, nor did a separate MonoBehaviour, as raycasting apparently only updates in the next FixedUpdate, which (to my knowledge) can't be forced like regular Update.
I could make the mesh in Play mode and then copy the results, but it's far from ideal.
Any suggestions?
(This is Unity 2019.4.18f1)
Answer by Boolean_of_True_th · Jun 24, 2021 at 12:16 AM
Ive never tried this but if you need your script to run in edit mode this might be relevant. Hope this helps.
https://docs.unity3d.com/ScriptReference/ExecuteInEditMode.html
Unfortunately it didn't, nor did the other options e.g. ExecuteAlways. The thing is that the script which executes the raycast is inheriting from Editor and not MonoBehaviour, and the raycast is executed under certain conditions within OnInspectorGUI().
I think you should mention this in the original question, these details would affect responses.
I guess I'm probably out of my league on this one then... I have no idea how Editor scripts work, sorry.
maybe you could use a MonoBehavior that continually updates in editor mode to call your function within your Editor script, might be a terrible solution. the cone below forces itself to constantly update, i don't know but maybe you could use it to call logic from your editor script.
using System.Collections;
using UnityEngine;
[ExecuteAlways]
public class Script : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void OnDrawGizmos()
{
if (!Application.isPlaying)
{
UnityEditor.EditorApplication.QueuePlayerLoopUpdate();
UnityEditor.SceneView.RepaintAll();
}
GameObject.Find("sp").transform.Rotate(0, 0, 10);
}
}
i found this code on another forum https://forum.unity.com/threads/solved-how-to-force-update-in-edit-mode.561436/
I tried and unfortunately it didn't work, because apparently raycasting yields a result after FixedUpdate, which cannot be forced during Edit.