Skip to main content
Removed reference to time, remove the "edit" word so that the post shows up as a whole.
Source Link
Vaillancourt
  • 16.4k
  • 17
  • 56
  • 61

I know this question is pretty old, but thereThere is a way to do this without SpriteRenderers using Graphics.DrawMesh(). You can use this method to draw sprites by using properties that sprites have like vertices, triangles, uv, texture, etc. to construct a mesh and appropriate material.

EDIT: Example Here is an example

public class SpriteDrawer : MonoBehaviour {

// A list of textures we want to build materials for beforehand
// Using the same material saves draw calls
public List<Texture2D> textures;
public Material spriteMaterial;

Dictionary<Texture2D, Material> textureMats = new Dictionary<Texture2D, Material>();

public void Awake() {
    //fill up textureMats with materials
    foreach (Texture2D texture in textures) {
        textureMats.Add(texture, new Material(spriteMaterial) {
            mainTexture = texture
        });
    }
}

public void Draw(Sprite sprite, Vector3 pos) {
    Material mat;
    //check if we already made a prebuilt material, otherwise make a new one
    if (textureMats.ContainsKey(sprite.texture)) {
        mat = textureMats[sprite.texture];
    } else {
        mat = new Material(spriteMaterial) {
            mainTexture = sprite.texture
        };
    }
    //build a mesh for the sprite
    Mesh mesh = new Mesh() {
        vertices = Array.ConvertAll(sprite.vertices, x => (Vector3)x),
        triangles = Array.ConvertAll(sprite.triangles, x => (int)x),
        uv = sprite.uv
    };
    Graphics.DrawMesh(mesh, Matrix4x4.TRS(pos, Quaternion.identity, sprite.bounds.size), mat, 0);
}
}

NOTE: I haven't tested this script so it may or may not work

I know this question is pretty old, but there is a way to do this without SpriteRenderers using Graphics.DrawMesh(). You can use this method to draw sprites by using properties that sprites have like vertices, triangles, uv, texture, etc. to construct a mesh and appropriate material.

EDIT: Example

public class SpriteDrawer : MonoBehaviour {

// A list of textures we want to build materials for beforehand
// Using the same material saves draw calls
public List<Texture2D> textures;
public Material spriteMaterial;

Dictionary<Texture2D, Material> textureMats = new Dictionary<Texture2D, Material>();

public void Awake() {
    //fill up textureMats with materials
    foreach (Texture2D texture in textures) {
        textureMats.Add(texture, new Material(spriteMaterial) {
            mainTexture = texture
        });
    }
}

public void Draw(Sprite sprite, Vector3 pos) {
    Material mat;
    //check if we already made a prebuilt material, otherwise make a new one
    if (textureMats.ContainsKey(sprite.texture)) {
        mat = textureMats[sprite.texture];
    } else {
        mat = new Material(spriteMaterial) {
            mainTexture = sprite.texture
        };
    }
    //build a mesh for the sprite
    Mesh mesh = new Mesh() {
        vertices = Array.ConvertAll(sprite.vertices, x => (Vector3)x),
        triangles = Array.ConvertAll(sprite.triangles, x => (int)x),
        uv = sprite.uv
    };
    Graphics.DrawMesh(mesh, Matrix4x4.TRS(pos, Quaternion.identity, sprite.bounds.size), mat, 0);
}
}

NOTE: I haven't tested this script so it may or may not work

There is a way to do this without SpriteRenderers using Graphics.DrawMesh(). You can use this method to draw sprites by using properties that sprites have like vertices, triangles, uv, texture, etc. to construct a mesh and appropriate material.

Here is an example

public class SpriteDrawer : MonoBehaviour {

// A list of textures we want to build materials for beforehand
// Using the same material saves draw calls
public List<Texture2D> textures;
public Material spriteMaterial;

Dictionary<Texture2D, Material> textureMats = new Dictionary<Texture2D, Material>();

public void Awake() {
    //fill up textureMats with materials
    foreach (Texture2D texture in textures) {
        textureMats.Add(texture, new Material(spriteMaterial) {
            mainTexture = texture
        });
    }
}

public void Draw(Sprite sprite, Vector3 pos) {
    Material mat;
    //check if we already made a prebuilt material, otherwise make a new one
    if (textureMats.ContainsKey(sprite.texture)) {
        mat = textureMats[sprite.texture];
    } else {
        mat = new Material(spriteMaterial) {
            mainTexture = sprite.texture
        };
    }
    //build a mesh for the sprite
    Mesh mesh = new Mesh() {
        vertices = Array.ConvertAll(sprite.vertices, x => (Vector3)x),
        triangles = Array.ConvertAll(sprite.triangles, x => (int)x),
        uv = sprite.uv
    };
    Graphics.DrawMesh(mesh, Matrix4x4.TRS(pos, Quaternion.identity, sprite.bounds.size), mat, 0);
}
}

