Exemplo n.º 1
0
    /// <summary>
    /// 绘制面片
    /// </summary>
    private void DrawMesh(Vector3 pos)
    {
        try
        {
            KeyValuePair <int, int> rw = this.GetRowColByPos(pos);

            if (rw.Key > this.m_arrRow || rw.Value > this.m_arrCol)
            {
                Debug.LogError("选择的区域已出边界!");
                return;
            }

            MeshFilter filter = this.m_mesh.GetComponent <MeshFilter>();
            if (filter == null)
            {
                filter = this.m_mesh.AddComponent <MeshFilter>();
            }


            Mesh         mesh     = filter.mesh;
            MeshRenderer renderer = this.m_mesh.GetComponent <MeshRenderer>();
            if (renderer == null)
            {
                renderer = this.m_mesh.AddComponent <MeshRenderer>();
            }

            if (renderer.material == null)
            {
                Material material = new Material(Shader.Find("Diffuse"));
                renderer.sharedMaterial = material;
            }

            Mesh CacheMesh = TryGetMesh(rw);
            if (CacheMesh != null && CacheMesh != mesh)
            {
                filter.mesh     = CacheMesh;
                this.CurrenMesh = CacheMesh;
            }
            else if (mesh == null || CacheMesh == null)
            {
                if (this.m_meshType == GridShapeType.RegularHexagon)
                {
                    mesh = RegularHexagonGrid.Create <RegularHexagonGrid>(pos).mesh;
                }
                else
                {
                    mesh = SquareGrid.Create <SquareGrid>(pos).mesh;
                }

                if (mesh == null)
                {
                    Debug.LogError("该区域不能选择!");
                    return;
                }

                if (mesh.vertices.Length > 300)
                {
                    Debug.LogErrorFormat("顶点数据过大 {0} :{1}", rw.Key, rw.Value);
                }

                mesh.RecalculateNormals();
                mesh.RecalculateBounds();

                filter.mesh     = mesh;
                this.CurrenMesh = mesh;
                this.TryAddMesh(rw, mesh);
            }
        }
        catch (Exception ex)
        {
            Debug.LogException(ex);
        }
    }