using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Cyber.Util {
///
/// Utility functions for mesh-related things.
///
public class MeshUtil : MonoBehaviour {
///
/// Gets the bounds of a gameobject or its children with a mesh.
///
/// The bounds of the object.
/// Object.
public static Bounds GetMeshBounds(GameObject obj) {
MeshFilter Mesh = GetMeshFilter(obj);
if (Mesh != null) {
return Mesh.mesh.bounds;
}
SkinnedMeshRenderer SkinnedMesh = GetSkinnedMeshRenderer(obj);
if (SkinnedMesh != null) {
return SkinnedMesh.sharedMesh.bounds;
}
return new Bounds();
}
///
/// Sets the material in the objects or its children.
///
/// Object.
/// Mat.
public static void SetMaterial(GameObject obj, Material mat) {
MeshRenderer MeshRenderer = MeshUtil.GetMeshRenderer(obj);
if (MeshRenderer != null) {
Material[] NewMats = new Material[MeshRenderer.materials.Length];
for (int j = 0; j < MeshRenderer.materials.Length; j++) {
NewMats[j] = mat;
}
MeshRenderer.materials = NewMats;
}
SkinnedMeshRenderer SkinnedMeshRenderer = MeshUtil.GetSkinnedMeshRenderer(obj);
if (SkinnedMeshRenderer != null) {
Material[] NewMats = new Material[SkinnedMeshRenderer.materials.Length];
for (int j = 0; j < SkinnedMeshRenderer.materials.Length; j++) {
NewMats[j] = mat;
}
SkinnedMeshRenderer.materials = NewMats;
}
}
///
/// Gets a MeshFilter in a gameobject or its children.
///
/// The MeshFilter in object.
/// Object.
public static MeshFilter GetMeshFilter(GameObject obj) {
MeshFilter Mesh = obj.GetComponent();
if (Mesh == null) {
// No mesh found, search children
Mesh = obj.GetComponentInChildren();
}
return Mesh;
}
///
/// Gets a MeshRenderer in a gameobject or its children.
///
/// The MeshRenderer in object.
/// Object.
public static MeshRenderer GetMeshRenderer(GameObject obj) {
MeshRenderer Mesh = obj.GetComponent();
if (Mesh == null) {
// No mesh found, search children
Mesh = obj.GetComponentInChildren();
}
return Mesh;
}
///
/// Gets a SkinnedMeshRenderer in a gameobject or its children.
///
/// The SkinnedMeshRenderer in object.
/// Object.
public static SkinnedMeshRenderer GetSkinnedMeshRenderer(GameObject obj) {
SkinnedMeshRenderer Mesh = obj.GetComponent();
if (Mesh == null) {
// No mesh found, search children
Mesh = obj.GetComponentInChildren();
}
return Mesh;
}
}
}