天天看点

Unity 3D UV实现小地图

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
[ExecuteInEditMode]
public class MiniMapSprite : MonoBehaviour 
{
    public Texture texture;
    public Vector2 m_size;                 //ui大小
    public float showMapSize;              //拉近
    public Vector2 m_mapCentre;            //uv中心

    MeshFilter meshFilter;
    MeshRenderer meshRenderer;
    Mesh aMesh;
    Material material;

    Vector2 m_oldSize;
    float oldShowMapSize;
    Vector2 m_oldMapCentre;

    bool m_dirty = true;

    void Awake()
    {
        meshFilter = transform.GetComponent<MeshFilter>();

        meshRenderer = transform.GetComponent<MeshRenderer>();
        aMesh = new Mesh();
        material = new Material(Shader.Find("Sprite/Vertex Colored, Fast"));
        
    }
	void Start () 
    {
        meshFilter.mesh = GeneratePlaneMeshXZ();

        m_oldSize = m_size;
        oldShowMapSize = showMapSize;
        m_oldMapCentre = m_mapCentre;

	}
	
	// Update is called once per frame
	void Update () 
    {
        if (texture == null) return;

        if (m_size != m_oldSize) m_dirty = true;
        if (oldShowMapSize != showMapSize) m_dirty = true;
        if (m_oldMapCentre != m_mapCentre) m_dirty = true;

        if (m_dirty)
        {
            meshFilter.mesh = GeneratePlaneMeshXZ();
            meshRenderer.sharedMaterial = material;
            meshRenderer.sharedMaterial.mainTexture = texture;
            m_dirty = false;
            m_oldSize = m_size;
            oldShowMapSize = showMapSize;
            m_oldMapCentre = m_mapCentre;
        }
	}
    private Mesh GeneratePlaneMeshXZ()
    {
        Vector3[] aVertices = new Vector3[4];
        Vector3 centreV = new Vector3(0, 0, 0);
        aVertices[0] = centreV + new Vector3(m_size.x, m_size.y, 0.0f) / 2.0f;
        aVertices[1] = centreV + new Vector3(m_size.x, -m_size.y, 0.0f) / 2.0f;
        aVertices[2] = centreV + new Vector3(-m_size.x, -m_size.y, 0.0f) / 2.0f;
        aVertices[3] = centreV + new Vector3(-m_size.x, m_size.y, 0.0f) / 2.0f;
        aMesh.vertices = aVertices;

        if (texture == null) return aMesh;

        Vector2[] anUVs = new Vector2[4];
        float helfTextureSize = texture.width / 2.0f / showMapSize;//确定四个顶点位置
        Vector2 offset = new Vector2(texture.width / 2, texture.height / 2);//将坐标转到第一象限
        anUVs[2] = (new Vector2(m_mapCentre.x + helfTextureSize, m_mapCentre.y + helfTextureSize) + offset) / texture.height;
        anUVs[3] = (new Vector2(m_mapCentre.x + helfTextureSize, m_mapCentre.y - helfTextureSize) + offset) / texture.height;
        anUVs[0] = (new Vector2(m_mapCentre.x - helfTextureSize, m_mapCentre.y - helfTextureSize) + offset) / texture.height;
        anUVs[1] = (new Vector2(m_mapCentre.x - helfTextureSize, m_mapCentre.y + helfTextureSize) + offset) / texture.height;

        int[] aTriangles = new int[]
        {
            0,1,2,
            2,3,0,
        };

        aMesh.uv = anUVs;
        aMesh.triangles = aTriangles;
        aMesh.RecalculateBounds();

        return aMesh;
    }
   
}