NOTE: I haven't tested this script so it may or may not work

added 62 characters in body
Source Link

I know this question is pretty old, but there is a way to do this without SpriteRenderers using Graphics.DrawMesh(). You can use this method to draw sprites by using properties that sprites have like vertices, triangles, uv, texture, etc. to construct a mesh and appropriate material.

EDIT: Example

public class SpriteDrawer : MonoBehaviour {

// A list of textures we want to build materials for beforehand
// Using the same material saves draw calls
public List<Texture2D> textures;
public Material spriteMaterial;

Dictionary<Texture2D, Material> textureMats = new Dictionary<Texture2D, Material>();

public void Awake() {
    //fill up textureMats with materials
    foreach (Texture2D texture in textures) {
        textureMats.Add(texture, new Material(spriteMaterial) {
            mainTexture = texture
        });
    }
}

public void Draw(Sprite sprite, Vector3 pos) {
    Material mat;
    //check if we already made a prebuilt material, otherwise make a new one
    if (textureMats.ContainsKey(sprite.texture)) {
        mat = textureMats[sprite.texture];
    } else {
        mat = new Material(spriteMaterial) {
            mainTexture = sprite.texture
        };
    }
    //build a mesh for the sprite
    Mesh mesh = new Mesh() {
        vertices = Array.ConvertAll(sprite.vertices, x => (Vector3)x),
        triangles = Array.ConvertAll(sprite.triangles, x => (int)x),
        uv = sprite.uv
    };
    Graphics.DrawMesh(mesh, Matrix4x4.TRS(pos, Quaternion.identity, sprite.bounds.size), mat, 0);
}
}

NOTE: I haven't tested this script so it may or may not work

I know this question is pretty old, but there is a way to do this without SpriteRenderers using Graphics.DrawMesh(). You can use this method to draw sprites by using properties that sprites have like vertices, triangles, uv, texture, etc. to construct a mesh and appropriate material.

EDIT: Example

public class SpriteDrawer : MonoBehaviour {

// A list of textures we want to build materials for beforehand
// Using the same material saves draw calls
public List<Texture2D> textures;
public Material spriteMaterial;

Dictionary<Texture2D, Material> textureMats = new Dictionary<Texture2D, Material>();

public void Awake() {
    //fill up textureMats with materials
    foreach (Texture2D texture in textures) {
        textureMats.Add(texture, new Material(spriteMaterial) {
            mainTexture = texture
        });
    }
}

public void Draw(Sprite sprite, Vector3 pos) {
    Material mat;
    //check if we already made a prebuilt material, otherwise make a new one
    if (textureMats.ContainsKey(sprite.texture)) {
        mat = textureMats[sprite.texture];
    } else {
        mat = new Material(spriteMaterial) {
            mainTexture = sprite.texture
        };
    }
    //build a mesh for the sprite
    Mesh mesh = new Mesh() {
        vertices = Array.ConvertAll(sprite.vertices, x => (Vector3)x),
        triangles = Array.ConvertAll(sprite.triangles, x => (int)x),
        uv = sprite.uv
    };
    Graphics.DrawMesh(mesh, Matrix4x4.TRS(pos, Quaternion.identity, sprite.bounds.size), mat, 0);
}
}

I know this question is pretty old, but there is a way to do this without SpriteRenderers using Graphics.DrawMesh(). You can use this method to draw sprites by using properties that sprites have like vertices, triangles, uv, texture, etc. to construct a mesh and appropriate material.

EDIT: Example

