0
\$\begingroup\$

I have 2D background in unity on which I would like to add multiple circle, box colliders. Assets are not separated. I have drawn assets on background.

How to move circle colliders with mouse or keyboard shortcut on the background? It's not very pleasant to define x,y position wherever needs to go for every collider.

\$\endgroup\$
1
\$\begingroup\$

Use Custom Editor

Add the following code to the background game object:
Important: Must be named MoveGizmo


MoveGizmo

using UnityEditor;
using UnityEngine;

public class MoveGizmo : MonoBehaviour {

    [CustomEditor(typeof(MoveGizmo))]
    public class ColliderMove : Editor {
        private float moveSize = 0.2f;

        public override void OnInspectorGUI() {
            Collider2D[] cols = ((MoveGizmo)target).GetComponents<Collider2D>();
            moveSize = EditorGUILayout.FloatField("Move Size", moveSize);
            foreach (var col in cols) {

                // a header for each collider, stating its current offset (location)
                GUILayout.Label(col.offset.ToString());

                // horizontal group is for placing the buttons on the same row
                GUILayout.BeginHorizontal("", GUIStyle.none);

                // repeat button allows you to hold the mouse on the button
                // to continuously move the collider 
                if (GUILayout.RepeatButton("<")) {
                    col.offset += new Vector2(-moveSize, 0);
                }
                if (GUILayout.RepeatButton(">")) {
                    col.offset += new Vector2(moveSize, 0);
                }
                if (GUILayout.RepeatButton("UP")) {
                    col.offset += new Vector2(0, moveSize);
                }
                if (GUILayout.RepeatButton("DOWN")) {
                    col.offset += new Vector2(0, -moveSize);
                }
                GUILayout.EndHorizontal();
            }

        }

    }
}

Note that I've also added a Move Size field so you can control the sensitivity of the move.

Adding this code to a gameobject will add the following buttons for each collider on that object: Buttons on inspector

\$\endgroup\$
  • \$\begingroup\$ I created script MoveGizmo and copy-pased your code in it. When I closed Mono, error occured ArgumentException: Illegal characters in path. \$\endgroup\$ – Wildfire Feb 20 '17 at 14:14
  • \$\begingroup\$ Maybe when you copied the name from the webpage it got in with some garbage html. Try to type the file name manually. \$\endgroup\$ – lilotop Feb 20 '17 at 14:18
  • \$\begingroup\$ Also, check this out: forum.unity3d.com/threads/… \$\endgroup\$ – lilotop Feb 20 '17 at 14:22
  • \$\begingroup\$ It's a lot of code to add. But possible. Just out of curiosity, why don't you use empty game objects for each collider? It would be much easier to control them using unity's built in tools. And you can easily add menu items and even shortcut keys to add new gameobjects already with a box/circle collider. \$\endgroup\$ – lilotop Feb 20 '17 at 14:34
  • \$\begingroup\$ Thnaks. Also, is it possible to place the empty collider on top of the image with the mouse ? For example, when you open sprite editor and select slice grid by cell size, you can move the rectangle gizmo with mouse in sprite editor. I was wondering how come this logic is not possible for colliders. \$\endgroup\$ – Wildfire Feb 20 '17 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.