创建脚本实例时未调用启动函数

时间:2017-10-16 13:16:08

标签: c# unity3d unity3d-2dtools

我按照这个tutorial在Unity中组成了一个表格。

我喜欢做的是 (1)我喜欢在代码中动态更改Cell的宽度x高度。 (2)我喜欢将第一排网格的高度设置得高于其余网格。

我的代码是

public class PopulateCellsToLeftPanel : MonoBehaviour {
    public GameObject cell;
    private int numberOfCells;
    private float cellWidth;
    private float cellHeight;

    public PopulateCellsToLeftPanel(int num, float w, float h)
    {
        numberOfCells = num;
        cellWidth = w;
        cellHeight = h;        
    }
    // Use this for initialization
    void Start()
    {

        GameObject newObj; // Create GameObject instance
        GridLayoutGroup grid = this.GetComponent<GridLayoutGroup>();
        grid.cellSize = new Vector2(cellWidth, cellHeight);
        int numRow = numberOfCells % 4;
        for (int i = 0; i < numRow; i++)//row
        {
            for (int j = 0; j < 4; j++)//column
            {                
                newObj = (GameObject)Instantiate(cell, transform);
                if(i%2 == 0)
                    newObj.GetComponent<Image>().color = Color.white;
                else
                    newObj.GetComponent<Image>().color = Color.red;
            }
        }
    }

}

然后从主类开始,我尝试使用类的构造函数进行设置。

public class Historycanvas : MonoBehaviour, IPointerClickHandler
{
    PopulateCellsToLeftPanel pL;
    pL = new PopulateCellsToLeftPanel(30, CellWidth, CellHeight);

}

但是numberOfCells,cellWidth和cellHeight总是0.是否比类构造函数更早地调用void Start()

是否可以设置网格中第一行单元格的高度与其余单元格不同?

编辑:

我在构造函数和Start函数中都进行了Debug.Log,如下所示。

public PopulateCellsToLeftPanel(int num, float w, float h)
    {
        Debug.Log("i am from constructor start");
        numberOfCells = num;
        cellWidth = w;
        cellHeight = h;
        Debug.Log("i am from constructor end");
    }
    // Use this for initialization
    void Start()
    {
        Debug.Log("i am from Start start");
        GameObject newObj; // Create GameObject instance
        GridLayoutGroup grid = this.GetComponent<GridLayoutGroup>();
        grid.cellSize = new Vector2(cellWidth, cellHeight);
        int numRow = numberOfCells % 4;
        for (int i = 0; i < numRow; i++)//row
        {
            for (int j = 0; j < 4; j++)//column
            {                
                newObj = (GameObject)Instantiate(cell, transform);
                if(i%2 == 0)
                    newObj.GetComponent<Image>().color = Color.white;
                else
                    newObj.GetComponent<Image>().color = Color.red;
            }
        }
        Debug.Log("i am from Start end");
    }

但是我很惊讶,因为Start()函数的Debug.log打印时间早于构造函数()。请看下面的附图。这就是为什么我的变量总是0。 enter image description here

编辑2:

我试图创建一个单独的公共函数来将图像实例化为网格,如下所示,但不填充图像。它只在Start()中有效吗?因为如果我在Start()函数中放置相同的东西,它可以工作,而Start()的唯一问题是那些参数(int rows,float cellWidth,float cellHeight)我无法动态更改。我需要动态改变。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PopulateCellToLeftPanel : MonoBehaviour {

    public GameObject cell; 

    // Use this for initialization
    void Start()
    {


    }

    public void Plot(int rows, float cellWidth, float cellHeight)
    {
        Debug.Log("cell width " + cellWidth + " cell height " + cellHeight + " num rows " + rows);
        GameObject newObj; // Create GameObject instance
        GridLayoutGroup grid = this.GetComponent<GridLayoutGroup>();
        if (grid == null)
            Debug.Log("grid is null");
        grid.cellSize = new Vector2(cellWidth, cellHeight);

        for (int i = 0; i < rows; i++)//row
        {
            for (int j = 0; j < 4; j++)//column
            {
                newObj = (GameObject)Instantiate(cell, transform);
                if (i % 2 == 0)
                    newObj.GetComponent<Image>().color = Color.white;
                else
                    newObj.GetComponent<Image>().color = Color.red;
            }
        }
    }

}

主类

public class main: MonoBehaviour
{
   PopulateCellToLeftPanel pL = null;
    void Start()
    {
       pL = gameObject.AddComponent<PopulateCellToLeftPanel>();
       pL.Plot(13, CellWidth, CellHeight);
    }
}

我的控制台输出如下,网格为空。

enter image description here

0 个答案:

没有答案