只有第一个克隆对象在层次结构中得到更改?Unity3D c#

本文关键字:Unity3D 层次结构 第一个 对象 | 更新日期: 2023-09-27 18:06:12

这是一个问题,我已经工作了几天,我还没有能够找到问题在这里或统一论坛,可以帮助,谁可以帮助?

我在unity 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");
        }
    }
}

如果有人能帮我,我将不胜感激:)

只有第一个克隆对象在层次结构中得到更改?Unity3D c#

因为每个克隆都会调用它的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");
        }
    }
}