Пример #1
0
    private HexCell CreateCell(Tile t, GameObject cellPrefabx)
    {
        int x = t.Pos.X;
        int y = t.Pos.Y;

        GameObject gameObjectCell = Instantiate(cellPrefabx);

        gameObjectCell.hideFlags = HideFlags.DontSave;         //.enabled = false;
        gameObjectCell.transform.SetParent(transform, false);
        gameObjectCell.name = "Ground " + x.ToString() + "," + y.ToString();

        HexCell cell = new HexCell();         // Instantiate<HexCell>(cellPrefab);

        cell.Cell    = gameObjectCell;
        cell.HexGrid = this;

        Vector2 gridPos  = new Vector2(x, y);
        Vector3 gridPos3 = CalcWorldPos(gridPos);

        cell.Tile = t;

        double height = t.Height;
        float  tileY;

        string materialName;

        if (height > 0.27 && height < 0.33)
        {
            materialName = "Materials/Sand";
            tileY        = 0.3f;
        }
        else if (height >= 0.26 && height <= 0.32)
        {
            materialName = "Materials/DarkSand";
            tileY        = 0.4f;
        }
        else if (height > 0.50 && height <= 0.52)
        {
            materialName            = "Materials/DarkWood";
            tileY                   = 0.9f;
            cell.NumberOfSmallTrees = 1;
        }
        else if (height > 0.47 && height <= 0.50)
        {
            materialName            = "Materials/Wood";
            tileY                   = 0.8f;
            cell.NumberOfSmallTrees = 0;
        }
        else if (height >= 0.46 && height <= 0.53)
        {
            materialName            = "Materials/LightWood";
            tileY                   = 0.7f;
            cell.NumberOfSmallTrees = 0;
        }
        else if (height >= 0.45 && height <= 0.55)
        {
            materialName            = "Materials/GrassDark";
            tileY                   = 0.6f;
            cell.NumberOfSmallTrees = 0;
        }
        else
        {
            materialName = "Materials/Grass";
            tileY        = 0.5f;
        }

        gridPos3.y = tileY / 2;
        gameObjectCell.transform.localPosition = gridPos3;

        //
        Material materialReource = Resources.Load <Material>(materialName);

        MeshRenderer meshRenderer = gameObjectCell.GetComponent <MeshRenderer>();

        meshRenderer.material = materialReource;

        //Material[] newMaterials = new Material[meshRenderer.materials.Length];

        /*
         * for (int i = 0; i < cell.meshRenderer.materials.Length; i++)
         * {
         *      Material material = cell.meshRenderer.materials[i];
         *
         *      //material.mainTexture = null;
         *      //material.globalIlluminationFlags = MaterialGlobalIlluminationFlags.EmissiveIsBlack;
         *      if (material.name.StartsWith("Player"))
         *      {
         *              //material.color = Color.red;
         *              cell.pheromaterial = material;
         *              material.color = materialReource.color;
         *
         *              //materialReource.name = "Player";
         *              //newMaterials[i] = materialReource;
         *      }
         *      else
         *      {
         *              material.color = materialReource.color;
         *              //materialReource.name = "Ground";
         *              //newMaterials[i] = materialReource;
         *      }
         *
         * }*/
        //meshRenderer.materials = newMaterials;

        cell.CreateMinerals();
        cell.CreateTrees(smallTrees);

        /*
         *
         *              Text label = Instantiate<Text>(cellLabelPrefab);
         *              label.rectTransform.SetParent(gridCanvas.transform, false);
         *              label.rectTransform.anchoredPosition = new Vector2(gridPos3.x, gridPos3.z);
         *              label.text = t.Pos.X.ToString() + "," + t.Pos.Y;
         */
        return(cell);
    }