我正在尝试更改游戏对象(尤其是多维数据集)以在运行时更改材质。
我没有尝试按名称搜索素材,而是决定尝试一种不同的方法,为一些游戏对象分配预设的素材,然后当我想更改某些素材时,我尝试从这些素材中获取素材。预制物品。
我已经花了一些时间来解决这个问题,发现了一些可能的错误,并进行了修复,但是我仍然没有得到所需的结果。
我还使用调试功能,下面将提供更多信息。
因此,我首先创建了一些材料作为全局变量。
public Material MaterialGrass;
public Material MaterialRock;
public Material MaterialSand;
public Material MaterialWater;
然后,我正在尝试从预设的游戏对象中加载实际材料。
GameObject MainVariablesObject = GameObject.Find("MainVariablesObject");
GlobalVariablesScript thisGlobalVariablesScript = MainVariablesObject.GetComponent<GlobalVariablesScript>();
thisGlobalVariablesScript.MaterialGrass = GameObject.Find("GrassTexture").GetComponent<MeshRenderer>().materials[0];
thisGlobalVariablesScript.MaterialRock = GameObject.Find("RockTexture").GetComponent<MeshRenderer>().materials[0];
thisGlobalVariablesScript.MaterialSand = GameObject.Find("SandTexture").GetComponent<MeshRenderer>().materials[0];
thisGlobalVariablesScript.MaterialWater = GameObject.Find("WaterTexture").GetComponent<MeshRenderer>().materials[0];
游戏视图分为9个正方形。
GameObject CubeFront0 = GameObject.Find("CubeFront0");
GameObject CubeFront1 = GameObject.Find("CubeFront1");
GameObject CubeFront2 = GameObject.Find("CubeFront2");
GameObject CubeLeft0 = GameObject.Find("CubeLeft0");
GameObject CubeLeft1 = GameObject.Find("CubeLeft1");
GameObject CubeLeft2 = GameObject.Find("CubeLeft2");
GameObject CubeRight0 = GameObject.Find("CubeRight0");
GameObject CubeRight1 = GameObject.Find("CubeRight1");
GameObject CubeRight2 = GameObject.Find("CubeRight2");
现在我仍然无法解决的部分:
public static void DisplayPlayerView()
{
GameObject MainVariablesObject = GameObject.Find("MainVariablesObject");
GlobalVariablesScript thisGlobalVariablesScript = MainVariablesObject.GetComponent<GlobalVariablesScript>();
DefineTexturesSources();
GetSurroundingCells();
GameObject CubeFront0 = GameObject.Find("CubeFront0");
GameObject CubeFront1 = GameObject.Find("CubeFront1");
GameObject CubeFront2 = GameObject.Find("CubeFront2");
GameObject CubeLeft0 = GameObject.Find("CubeLeft0");
GameObject CubeLeft1 = GameObject.Find("CubeLeft1");
GameObject CubeLeft2 = GameObject.Find("CubeLeft2");
GameObject CubeRight0 = GameObject.Find("CubeRight0");
GameObject CubeRight1 = GameObject.Find("CubeRight1");
GameObject CubeRight2 = GameObject.Find("CubeRight2");
GameObject[] Cubes = new GameObject[] { CubeFront0, CubeFront1, CubeFront2,
CubeLeft0, CubeLeft1, CubeLeft2,
CubeRight0, CubeRight1, CubeRight2};
char[] Map = new char[] { thisGlobalVariablesScript.Front0, thisGlobalVariablesScript.Front1, thisGlobalVariablesScript.Front2,
thisGlobalVariablesScript.Left0, thisGlobalVariablesScript.Left1, thisGlobalVariablesScript.Left2,
thisGlobalVariablesScript.Right0, thisGlobalVariablesScript.Right1, thisGlobalVariablesScript.Right2};
for (int i = 0; i < 9; i++)
{
Material thisMaterial = Cubes[i].GetComponent<MeshRenderer>().materials[0];
//MeshRenderer thisMaterial = Cubes[i].GetComponent<MeshRenderer>();
if (Map[i] == 'G')
thisMaterial = thisGlobalVariablesScript.MaterialGrass;
if (Map[i] == 'R')
thisMaterial = thisGlobalVariablesScript.MaterialRock;
if (Map[i] == 'S')
thisMaterial = thisGlobalVariablesScript.MaterialSand;
if (Map[i] == 'W')
thisMaterial = thisGlobalVariablesScript.MaterialWater;
Cubes[i].GetComponent<MeshRenderer>().materials[0] = thisMaterial;
Debug.Log(Cubes[i].name + " " + Cubes[i].GetComponent<MeshRenderer>().materials[0]);
}
我通过放置那些Debug.Log命令发现了什么:
1)我尝试“查找”的所有游戏对象都存在,这里没有错误。
2)“ thisMaterial”获得正确的材料,因此我认为将材料定义为变量并获得正确的数据没有问题。
3)我不再收到未附加材料的错误。
4)在“ for”循环中,“ thisMaterial”显示新的正确数据,而“ Cubes [i] .GetComponent()。materials [0]”显示旧数据-我一开始就手工设置的材料
5)数组“ Map”中的数据正确。
我想念什么?