Skip to content

Instantly share code, notes, and snippets.

@jeffomatic
Created September 6, 2022 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffomatic/8675200ede20c73b58cd3e211cc130b4 to your computer and use it in GitHub Desktop.
Save jeffomatic/8675200ede20c73b58cd3e211cc130b4 to your computer and use it in GitHub Desktop.
Utility for drawing Unity objects without instantiation
using UnityEngine;
public class BlockEditorUtil {
public static void DrawGameObjectHierarchy(
GameObject obj,
Vector3 trans,
Quaternion rot,
Vector3 scale,
Material overrideMat
) {
trans += rot * scale.Mul(obj.transform.localPosition);
rot *= obj.transform.localRotation;
scale = scale.Mul(obj.transform.localScale);
DrawGameObjectMesh(obj, trans, rot, scale, overrideMat);
foreach (Transform child in obj.transform) {
DrawGameObjectHierarchy(
child.gameObject,
trans,
rot,
scale,
overrideMat
);
}
}
private static void DrawGameObjectMesh(
GameObject obj,
Vector3 trans,
Quaternion rot,
Vector3 scale,
Material overrideMat
) {
Mesh mesh;
Material mat;
if (obj.TryGetComponent<SkinnedMeshRenderer>(out var smr)) {
mesh = smr.sharedMesh;
mat = overrideMat != null ? overrideMat : smr.sharedMaterial;
} else {
var filter = obj.GetComponent<MeshFilter>();
if (filter == null) {
return;
}
mesh = filter.sharedMesh;
mat = overrideMat != null ? overrideMat : obj.GetComponent<Renderer>().sharedMaterial;
}
var matrix = Matrix4x4.TRS(trans, rot, scale);
Graphics.DrawMesh(mesh, matrix, mat, obj.layer);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment