/// <summary> /// Sets the render meshes for <paramref name="obj"/> using <paramref name="sharedMeshes"/>. /// </summary> /// <param name="obj">The object to initialise visuals for.</param> /// <param name="sharedMeshes">The meshes to render with.</param> /// <param name="colour">Primary rendering colour.</param> /// <remarks> /// This function sets <paramref name="sharedMeshes"/> as a set of shared mesh resources /// to render <paramref name="obj"/> with. When there is one element in /// <paramref name="sharedMeshes"/>, the mesh and material are set on the /// <c>MeshFilter</c> and <c>MeshRenderer</c> belonging to <paramref name="obj"/> itself. /// When multiple items are given, the children of <paramref name="obj"/> are used instead. /// This means the number of children must match the number of elements in /// <paramref name="sharedMeshes"/>. /// /// The materials used are attained via <see cref="LookupMaterialFor(ShapeComponent)"/> /// where the <see cref="ShapeComponent"/> belongs to <paramref name="obj"/>. /// </remarks> protected virtual void InitialiseMesh(ShapeComponent obj, Mesh[] sharedMeshes, Color colour) { MeshFilter filter = obj.GetComponent <MeshFilter>(); MeshRenderer render = obj.GetComponent <MeshRenderer>(); ShapeComponent shape = obj.GetComponent <ShapeComponent>(); if (sharedMeshes.Length == 1) { // Single mesh. Set on this object. if (filter != null) { filter.sharedMesh = sharedMeshes[0]; if (render != null) { int componentCount = (filter.sharedMesh != null) ? filter.sharedMesh.subMeshCount : 0; SetMaterial(LookupMaterialFor(shape), render, componentCount, colour); } } } else { // Multiple meshes. Set on children. Transform child; for (int i = 0; i < sharedMeshes.Length; ++i) { child = obj.transform.GetChild(i); if ((filter = child.GetComponent <MeshFilter>()) != null) { filter.sharedMesh = sharedMeshes[i]; } if ((render = child.GetComponent <MeshRenderer>())) { SetMaterial(LookupMaterialFor(shape), render, sharedMeshes[i].subMeshCount, colour); } } } }