Unity3d 将单独的图像上载到预置的每个子级

Unity3d 将单独的图像上载到预置的每个子级,unity3d,parent-child,unity-editor,Unity3d,Parent Child,Unity Editor,所以我想我错过了一个图书馆。我没有使用“MeshRenderer”,而是使用“MeshRenderer[]”来字符串化一个条目数组。简言之,我正试图将单独的图像上传到预制场的每个孩子。我收到以下错误消息。 使用UnityEngine; 使用UnityEditor; 使用System.IO; 公共类WallClick:单行为 { 字符串路径; 公共网络; 公共交通工具【】boxCols; void OnMouseDown() { boxCols=GetComponentsInChildren(

所以我想我错过了一个图书馆。我没有使用“MeshRenderer”,而是使用“MeshRenderer[]”来字符串化一个条目数组。简言之,我正试图将单独的图像上传到预制场的每个孩子。我收到以下错误消息。

使用UnityEngine;
使用UnityEditor;
使用System.IO;
公共类WallClick:单行为
{
字符串路径;
公共网络;
公共交通工具【】boxCols;
void OnMouseDown()
{
boxCols=GetComponentsInChildren();
path=EditorUtility.OpenFilePanel(“用png覆盖”、“用png覆盖”);
GetImage();
}
void EnableChildComponents()
{
foreach(以boxCols为单位)
{
col.enabled=true;
}
}
void GetImage()
{
if(路径!=null)
{
UpdateImage();
}
}
void UpdateImage()
{
字节[]imgByte=File.ReadAllBytes(路径);
纹理2d纹理=新纹理2d(2,2);
纹理。加载图像(imgByte);
boxCols.material.mainTexture=texture;//此处出错
}

}

boxCols是一个数组,您需要引用数组中的一个元素,该元素的类型为MeshRenderer。例如:


boxCols[0].material.mainTexture=texture

我对它投了赞成票。所以,它仍然只是一个特定立方体上的图像,而不管我在哪里单击预置。。。有没有办法区分预置中的每个多维数据集并上传单独的文件?
using UnityEngine;
using UnityEditor;
using System.IO;



public class WallClick : MonoBehaviour
{
string path;
public MeshRenderer col;
public MeshRenderer[] boxCols;





void OnMouseDown()
{
    boxCols = GetComponentsInChildren<MeshRenderer>();
    path = EditorUtility.OpenFilePanel("Overwrite with png", "", "png");
    GetImage();
}

void EnableChildComponents()
{
    foreach (MeshRenderer col in boxCols)
    {
        col.enabled = true;
    }
}

void GetImage()
{
    if (path != null)
    {
        UpdateImage();
    }
}
void UpdateImage()
{
    byte[] imgByte = File.ReadAllBytes(path);
    Texture2D texture = new Texture2D(2, 2);
    texture.LoadImage(imgByte);

    boxCols.material.mainTexture = texture; //Error here

}