- A* version: [5.24]
- Unity version: [2022.3.50f1]
Recently upgraded my project to A* 5.x and my live recompile solution broke with the new changes.
Luckily I was able to get it working again with some hacks.
Attach this script to the AStar pathfinder to rescan the navmesh and re-assign the static AstarPath variable on live recompile
using UnityEngine;
using System;
#if UNITY_EDITOR
using UnityEditor;
#endif
/// IMPORTANT!!!!
/// PLACE THIS SCRIPT ABOVE ASTAR IN THE SCRIPT EXECUTION ORDER IN PROJECT SETTINGS IN UNITY (EDIT MENU)
[Serializable]
public class AStarLiveRecompile : MonoBehaviour
{
#if UNITY_EDITOR
void OnEnable()
{
if (EditorApplication.isUpdating) // catch the live reload
{
AstarPath.active = GetComponent<AstarPath>();
if(EditorApplication.isUpdating)
Invoke(nameof(Scan),1f);
}
}
public void Scan()
{
AstarPath.active.Scan();
}
#endif
}
Then make these modifications to A*Project AIBase.cs line 382 to avoid an exception error
// When using rigidbodies all movement is done inside FixedUpdate instead of Update
bool fixedUpdate = rigid != null || rigid2D != null;
if(!BatchedEvents.Has(this))
BatchedEvents.Add(this, fixedUpdate ? BatchedEvents.Event.FixedUpdate : BatchedEvents.Event.Update, OnUpdate);
}
And remove this from AStarPath.cs line 1256 to prevent AStarPath from sabotaging the domain reload
// Note that the first time the component loads, then Awake will run first
// and will already have set the #active field.
// In the editor, OnDisable -> OnEnable will be called when an undo or redo event happens (both in and outside of play mode).
/*if (active != null) {
if (active != this && Application.isPlaying) {
if (this.enabled) {
Debug.LogWarning("Another A* component is already in the scene. More than one A* component cannot be active at the same time. Disabling this one.", this);
}
enabled = false;
}
return;
}*/
// Very important to set this. Ensures the singleton pattern holds
active = this;
AStar will now function with a live recompile and re-scan the graph, it’s a bit hacky but it works. Maybe in the future we could see a proper solution but for now this works.