首页 > 解决方案 > 将单独的图像上传到预制件的每个孩子

问题描述

所以我想我在这里错过了一个图书馆。而不是“MeshRenderer”,我使用“MeshRenderer[]”来对条目数组进行字符串化。简而言之,我正在尝试将单独的图像上传到预制件的每个孩子。我收到以下错误消息。 在此处输入图像描述

错误信息

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

}

}

标签: unity3dparent-childunity-editor

解决方案


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

boxCols[0].material.mainTexture = texture;


推荐阅读