这是一个我已经工作了几天的问题,我无法在这里找到问题或者可以提供帮助的团结论坛,有人可以帮忙吗?
我在团结3D C#中有代码,它采用六边形预制件并将其克隆到网格中。但是当我的代码在单击时仅更改六边形的颜色时,层次结构中的第一个会受到影响,如果我点击任何其他图块,则没有任何变化。没有错误。
HERE是一个显示我的意思的视频(你看不到鼠标,但我也尝试点击第一个旁边的瓷砖)。
网格生成代码:
//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");
}
}
}
如果有人能帮助我,我将不胜感激:)
答案 0 :(得分:2)
因为每个克隆都会调用它的OnGUI(),所以你会有很多&#34; Black&#34;在同一位置的按钮,你点击的按钮是第一个克隆的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");
}
}
}