Unity Battleships - 改变GameObject的颜色

时间:2017-02-24 16:12:55

标签: c# unity3d

对于学校我想创建一个小型战舰游戏。我想用OnMouseEnter事件悬停细胞,但没有任何反应,事件永远不会被调用。

我将代码分为3个脚本,数据,控制器和视图。

我的数据保存信息:

VERSION 300
DELIMITER ","
COORDSYS EARTH PROJECTION 8,79,"m",-2,49,0.99960127,400000,-100000
COLUMNS 2
  POSTCODEAREA CHAR (0)
  POSTCODEAREANAME CHAR (0)
DATA
POINT 384222.000 819125.000
POINT 518661.000 209809.000
POINT 407763.000 285514.000
POINT 371106.000 147748.000
.... as many points as many was areas in MID file (about 126)

我的控制器将处理玩家的行动:

public class CellCommonData : MonoBehaviour // Datastore
{
    public bool IsShipCell { get; set; } // is a ship there?

    public bool WasActivated { get; set; } // cell already clicked

    [SerializeField]
    private Color defaultColor; // default Color -> light blue

    public Color DefaultColor { get { return defaultColor; } }

    [SerializeField]
    private Color mouseOverColor; // hover Color -> dark blue

    public Color MouseOverColor { get { return defaultColor; } }

    [SerializeField]
    private Color hitColor; // hit Color -> green

    public Color HitColor { get { return defaultColor; } }

    [SerializeField]
    private Color noHitColor; // no Hit -> red

    public Color NoHitColor { get { return defaultColor; } }

    private void Start()
    {
        WasActivated = false; // the cell is not clicked on Start
    }
}

View处理单元格的“外观”:

public class CellCommonController : MonoBehaviour
{
    private CellCommonData cellData;
    private CellCommonView cellView;

    private void Start()
    {
        cellData = GetComponent<CellCommonData>();
        cellView = GetComponent<CellCommonView>();
    }

    private void OnMouseDown() // Cell clicked
    {
        if (!cellData.WasActivated) // Cell was not clicked before
        {
            if (cellData.IsShipCell) // ship hit?
            {
                cellView.SetCellColor(cellData.HitColor);
            }
            else // No ship there
            {
                cellView.SetCellColor(cellData.NoHitColor);
            }
            cellData.WasActivated = true; // Cell got clicked
        }
    }
}

我唯一的问题是,永远不会调用事件public class CellCommonView : MonoBehaviour { private CellCommonData cellData; private Renderer cellRenderer; private void Start() { cellData = GetComponent<CellCommonData>(); cellRenderer = GetComponent<Renderer>(); SetCellColor(cellData.DefaultColor); // colorize the cell with the default Color } public void SetCellColor(Color color) // Change the cells color { cellRenderer.material.SetColor("_Color", color); } private void OnMouseEnter() // cell got hovered { if (!cellData.WasActivated) // not clicked { SetCellColor(cellData.MouseOverColor); } } private void OnMouseExit() // leave the cell { if (!cellData.WasActivated) // not clicked { SetCellColor(cellData.DefaultColor); } } } OnMouseEnter()。但我不明白,那里有什么不对。

以下是它的图片:

SceneView

1 个答案:

答案 0 :(得分:1)

错误在CellCommonData.cs中,所有getter都返回defaultColor而不是相应的支持字段,如下所示:

public class CellCommonData : MonoBehaviour // Datastore
{
    public bool IsShipCell { get; set; } // is a ship there?

    public bool WasActivated { get; set; } // cell already clicked

    [SerializeField]
    private Color defaultColor; // default Color -> light blue

    public Color DefaultColor { get { return defaultColor; } }

    [SerializeField]
    private Color mouseOverColor; // hover Color -> dark blue

    public Color MouseOverColor { get { return mouseOverColor; } }

    [SerializeField]
    private Color hitColor; // hit Color -> green

    public Color HitColor { get { return hitColor; } }

    [SerializeField]
    private Color noHitColor; // no Hit -> red

    public Color NoHitColor { get { return noHitColor; } }

    private void Start()
    {
        WasActivated = false; // the cell is not clicked on Start
    }
}