Exemplo n.º 1
0
    public void RecreateSprite()
    {
        Vector3[] pos  = new Vector3[4];
        Vector3[] norm = new Vector3[4];
        Vector2[] uv   = new Vector2[4];

        int[] offsetsX = { 0, 0, 1, 1 };
        int[] offsetsY = { 1, 0, 0, 1 };

        float normalizedX = rect.x / spriteSheet.width;
        float normalizedY = rect.y / spriteSheet.height;
        float normalizedW = rect.width / spriteSheet.width;
        float normalizedH = rect.height / spriteSheet.height;

        for (int i = 0; i < 4; ++i)
        {
            pos[i].Set(offsetsX[i] * rect.width,
                       offsetsY[i] * rect.height,
                       0);

            norm[i].Set(0, 0, -1);

            //color[vertex] = new Color32(255, 255, 255, 255);
            uv[i].Set(normalizedX + offsetsX[i] * normalizedW, normalizedY + offsetsY[i] * normalizedH);
        }

        MeshFilter mf = GetComponent <MeshFilter>();

        if (mf == null)
        {
            mf = gameObject.AddComponent <MeshFilter>();
        }

        DestroyImmediate(mf.sharedMesh);
        mf.sharedMesh = new Mesh();

        mf.sharedMesh.vertices  = pos;
        mf.sharedMesh.normals   = norm;
        mf.sharedMesh.uv        = uv;
        mf.sharedMesh.triangles = new int[] { 0, 3, 1, 1, 3, 2 };

        mf.sharedMesh.RecalculateBounds();

        MeshRenderer mr = GetComponent <MeshRenderer>();

        if (mr == null)
        {
            mr = gameObject.AddComponent <MeshRenderer>();
            mr.sharedMaterial = MaterialDatabase.Get(spriteSheet);
        }

        if (mr.sharedMaterial.mainTexture != spriteSheet)
        {
            MaterialDatabase.Unload(mr.sharedMaterial.mainTexture as Texture2D);
            mr.sharedMaterial = MaterialDatabase.Get(spriteSheet);
        }
    }
Exemplo n.º 2
0
    public void MakeTilemap()
    {
        if (indexes == null || indexes.Length < _internalHeight * _internalWidth)
        {
            BuildMap();
        }

        MeshFilter mf = GetComponent <MeshFilter>();

        if (mf.sharedMesh == null)
        {
            mf.sharedMesh = new Mesh();
        }

        Vector3[] positions = new Vector3[_internalWidth * _internalHeight * 4];
        Vector2[] uv        = new Vector2[_internalWidth * _internalHeight * 4];
        Vector3[] normals   = new Vector3[_internalWidth * _internalHeight * 4];
        Color32[] color     = new Color32[_internalWidth * _internalHeight * 4];


        int[] triangles = new int[_internalWidth * _internalHeight * 6];

        float normalizedW = tileSize / (float)spriteSheet.width;
        float normalizedH = tileSize / (float)spriteSheet.height;

        int nbTileLine = spriteSheet.width / tileSize;

        for (int i = 0; i < _internalWidth; ++i)
        {
            for (int j = 0; j < _internalHeight; ++j)
            {
                int idx = (i * _internalHeight + j);

                for (int k = 0; k < 4; ++k)
                {
                    int[] offsetsX = { 0, 0, 1, 1 };
                    int[] offsetsY = { 0, 1, 1, 0 };

                    int vertex = idx * 4 + k;

                    positions[vertex].Set(i * tileSize + offsetsX[k] * tileSize,
                                          j * tileSize + offsetsY[k] * tileSize,
                                          0);
                    int line = indexes[i * height + j] / nbTileLine;
                    int col  = indexes[i * height + j] - line * nbTileLine;


                    normals[vertex].Set(0, 0, -1);

                    if (indexes[i * _internalHeight + j] == -1)
                    {
                        color[vertex] = new Color32(0, 0, 0, 0);
                        uv[vertex].Set(0, 0);
                    }
                    else
                    {
                        color[vertex] = new Color32(255, 255, 255, 255);
                        uv[vertex].Set(col * normalizedW + offsetsX[k] * normalizedW, line * normalizedH + offsetsY[k] * normalizedH);
                    }
                }

                triangles[idx * 6 + 0] = idx * 4;
                triangles[idx * 6 + 1] = idx * 4 + 1;
                triangles[idx * 6 + 2] = idx * 4 + 3;
                triangles[idx * 6 + 3] = idx * 4 + 1;
                triangles[idx * 6 + 4] = idx * 4 + 2;
                triangles[idx * 6 + 5] = idx * 4 + 3;
            }
        }

        mf.sharedMesh.Clear();
        mf.sharedMesh.vertices  = positions;
        mf.sharedMesh.uv        = uv;
        mf.sharedMesh.normals   = normals;
        mf.sharedMesh.colors32  = color;
        mf.sharedMesh.triangles = triangles;

        MeshRenderer mr = GetComponent <MeshRenderer>();

        if (mr.sharedMaterial != null)
        {
            MaterialDatabase.Unload(mr.sharedMaterial.mainTexture as Texture2D);
        }

        mr.sharedMaterial = MaterialDatabase.Get(spriteSheet);
    }