When I try to frame selected on a GameObject with a Mesh created from code or one imported as an fbx file from Blender, the camera will cycle between framing the mesh’s origin, and framing the mesh’s bounds. However if you select a default cube and repeatedly frame selected, the camera will first frame the cube’s bounds and then not move anymore. The latter is the kind of behavior I want for my code created Mesh’s and imported fbx files. I have included an example MonoBehaviour that you can attach to any GameObject to assign it a Mesh created in code that’s just a quad. I am using Unity version 2020.3.16f1. So why doesn’t the frame selected command behave the same for Meshes created in code and those imported as fbx files as for Unity’s built in meshes like cubes and spheres and how do I get the behavior I want?
using UnityEngine;
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class TestMeshCreator : MonoBehaviour
{
public float meshScale = 1;
public void AssignTestMesh()
{
var filter = GetComponent<MeshFilter>();
var mesh = CreateMesh();
filter.sharedMesh = mesh;
mesh.RecalculateBounds();
var rend = GetComponent<MeshRenderer>();
rend.sharedMaterial = new Material(Shader.Find("Standard"));
}
Mesh CreateMesh()
{
Mesh mesh = new Mesh();
mesh.name = "Test mesh";
mesh.SetVertices(new Vector3[]
{
new Vector3(-.5f, 0, 0) * meshScale,
new Vector3(+.5f, 0, 0) * meshScale,
new Vector3(-.5f, 1, 0) * meshScale,
new Vector3(+.5f, 1, 0) * meshScale,
});
mesh.SetIndices(new int[]
{
0, 2, 3,
0, 3, 1
},
MeshTopology.Triangles,
0);
mesh.RecalculateBounds();
mesh.RecalculateNormals();
return mesh;
}
#if UNITY_EDITOR
[UnityEditor.CustomEditor(typeof(TestMeshCreator))]
public class TestMeshCreator_Editor : UnityEditor.Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
var t = target as TestMeshCreator;
if (GUILayout.Button("Create Mesh"))
{
t.AssignTestMesh();
}
}
}
#endif
}