-
-
Save jeffomatic/8675200ede20c73b58cd3e211cc130b4 to your computer and use it in GitHub Desktop.
Utility for drawing Unity objects without instantiation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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