- Home /
How to apply UV to Mesh in 2D tilemap?
I am trying out example project from: http://studentgamedev.blogspot.no/2013/08/unity-voxel-tutorial-part-1-generating.html
I can render the pink square, but when I apply the UV, I don't see anything. The code is exactly as he has it, so I suspect the way I'm importing the image is incorrect.
Here is what I have done:
Right-click in project view, Import New Asset..., select the image.
Change settings on the image in Inspector View. (Note: tutorial author doesn't seem to have the Sprite Mode option due to an older Unity version. Mine is Unity 4.5 (latest) and was set to
Single
by default, but I changed it toNone
. Not sure if this is correct). Click APPLY.
Drag and drop the image to my game object. Run the code.
As you can see, the result is just a blank (and very dark) square. No uv image seems to have been applied. How do I get this to work?
As a side question, I also played with the Sprite Editor. If I select Multiple
for the Sprite Mode, I can create individual sprites by defining a grid on the spritesheet image. Is there a way to map these defined individual sprites to the mesh I'm generating in the code from the tutorial?
In other words, instead of importing the image the way the tutorial author has done it, could I use Unity's new Sprite Editor system to map the UV to the square mesh? If so, would it be very inefficient for mapping to many square meshes (would it scale to a large tiled world)?
EDIT Added code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(MeshCollider))]
public class PolygonGenerator : MonoBehaviour {
public List<Vector3> newVertices = new List<Vector3>();
public List<int> newTriangles = new List<int>();
public List<Vector2> newUV = new List<Vector2>();
private Mesh mesh;
public float tUnit = 0.25f;
private Vector2 tStone = new Vector2(0,0);
private Vector2 tGrass = new Vector2(0,1);
// Use this for initialization
void Start () {
mesh = GetComponent<MeshFilter> ().mesh;
float x = transform.position.x;
float y = transform.position.y;
float z = transform.position.z;
newVertices.Add (new Vector3 (x, y, z));
newVertices.Add (new Vector3 (x + 1, y, z));
newVertices.Add (new Vector3 (x + 1, y - 1, z));
newVertices.Add (new Vector3 (x, y - 1, z));
newTriangles.Add (0);
newTriangles.Add (1);
newTriangles.Add (3);
newTriangles.Add (1);
newTriangles.Add (2);
newTriangles.Add (3);
newUV.Add(new Vector2 (tUnit * tStone.x, tUnit * tStone.y + tUnit));
newUV.Add(new Vector2 (tUnit * tStone.x + tUnit, tUnit * tStone.y + tUnit));
newUV.Add(new Vector2 (tUnit * tStone.x + tUnit, tUnit * tStone.y));
newUV.Add(new Vector2 (tUnit * tStone.x, tUnit * tStone.y));
mesh.Clear ();
mesh.vertices = newVertices.ToArray();
mesh.triangles = newTriangles.ToArray();
mesh.uv = newUV.ToArray();
mesh.Optimize ();
mesh.RecalculateNormals ();
}
// Update is called once per frame
void Update () {
}
}
Can we see your code where you build your mesh and set your uvs?
@robertbu Sure, I've edited my post. It's nearly identical to the author's code except for the Require statements.
Answer by meshhat · Nov 05, 2014 at 05:53 PM
Solved. I was missing the material. I guess the tutorial implied its creation (I'm new to Unity. Thought I could directly apply the texture).
Your answer
Follow this Question
Related Questions
In my 2D platformer game, how would I create a height marker?, 1 Answer
Get a tile relative to its position in the world 1 Answer
How to check if an object hits the ground hard enough then add explosive force around it (2D) 1 Answer
Firing Projectiles in the same direction as my character is looking. 1 Answer
Box collider 2d detecting collisions while not actually colliding 1 Answer