只有层次结构中的第一个克隆对象得到更改?Unity3D C#
这是一个我已经研究了好几天的问题,我在这里或unity论坛上都找不到有帮助的问题,有人能帮忙吗 我有unity 3D C#中的代码,它采用六边形预制并将其克隆到网格中。但是,我的代码在单击层次结构中的第一个时更改六边形的颜色,如果我单击任何其他平铺,则不会更改任何内容。没有给出错误 这是一个显示我的意思的视频(你看不到鼠标,但我也试着点击第一个相邻的瓷砖) 网格生成代码:只有层次结构中的第一个克隆对象得到更改?Unity3D C#,c#,unity3d,hexagonal-tiles,C#,Unity3d,Hexagonal Tiles,这是一个我已经研究了好几天的问题,我在这里或unity论坛上都找不到有帮助的问题,有人能帮忙吗 我有unity 3D C#中的代码,它采用六边形预制并将其克隆到网格中。但是,我的代码在单击层次结构中的第一个时更改六边形的颜色,如果我单击任何其他平铺,则不会更改任何内容。没有给出错误 这是一个显示我的意思的视频(你看不到鼠标,但我也试着点击第一个相邻的瓷砖) 网格生成代码: //Method to initialise Hexagon width and height void setSi
//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();
}
}
如果有人能帮我的话,我将不胜感激:)因为每个克隆都会调用它的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");
}
}
}
这个问题可能的重复现在已经改变,并且有很多进一步的改进。使用相同的代码,但问题不同
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");
}
}
}