Unity Collision Box Macro Extension

Since my tablet is barely inoperable and I’m on winter break now, I can’t work on my comic at the moment. A new tablet should be one of my Christmas presents. So until then, I decided to play around with unity some more.

12_14_14_editorext01 12_14_14_editorext02

After being used to Unreal Engine 4’s collision geometry procedures, coming back to unity led to frustration. In Unity, to make collision geometry for a complex room, I had to drag in new game objects for each “piece” of the room, line it up, remove the rendering component, and add a box collider component. That becomes very time consuming for large levels with multiple room pieces.

I had the idea to program my own editor extension that imitates UE4’s collision object generator. I model the room following the same process I would for a UE4 asset, making collision boxes as part of the model with a standard prefix “UBX_”. Then my script will recursively flip through all the sub-objects in the model’s hierarchy and if the prefix matches “UBX_”, it will remove the rendering component and add a box collider.

Editor Extension Script C# (Unity 4+):


using UnityEngine;
using UnityEditor;
using System.Collections;

// Collision Box Tool by Seth J. Hampson
//    based on the collision geometry procedure from Unreal Engine 4
//    How To Use: Import your model with collision boxes built in.
//        make sure their name starts with your prefix (I.E. “UBX_”)
//        make sure the collision objects are children of the main model in the hierarchy
//
//    Dec 14, 2014
public class CollisionBoxTool : EditorWindow
{
string prefixtype = “UBX_”; // the prefix to look for
string finishtype = “COL_”; // the new prefix set when done

[MenuItem(“Tools/Collision Box Generator”)]
public static void ShowWindow()
{
EditorWindow.GetWindow (typeof(CollisionBoxTool));
}

void OnGUI()
{
GUILayout.Label (“Collision Box Generator”, EditorStyles.boldLabel);
prefixtype = EditorGUILayout.TextField (“Prefix To Find”, prefixtype);
finishtype = EditorGUILayout.TextField (“New Prefix”, finishtype);

if(Selection.activeGameObject)
{
if(GUILayout.Button(“Build Collision”))
{
if(Selection.activeGameObject)
{
SelectionRecursion(Selection.activeGameObject.transform);
}
}
}
}

void SelectionRecursion(Transform level)
{
if(level == null)
return;
if(level.childCount == 0)
{
Debug.Log(“root ob ” + level.gameObject.name);
if(level.gameObject.name.StartsWith(prefixtype))
{
MeshRenderer mren = (MeshRenderer)level.gameObject.GetComponent(typeof(MeshRenderer));
if(mren != null)
{
DestroyImmediate(mren);
}
level.gameObject.AddComponent(typeof(BoxCollider));
string newname = level.gameObject.name;
newname = newname.Remove(0, prefixtype.Length);
newname = finishtype + newname;
level.gameObject.name = newname;
}
return;
}
for(int i = 0; i < level.childCount; i++)
{
SelectionRecursion(level.GetChild(i).transform);
}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s