unity3d - 将单独的图像上传到预制件的每个孩子
问题描述
所以我想我在这里错过了一个图书馆。而不是“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;
推荐阅读
- math - 如何通过绘制方程的实部和虚部来识别方程的根
- webpack - 如何配置 webpack 自动检测 .css 文件的变化,以及导出内部 bundle.js 功能
- azure-service-fabric - Service Fabric - 无论如何要防止在升级期间初始化旧版本的服务?
- timer - STM32F401RE PWM 模式在 Protues 中不起作用
- ios - 尝试“存档”IOS 颤振应用程序时出错
- java - 用于强制 Java 文件大小的 Maven 插件
- react-native - 在我的 React Native 应用程序中从 Ionic 应用程序获取 localStorage
- javascript - 如何在较小的屏幕上整齐地包装列表项
- java - Spring中2个请求参数的自定义验证
- r - 使用插入符号创建混淆矩阵