I have an issue where I wrote a small editor script that rebuilt my scene graph and is listed as below:
public void RecreateTransform( Transform t )
{
GameObject g = t.gameObject;
string name = g.name;
Transform parent = g.transform.parent;
Vector3 localPos = g.transform.localPosition;
Quaternion localRot = g.transform.localRotation;
Vector3 localScale = g.transform.localScale;
bool bRecreate = false;
MeshFilter filter = null;
MeshRenderer renderer = null;
PlanePart oldPart = null;
Mesh mesh = null;
Material[] mats = null;
bool hasWireFrame = false;
Component[] components = g.GetComponents<Component>();
for (int j = 0; j < components.Length; j++)
{
if (components[j] == null)
{
bRecreate = true;
}
else if( components[j] is MeshFilter )
{
filter = (MeshFilter)components[j];
mesh = filter.mesh;
}
else if( components[j] is PlanePart )
{
oldPart = (PlanePart)components[j];
}
else if( components[j] is MeshRenderer )
{
renderer = (MeshRenderer)components[j];
mats = renderer.materials;
}
else if( components[j] is WireFrameLineRenderer )
{
hasWireFrame = true;
}
}
if( bRecreate || !hasWireFrame)
{
List<Transform> oldChildren = new List<Transform>();
for( int i = 0; i<t.childCount; i++ )
{
oldChildren.Add( t.GetChild(i) );
}
GameObject recreated = new GameObject();
recreated.name = name;
recreated.transform.parent = parent;
recreated.transform.localPosition = localPos;
recreated.transform.localRotation = localRot;
recreated.transform.localScale = localScale;
foreach( Transform oldchild in oldChildren )
{
oldchild.parent = recreated.transform;
RecreateTransform( oldchild );
}
if( oldPart != null )
{
PlanePart newPart = recreated.AddComponent<PlanePart>();
newPart.IconTexture = oldPart.IconTexture;
newPart.Category = oldPart.Category;
newPart.PartName = oldPart.PartName;
newPart.ID = oldPart.ID;
}
if( mesh != null )
{
MeshFilter newFilter = recreated.AddComponent<MeshFilter>();
newFilter.mesh = mesh;
MeshRenderer newRenderer = recreated.AddComponent<MeshRenderer>();
newRenderer.materials = mats;
WireFrameLineRenderer lineRender = recreated.AddComponent<WireFrameLineRenderer>();
lineRender.Fidelity = 2;
lineRender.LineColor = new Color( 0, 204f/255f, 1, 1);
}
GameObject.DestroyImmediate(g);
}
}
public void OnGUI() {
if (GUI.Button(new Rect(0.3f, 60.0f, 100.0f, 20.0f), "Rebuild"))
{
Undo.RegisterSceneUndo("RECREATE");
GameObject[] go = Selection.gameObjects;
for( int i = 0; i<go.Length;i++)
{
RecreateTransform( go*.transform );*
*}*
*}*
*}*
*```*
*<p>After this processed my scene graph all the meshes were turned to 'Instances' rather than mantaining their original references to 20 meshes, I now have 1000+ meshes being loaded which is blowing out the ram usage on the iPhone....</p>*
*<p>Does anyone have a suggestion on pointing these meshes back to their asset reference? I would hate to have to go through 900+ scene objects just to re-wire 20 mesh instances....</p>*
*<p>Should I do sharedMesh on the renderer instead of mesh ?</p>*