Unity:根据图像上方按钮的位置获取图像上的像素

时间:2016-09-01 14:03:19

标签: image unity3d grid getpixel

我的2D游戏;我有一个带有图像Component(带有sprite)和GridLayout组件的面板。 在面板上我还放了一个脚本,为面板创建按钮(作为孩子)

public void InitializeGrid(int rows, int columns, List<RbzFilteredWord> words)
{
    RectTransform myRect = GetComponent<RectTransform>();
    buttonHeight = myRect.rect.height / (float)rows;
    buttonWidth = myRect.rect.width / (float)columns;
    GridLayoutGroup grid = this.GetComponent<GridLayoutGroup>();
    grid.cellSize = new Vector2(buttonWidth, buttonHeight);

...
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < columns; j++)
        {
            button = (Button)Instantiate(prefab);
            button.transform.SetParent(transform, false);
          ...
}

问题:

每个按钮的文字必须指明该按钮枢轴位置的像素颜色(来自底层图像)

我知道这个方法是可用的:GetPixel(x,y),但我不知道我需要使用哪个坐标作为此方法的参数,因为我在一致的

1 个答案:

答案 0 :(得分:0)

x和y是纹理的像素二维数组中的位置索引。

那是:

  • x从0到纹理图像的宽度 - 1(以像素为单位)
  • y从0到高度 - 纹理图像的1(以像素为单位)