Okay, i have created a mesh using the method here.
http://unity3d.com/support/documentation/ScriptReference/Mesh-triangles.html
I have it showing up when the game starts, now i just want to know how to make a texture show now.
Okay, i have created a mesh using the method here.
http://unity3d.com/support/documentation/ScriptReference/Mesh-triangles.html
I have it showing up when the game starts, now i just want to know how to make a texture show now.
Okay i figured it out. My problem was i didnt have a resources folder. :D ( fail, i know ) I guess i assumed it would automatically be created, or was somehow everything in the project panel. But i was wrong.
So for those who are noobs like myself ill try to explain all that i did.
Look in your project panel. If you dont see a folder named resources, click the create button, and create a folder, then name it resources. Anything you want to load with resources.load must be in their i believe, so drag whatever you want to load into it.
As for creating the mesh. The following code sample is assuming that you have created a list of vertices, normals, uv's and triangles.
gameObject.AddComponent("MeshFilter");
gameObject.AddComponent("MeshRenderer");
MeshFilter filter = GetComponent<MeshFilter>();
Mesh mesh = filter.mesh;
Renderer renderer = GetComponent<MeshRenderer>().renderer;
mesh.Clear();
mesh.vertices = vertices;
mesh.normals = normals;
mesh.uv = uvs;
mesh.triangles = triangles;
mesh.RecalculateNormals();
Material mat = Resources.Load("RoadMat") as Material;
renderer.material = mat;
Links to what i used to get me started creating the mesh
I figured it out when i started printing the materials i would load. Like i would run.
Material mat = Resources.Load("RoadMat") as Material;
print(mat);
And it would return null. So this told me, that maybe i was applying it right theoretically, but that in reality i wasnt apply anything. So i looked up the resources class in the manual. ( More specifically its load function )
http://unity3d.com/support/documentation/ScriptReference/Resources.Load.html
And when in the description it says:
Blockquote> The Resources folder can be anywhere inside the Assets folder. Blockquote
That stood out to me, so i opened up the project folder in windows explorder, and what do you know, no resources folder. So i created one, dragged the material into it. And everything went gravy.
I think you can achieve quickest results by exposing a public material field and use that material on your renderer. This is probably the preferred way unless you need to explicitly control the materials within the script. Once you see it works properly you can create materials from scratch if you need to. Remember to destroy any assets you create during runtime to prevent memory leak/pooling.
Here's how you could do a simple test:
var material : Material;
function BuildModel()
{
// ... Create mesh renderer
// ... Create mesh
// ... Generate vertices
// ... Generate texcoords
// ... etc etc etc
SetMaterial();
}
function SetMaterial()
{
renderer.material = material;
}
Just set the material in the inspector and see if it becomes applied.