- Home /
Editor-only circular contour around prefab instances
I have a prefab with radius setting, which programatically controls circular area with some stuff. I want instances of this prefab to display a circle with this radius in editor for convenience. Instead of radius setting I can attach circle collider to get visual border and then read its radius at runtime, but this seems code smell (I will need to programmatically remove/disable it etc), and resource waste. Are there some good practices to implement things like this?
Answer by aemxdp · Oct 17, 2015 at 04:38 PM
Found an answer myself. That's actually quite easy. There is some "Gizmos" stuff for things like that, and it's possible to just overload OnDrawGizmos (in a script attached to prefab) and use built-in function to draw circle in editor:
private void OnDrawGizmos()
{
UnityEditor.Handles.color = Color.yellow;
UnityEditor.Handles.DrawWireDisc(transform.position, Vector3.back, Radius);
}
Answer by _dns_ · Oct 17, 2015 at 03:44 PM
Hi, there is something very convenient to do this: OnDrawGizmos() and OnDrawGizmosSelected(), check Monobehavior documentation.
In those callbacks, you can use Gizmos.DrawLine (check other Gizmos. methods) to draw a circle around the object.
You can also use "#if UNITY_EDITOR" around this code so it is only included in the editor (not sure those are conditionally compiled).
I'm sorry, I've answered this and accepted my own answer few $$anonymous$$utes after asking the question ( proof here crossposted: http://gamedev.stackexchange.com/questions/109852/editor-only-circular-contour-around-prefab-instances ), but this forum has some extreme premoderation rules, so my answer was unscreened only recently. Thank you for writing an answer anyway!
Your answer
Follow this Question
Related Questions
Creating Prefabs via Code the renderer loses Sprite. 1 Answer
Prefab brush and EnsureUniqueNameForSibling 0 Answers
Detecting if the 2D Mode button was pressed 2 Answers
Mark gameobject field as changed from prefab 2 Answers
Editing prefab fields from custom editor that are not automatically serialized 0 Answers