【问题标题】:Write text on sprites in Unity在 Unity 中的精灵上写文字
【发布时间】:2017-10-07 20:46:24
【问题描述】:

我想在 Unity 中创建一个带有精灵的网格。每个单元格上都应该有一个数字。

应该是这样的

我的网格看起来像这样

所以我生成了单元格并将它们添加到一个名为

的空游戏对象中

地图

private GameObject cellPrefab;

private const int CELL_COUNT_X = 10; // create 100 cells
private const int CELL_COUNT_Y = 10;
private const float CELL_SPACING = 1.1f; // with a small spacing

private List<Cell> cells = new List<Cell>(); // store all cells here

private const int NUM_RANGE_MIN = 1; // cell value range
private const int NUM_RANGE_MAX = 10;

private void Start()
{
    cellPrefab = Resources.Load(StringCollection.CELL) as GameObject;

    for (int x = 0; x < CELL_COUNT_X; x++)
    {
        for (int y = 0; y < CELL_COUNT_Y; y++)
        {
            float spawnPosX = x * CELL_SPACING - CELL_COUNT_X / 2;
            float spawnPosY = y * CELL_SPACING - CELL_COUNT_Y / 2;
            GameObject cell = Instantiate(cellPrefab, new Vector2(spawnPosX, spawnPosY), cellPrefab.transform.rotation); // create the new cell

            cell.transform.SetParent(transform); // add the cell to the map

            Cell cellComponent = cell.GetComponent<Cell>();

            cellComponent.InitCell(Random.Range(NUM_RANGE_MIN, NUM_RANGE_MAX)); // init the cell value

            cells.Add(cellComponent); // add to list
        }
    }
}

每个单元格都附有这个脚本

private Text txtCellValue;
private int cellValue;

public void InitCell(int value)
{
    txtCellValue = transform.GetChild(0).GetChild(0).GetComponent<Text>(); // get the text component of the cell
    cellValue = value; // set the value
    txtCellValue.text = cellValue.ToString(); // update the GUI
}

所以在层次结构中,每个单元格都被添加到“地图”中并得到这个自己的层次结构

画布设置为“按屏幕大小缩放”,文本本身具有这些设置

我只想在这个精灵上写下单元格的值。也许有更干净的方法?

如果有人可以帮助解决这个问题,那就太好了!

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    您将为画布选择渲染模式“世界”。然后设置比例和宽度/高度值。

    此外,您还会记得对图层进行排序。如果你不为 UI 使用单独的相机,画布层会比精灵渲染器大。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-26
      • 1970-01-01
      • 2014-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-09
      • 1970-01-01
      相关资源
      最近更新 更多