0

这是我几天来一直在解决的问题,我无法在此处或可以提供帮助的统一论坛上找到问题,有人可以帮忙吗?

我有统一 3D C# 中的代码,它采用六边形预制件并将其克隆到网格中。但是,当我点击层次结构中的第一个时,我的代码改变了六边形的颜色,如果我点击任何其他瓷砖,没有任何变化。没有给出错误。

是一段视频,显示了我的意思(您看不到鼠标,但我也尝试单击与第一个相邻的图块)。

网格生成代码:

//Method to initialise Hexagon width and height
    void setSizes()
    {
        //renderer component attached to the Hex prefab is used to get the current width and height
        hexWidth = Hex.renderer.bounds.size.x;
        hexHeight = Hex.renderer.bounds.size.z;
    }

    //Method to calculate the position of the first hexagon tile
    //The center of the hex grid is (0,0,0)
    Vector3 calcInitPos()
    {
        Vector3 initPos;
        //the initial position will be in the left upper corner
        initPos = new Vector3(-hexWidth * gridWidthInHexes / 2f + hexWidth / 2, 0,
                              gridHeightInHexes / 2f * hexHeight - hexHeight / 2);

        return initPos;
    }

    //method used to convert hex grid coordinates to game world coordinates
    public Vector3 calcWorldCoord(Vector2 gridPos)
    {
        //Position of the first hex tile
        Vector3 initPos = calcInitPos();
        //Every second row is offset by half of the tile width
        float offset = 0;
        if (gridPos.y % 2 != 0)
            offset = hexWidth / 2;

        float x =  initPos.x + offset + gridPos.x * hexWidth;
        //Every new line is offset in z direction by 3/4 of the hexagon height
        float z = initPos.z - gridPos.y * hexHeight * 0.75f;
        return new Vector3(x, 0, z);
    }

    //Finally the method which initialises and positions all the tiles
    void createGrid()
    {
        //Game object which is the parent of all the hex tiles
        GameObject hexGridGO = new GameObject("HexGrid");

        for (float y = 0; y < gridHeightInHexes; y++)
        {
            for (float x = 0; x < gridWidthInHexes; x++)
            {
                //GameObject assigned to Hex public variable is cloned
                GameObject hex = (GameObject)Instantiate(Hex);
                //Current position in grid
                Vector2 gridPos = new Vector2(x, y);
                hex.transform.position = calcWorldCoord(gridPos);
                hex.transform.parent = hexGridGO.transform;
            }
        }
    }


    //The grid should be generated on game start
    void Start()
    {
        setSizes();
        createGrid();
    }
}

颜色变换代码:

using UnityEngine;
using System.Collections;

public class tilechange : MonoBehaviour {

    public Material Black;
    public Material Grey;
    public Material OGrey;
    public int colour;

    void Start()
    {
        renderer.material = Grey;
    }

    void OnGUI()
    {
        if (GUI.Button (new Rect (400,300, 60, 25), "Black")) 
        {
            colour = 2;
            Debug.Log ("colour black");
        }
        if (GUI.Button (new Rect (400,330, 60, 25), "Grey")) 
        {
            colour = 1;
            Debug.Log ("colour grey");
        }
        if (GUI.Button (new Rect (400,360, 60, 25), "Orange Border")) 
        {
            colour = 3;
            Debug.Log ("colour OGrey");
        }
    }


    void OnMouseDown () {

        Debug.Log ("click worked");


        if(colour == 1)
        {
            renderer.material = Grey;
            Debug.Log ("grey");
        }

        if(colour == 2)
        {
            renderer.material = Black;
            Debug.Log ("black");
        }
        if(colour == 3)
        {
            renderer.material = OGrey;
            Debug.Log ("OGrey");
        }
    }
}

如果有人可以帮助我,将不胜感激:)

4

1 回答 1

2

因为每个克隆都会调用它的 OnGUI(),所以在同一个位置会有很多“黑色”按钮,所以你点击的按钮就是第一个克隆的 OnGUI 按钮。

您不应该让克隆对象调用 OnGUI()。您可以使用 PlayerPref() 来存储价值。

有一个对象来处理 GUI。

public class MyGUI : MonoBehaviour {
    void OnGUI()
    {
        if (GUI.Button(new Rect(400, 300, 60, 25), "Black"))
        {
            Debug.Log("colour black");
            PlayerPrefs.SetInt("Colour", 2);
        }
        if (GUI.Button(new Rect(400, 330, 60, 25), "Grey"))
        {
            Debug.Log("colour grey");
            PlayerPrefs.SetInt("Colour", 1);
        }
        if (GUI.Button(new Rect(400, 360, 60, 25), "Orange Border"))
        {
            Debug.Log("colour OGrey");
            PlayerPrefs.SetInt("Colour", 3);
        }
    }
}  

然后颜色更改组件到每个克隆。

public class tilechange : MonoBehaviour {
    public Material Black;
    public Material Grey;
    public Material OGrey;

    void Start()
    {
        renderer.material = Grey;
    }

    void OnMouseDown()
    {

        Debug.Log("click worked");

        if (PlayerPrefs.GetInt("Colour") == 1)
        {
            renderer.material = Grey;
            Debug.Log("grey");
        }
        if (PlayerPrefs.GetInt("Colour") == 2)
        {
            renderer.material = Black;
            Debug.Log("black");
        }
        if (PlayerPrefs.GetInt("Colour") == 3)
        {
            renderer.material = OGrey;
            Debug.Log("OGrey");
        }
    }
}
于 2014-06-01T20:06:44.887 回答