- Home /
Calling Functions Within A Script?
So far, I am creating a modeling program, similar to blender in which you can create 3D models.
The only problem ive had is exporting in game. I have a .obj exporter script, yet it is a editor script so it is apparently not usable in game!
The ISSUE I NEED YOU TO FIX IS..Calling this function in the script! -
My Code Of Thingy That I Need To Access :
using UnityEngine;
using System.Collections;
using System.IO;
using System.Text;
public class ObjExporter : MonoBehaviour {
public ObjExporter script;
public GameObject ToBeExported;
public static string MeshToString(MeshFilter mf) {
Mesh m = mf.mesh;
Material[] mats = mf.renderer.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();
}
void OnGUI () {
if(GUI.Button(new Rect(60,290,25,200),"Export Creation As .Obj"))
}
public static void MeshToFile(MeshFilter mf, string filename) {
using (StreamWriter sw = new StreamWriter(filename))
{
sw.Write(MeshToString(mf));
}
}
}
Basically, I need to call this function at the bottom of the script from a C#, (I could also do it from a JAVA script if thats the only thing you can help with!)
CALL THIS FUNCTION...
public static void MeshToFile () {
this is confusing,you just need to call the function at the top? if so, just use void Awake(){$$anonymous$$eshToFile();}
Answer by Loius · Oct 21, 2012 at 04:29 AM
Just don't put that function in an editor script. You can call things from other places.
ClassName.StaticFunctionName()
Called it using GetComponent... Now how can I make it export a whole variable like..
public static GameObject ToBeExported = GameObject.Find("ToBeExported");
$$anonymous$$yType thing;
thing = GetComponent($$anonymous$$yType);
thing.variable = "hello"