Search Unity

Vanishing mesh

Discussion in 'Scripting' started by Lka, Aug 17, 2006.

  1. Lka

    Lka

    Joined:
    Aug 6, 2006
    Posts:
    297
    Hi
    I have a vanishing mesh: this short script works if I play it one time, the second time the mesh has 0 vertices and 0 triangles and I have to reload unity.

    The scene is composed by a camera, a directional light and a empty gameobject where I've attached this script.

    Can someone help me?
    Thanks
    Lka

    SaveVertexFaces.cs :
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. using System;
    5. using System.Collections;
    6. using System.Text;
    7.  
    8. class SaveVertexFaces : MonoBehaviour
    9. {
    10.     public string fileName;
    11.     public GameObject meshExport;
    12.  
    13.     public void Start()
    14.     {
    15.         Save(meshExport);        
    16.     }
    17.     private void Save(GameObject gobject)
    18.     {
    19.         Mesh meshP = ((MeshFilter)(gobject.GetComponent(typeof(MeshFilter)))).mesh;
    20.         Debug.Log("vertices count: " + meshP.vertexCount.ToString());
    21.         int i;
    22.         string s=string.Empty;
    23.         for(i=0;i<meshP.vertexCount;i++)
    24.             s+= "v "
    25.                 + meshP.vertices[i].x.ToString(LUtils.numberFormat) + " "
    26.                 + meshP.vertices[i].y.ToString(LUtils.numberFormat) + " "
    27.                 + meshP.vertices[i].z.ToString(LUtils.numberFormat) + '\n';
    28.         for(i=0;i<meshP.triangles.Length;i++)
    29.             s+= "t "
    30.                 + meshP.triangles[i].ToString(LUtils.numberFormat) + '\n';
    31.         LUtils.WriteFile(fileName, s);
    32.    }    
    33. }
    34.  
    LUtils.cs:
    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Text;
    5. #if !LGWINDEBUG
    6. using UnityEngine;
    7. #endif
    8.  
    9. class LUtils
    10. {
    11.     public static string AbsPath(string filename)
    12.     {
    13. #if LGWINDEBUG
    14.         return System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, filename);
    15. #else
    16.         return System.IO.Path.Combine(Application.dataPath, filename);
    17. #endif
    18.     }
    19.  
    20.     static System.Globalization.NumberFormatInfo nfi;
    21.     public static System.Globalization.NumberFormatInfo numberFormat
    22.     {
    23.         get
    24.         {
    25.             if (nfi == null)
    26.             {
    27.                 nfi = new
    28.                     System.Globalization.NumberFormatInfo();
    29.                 nfi.NumberDecimalSeparator = ".";
    30.                 nfi.NumberGroupSeparator = ",";
    31.             }
    32.             return nfi;
    33.         }
    34.     }
    35.  
    36.     public static void WriteFile(string filename, string text)
    37.     {
    38.         System.IO.FileStream fs;
    39.        
    40.         System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
    41.         byte[] ar = enc.GetBytes(text);
    42.        
    43.         fs = new System.IO.FileStream(filename, System.IO.FileMode.Create);
    44.         try
    45.         {
    46.             fs.Write(ar, 0, ar.Length);
    47.         }
    48.         finally
    49.         {
    50.             fs.Close();
    51.         }
    52.     }
    53. }
    54.  
     
  2. Lka

    Lka

    Joined:
    Aug 6, 2006
    Posts:
    297
    Instantiating the object all is working.
    Thanks
    Lka

    Code (csharp):
    1.  
    2.     public void Start()
    3.     {
    4.         Save(Instantiate(meshExport) as GameObject);        
    5.     }
    6.  
     
  3. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    hey no problem. glad we could help! ;) no seriously, glad you got it figured out!