public class SpriteDrawer : MonoBehaviour {

// A list of textures we want to build materials for beforehand
// Using the same material saves draw calls
public List<Texture2D> textures;
public Material spriteMaterial;

Dictionary<Texture2D, Material> textureMats = new Dictionary<Texture2D, Material>();

public void Awake() {
    //fill up textureMats with materials
    foreach (Texture2D texture in textures) {
        textureMats.Add(texture, new Material(spriteMaterial) {
            mainTexture = texture
        });
    }
}

public void Draw(Sprite sprite, Vector3 pos) {
    Material mat;
    //check if we already made a prebuilt material, otherwise make a new one
    if (textureMats.ContainsKey(sprite.texture)) {
        mat = textureMats[sprite.texture];
    } else {
        mat = new Material(spriteMaterial) {
            mainTexture = sprite.texture
        };
    }
    //build a mesh for the sprite
    Mesh mesh = new Mesh() {
        vertices = Array.ConvertAll(sprite.vertices, x => (Vector3)x),
        triangles = Array.ConvertAll(sprite.triangles, x => (int)x),
        uv = sprite.uv
    };
    Graphics.DrawMesh(mesh, Matrix4x4.TRS(pos, Quaternion.identity, sprite.bounds.size), mat, 0);
}
}

NOTE: I haven't tested this script so it may or may not work

added 1372 characters in body
Source Link

I know this question is pretty old, but there is a way to do this without SpriteRenderers using Graphics.DrawMesh(). You can use this method to draw sprites by using properties that sprites have like vertices, triangles, uv, texture, etc. to construct a mesh and appropriate material.

EDIT: Example

public class SpriteDrawer : MonoBehaviour {

// A list of textures we want to build materials for beforehand
// Using the same material saves draw calls
public List<Texture2D> textures;
public Material spriteMaterial;

Dictionary<Texture2D, Material> textureMats = new Dictionary<Texture2D, Material>();

public void Awake() {
    //fill up textureMats with materials
    foreach (Texture2D texture in textures) {
        textureMats.Add(texture, new Material(spriteMaterial) {
            mainTexture = texture
        });
    }
}

public void Draw(Sprite sprite, Vector3 pos) {
    Material mat;
    //check if we already made a prebuilt material, otherwise make a new one
    if (textureMats.ContainsKey(sprite.texture)) {
        mat = textureMats[sprite.texture];
    } else {
        mat = new Material(spriteMaterial) {
            mainTexture = sprite.texture
        };
    }
    //build a mesh for the sprite
    Mesh mesh = new Mesh() {
        vertices = Array.ConvertAll(sprite.vertices, x => (Vector3)x),
        triangles = Array.ConvertAll(sprite.triangles, x => (int)x),
        uv = sprite.uv
    };
    Graphics.DrawMesh(mesh, Matrix4x4.TRS(pos, Quaternion.identity, sprite.bounds.size), mat, 0);
}
}

I know this question is pretty old, but there is a way to do this without SpriteRenderers using Graphics.DrawMesh(). You can use this method to draw sprites by using properties that sprites have like vertices, triangles, uv, texture, etc. to construct a mesh and appropriate material.

I know this question is pretty old, but there is a way to do this without SpriteRenderers using Graphics.DrawMesh(). You can use this method to draw sprites by using properties that sprites have like vertices, triangles, uv, texture, etc. to construct a mesh and appropriate material.

EDIT: Example

public class SpriteDrawer : MonoBehaviour {

// A list of textures we want to build materials for beforehand
// Using the same material saves draw calls
public List<Texture2D> textures;
public Material spriteMaterial;

Dictionary<Texture2D, Material> textureMats = new Dictionary<Texture2D, Material>();

public void Awake() {
    //fill up textureMats with materials
    foreach (Texture2D texture in textures) {
        textureMats.Add(texture, new Material(spriteMaterial) {
            mainTexture = texture
        });
    }
}

public void Draw(Sprite sprite, Vector3 pos) {
    Material mat;
    //check if we already made a prebuilt material, otherwise make a new one
    if (textureMats.ContainsKey(sprite.texture)) {
        mat = textureMats[sprite.texture];
    } else {
        mat = new Material(spriteMaterial) {
            mainTexture = sprite.texture
        };
    }
    //build a mesh for the sprite
    Mesh mesh = new Mesh() {
        vertices = Array.ConvertAll(sprite.vertices, x => (Vector3)x),
        triangles = Array.ConvertAll(sprite.triangles, x => (int)x),
        uv = sprite.uv
    };
    Graphics.DrawMesh(mesh, Matrix4x4.TRS(pos, Quaternion.identity, sprite.bounds.size), mat, 0);
}
}
Source Link
Loading