In the short clip above you will see I have a fairly minimal set-up in Unity. There is a counter that is incremented every frame (in Update), along side that is this call 'UnityEditor.SceneView.RepaintAll()'. You can see a counter for that being outputted to the Console box.
Once I snap into the Game view and click on a button in the bottom left hand corner (the highlighted button press), it calls a script which imports a mesh at run time into the scene and renders it. You will notice the counter and repaint calls freeze during this, albeit brief, but very noticeable period of time. In actuality, everything in the scene freezes during this load time.
I have been googling how to refresh Unity's scene so that I can, in essence, load meshes at run time while maintaining responsiveness for the player. So far, nothing.
What is the best practice here to periodically refresh/update the scene so that the player isn't afflicted by load time game halts?
EDIT: RepaintAll() call does not work for me. Should have added that previously. This method does not work as I had hoped it would, in fact I am not exactly sure what its purpose is if not to refresh the scene
Mesh import code:
const int MAX_FACETS_PER_MESH = 65535 / 3;
class Facet
{
public Vector3 normal;
public Vector3 a, b, c;
public override string ToString()
{
return string.Format("{0:F2}: {1:F2}, {2:F2}, {3:F2}", normal, a, b, c);
}
}
private static Mesh[] ImportBinary(string path)
{
Facet[] facets;
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
using (BinaryReader br = new BinaryReader(fs, new ASCIIEncoding()))
{
// read header
byte[] header = br.ReadBytes(80);
uint facetCount = br.ReadUInt32();
facets = new Facet[facetCount];
for(uint i = 0; i < facetCount; i++)
{
facets[i] = new Facet();
facets[i].normal.x = br.ReadSingle();
facets[i].normal.y = br.ReadSingle();
facets[i].normal.z = br.ReadSingle();
facets[i].a.x = br.ReadSingle();
facets[i].a.y = br.ReadSingle();
facets[i].a.z = br.ReadSingle();
facets[i].b.x = br.ReadSingle();
facets[i].b.y = br.ReadSingle();
facets[i].b.z = br.ReadSingle();
facets[i].c.x = br.ReadSingle();
facets[i].c.y = br.ReadSingle();
facets[i].c.z = br.ReadSingle();
// padding
br.ReadUInt16();
}
}
}
return CreateMeshWithFacets(facets);
}
private static Mesh[] CreateMeshWithFacets(IList<Facet> facets)
{
int fl = facets.Count, f = 0, mvc = MAX_FACETS_PER_MESH * 3;
Mesh[] meshes = new Mesh[fl / MAX_FACETS_PER_MESH + 1];
for(int i = 0; i < meshes.Length; i++)
{
int len = System.Math.Min(mvc, (fl - f) * 3);
Vector3[] v = new Vector3[len];
Vector3[] n = new Vector3[len];
int[] t = new int[len];
for(int it = 0; it < len; it += 3)
{
v[it ] = facets[f].a;
v[it+1] = facets[f].b;
v[it+2] = facets[f].c;
n[it ] = facets[f].normal;
n[it+1] = facets[f].normal;
n[it+2] = facets[f].normal;
t[it ] = it;
t[it+1] = it+1;
t[it+2] = it+2;
f++;
}
meshes[i] = new Mesh();
meshes[i].vertices = v;
meshes[i].normals = n;
meshes[i].triangles = t;
}
return meshes;
}