- Home /
Detect editing of collider in editor
Is there any way to detect if a collider has been edited in the editor? Like an event firing? It would also be enough to have an event that fires when the "Edit collider" button inside the inspector has been pressed.
Answer by _geo__ · Jul 07, 2021 at 03:09 PM
If you are interested in a change of the shape then you might use the fact that "Tools.current" changes to "Tool.Custom" while editing. Here is a short demo code sample for 2D colliders.
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class YourCustomBehaviour : MonoBehaviour
{
#if UNITY_EDITOR
protected Collider2D _tmpCollider2D;
public Collider2D tmpCollider2D
{
get
{
if (_tmpCollider2D == null)
{
_tmpCollider2D = this.GetComponent<Collider2D>();
}
return _tmpCollider2D;
}
}
protected uint tmpShapeHash;
void OnDrawGizmos()
{
if(Tools.current == Tool.Custom && tmpCollider2D.GetShapeHash() != tmpShapeHash)
{
tmpShapeHash = tmpCollider2D.GetShapeHash();
Debug.Log("Collider shape changed!");
}
}
#endif
}
This might be a solution you can build on (depending on your needs). Though offests and bool flags etc. are not detected.
Your answer
Follow this Question
Related Questions
Event when exiting PolygonCollider2D edit mode 1 Answer
Start/Stop Playmode from editor script 10 Answers
mouseposition and clicks in editor sceneview 0 Answers
How can I let the ray to the edge of the capsule? 1 Answer
How do I solve a BuildAssetBundles Compilation Error that only occurs in the Editor? 1 Answer