将单独的图像上传到预制件的每个子项
Upload separate images to each child of a prefab
所以我想我这里少了一个图书馆。我没有使用 '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
}
}
boxCols 是一个数组,您需要引用数组中的一个元素,该元素将是MeshRenderer 类型。例如:
boxCols[0].material.mainTexture = texture;
所以我想我这里少了一个图书馆。我没有使用 '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
}
}
boxCols 是一个数组,您需要引用数组中的一个元素,该元素将是MeshRenderer 类型。例如:
boxCols[0].material.mainTexture = texture;