Making procedural prefabs?

Hey, I’m wondering if anyone has any idea on making a large number of prefabs(like 1024?) My problem is that I have a single 2k texture that holds all my textures for my game (each 64x64) and each is applied to a plain, so I need 1024 materials and 1024 prefabs, I was using a 512 texture to hold them but quickly this got filled making it not possible and I only want a single texture for everything but it was not so bad with 512 as there were only a small amount of textures but now I have 1024 it will take eons to make them all. Surely other people have had this problem?

Why not have one prefab which would be a plane and a custom script component.
Have the custom script alter the UV coords of the plane vertices to the appropriate texture point in some function.
Assign one material to this prefab with the texture in it.

At runtime, Instantiate, then call this new script’s function with the corner texture points.

This will get you the ability to have one material across all the objects (which should batch better then).

Interesting, but wouldn’t I need to have a variable of the texture coords and change this on everyone?

Yes. But you could set that up in a manager class somewhere. If all of the images are 64x64, you wouldn’t necessarily even need the specific coordinates, just use an index and divide and mod to find the appropriate box coordinates.

Sorry for the lack of code, but it’s 1:30 AM here, and I don’t feel like spinning up my editor right now.

Also the variable that holds the texture coordinates is the UV members of the mesh.

In another thread, someone had a similar problem, this is a slightly modified code of the one Loius posted(Thank you) it is a start, once i get something working i’ll post it :slight_smile:

function Start()
{
Frame (0);
}

function Frame(index : int) 
{   
    var mesh : Mesh = GetComponent(MeshFilter).mesh;
    size = Vector2 (1.0 / 32, 1.0 / 32);

        // Calculate index

        // repeat when exhausting all frames

        var uIndex : float = index % 32;

        var vIndex : float = index / 32;

        

        var t : float = 1.0 - (size.y * vIndex);

        var b : float = t - size.y;

        var l : float = size.x * uIndex;

        var r : float = l + size.x;
        
       mesh.uv = [ Vector2(l,t), Vector2(r,t), Vector2(l,b), Vector2(r,b) ];
}