private void Start()
    {
        Object[] loadedBaseMeshes  = Resources.LoadAll("Meshes/Bases");
        Object[] loadedTowerMeshes = Resources.LoadAll("Meshes/Towers");

        allMeshes[0] = new Mesh[loadedBaseMeshes.Length];
        allMeshes[1] = new Mesh[loadedTowerMeshes.Length];

        for (int i = 0; i < loadedBaseMeshes.Length; i++)
        {
            allMeshes[0][i] = (loadedBaseMeshes[i] as GameObject).GetComponent <MeshFilter>().sharedMesh;
        }

        for (int i = 0; i < loadedTowerMeshes.Length; i++)
        {
            allMeshes[1][i] = (loadedTowerMeshes[i] as GameObject).GetComponent <MeshFilter>().sharedMesh;
        }

        prefabTankBaseMesh  = tankPrefab.GetComponentInChildren <MeshFilter>();
        prefabTankTowerMesh = prefabTankBaseMesh.GetComponentInChildren <MeshFilter>();

        previewTank = Instantiate(tankPrefab);

        previewTank.GetComponent <TankScript>().enabled = false;

        previewTankBaseMesh  = previewTank.GetComponentInChildren <MeshFilter>();
        previewTankTowerMesh = previewTankBaseMesh.GetComponentInChildren <MeshFilter>();

        previewTankBaseMesh.GetComponentInParent <TankScript>().enabled = false;

        foreach (ParticleSystem p in previewTankBaseMesh.GetComponentsInChildren <ParticleSystem>())
        {
            p.Stop();
        }

        previewTank.GetComponentInChildren <Canvas>().gameObject.SetActive(false);

        allMats[0] = new Dictionary <string, Material[]>();
        allMats[1] = new Dictionary <string, Material[]>();

        /*
         * foreach (Mesh baseMesh in loadedBaseMeshes)
         * {
         *  allMats[0].Add(baseMesh.name, (Material[])Resources.LoadAll("Materials/Bases/" + baseMesh.name, typeof(Material)));
         * }
         * foreach (Mesh towerMesh in loadedTowerMeshes)
         * {
         *  allMats[1].Add(towerMesh.name, (Material[])Resources.LoadAll("Materials/Towers/" + towerMesh.name, typeof(Material)));
         * }
         */
    }
Example #2
0
    public static string MeshToString(MeshFilter mf)
    {
        Mesh m = mf.mesh;

        Material[] mats = mf.GetComponentInChildren <MeshRenderer>().sharedMaterials;

        StringBuilder sb = new StringBuilder();

        sb.Append("g ").Append(mf.name).Append("\n");
        foreach (Vector3 v in m.vertices)
        {
            sb.Append(string.Format("v {0} {1} {2}\n", v.x, v.y, v.z));
        }
        sb.Append("\n");
        foreach (Vector3 v in m.normals)
        {
            sb.Append(string.Format("vn {0} {1} {2}\n", v.x, v.y, v.z));
        }
        sb.Append("\n");
        foreach (Vector3 v in m.uv)
        {
            sb.Append(string.Format("vt {0} {1}\n", v.x, v.y));
        }
        for (int material = 0; material < m.subMeshCount; material++)
        {
            sb.Append("\n");
            sb.Append("usemtl ").Append(mats[material].name).Append("\n");
            sb.Append("usemap ").Append(mats[material].name).Append("\n");

            int[] triangles = m.GetTriangles(material);
            for (int i = 0; i < triangles.Length; i += 3)
            {
                sb.Append(string.Format("f {0}/{0}/{0} {1}/{1}/{1} {2}/{2}/{2}\n",
                                        triangles[i] + 1, triangles[i + 1] + 1, triangles[i + 2] + 1));
            }
        }
        return(sb.ToString());
    }