【发布时间】:2022-05-22 03:13:39
【问题描述】:
我对 Unity3d 很陌生。我有一个包含 6 个四边形的预制件,使其成为一个立方体。我想为立方体的不同面添加图像纹理。 我从 Web 服务获取图像,因此我必须在脚本中添加或更改材料。 我面临的问题是,我无法在游戏对象中找到材质属性。
我试过下面的代码:
using UnityEngine;
using System.Collections;
public class shelfRuntime : MonoBehaviour {
public GameObject bottle;
public GameObject newBottle;
// Use this for initialization
void Start () {
iterateChildren(newBottle.transform.root);
GameObject rocketClone = (GameObject)Instantiate(bottle, bottle.transform.position, bottle.transform.rotation);
rocketClone.transform.localScale += new Vector3(1, 1, 1);
GameObject newBottleInMaking = (GameObject)Instantiate(newBottle, newBottle.transform.position, newBottle.transform.rotation);
Transform[] allChildren = newBottleInMaking.GetComponentsInChildren<Transform>();
foreach (Transform child in allChildren)
{
// do whatever with child transform here
}
}
void iterateChildren(Transform trans)
{
Debug.Log(trans.name);
if (trans.name == "Back") {
var ting = trans.gameObject.GetComponent<Renderer>();
// trans.renderer.material // there is no material property here
}
// Do whatever logic you want on child objects here
if (trans.childCount == 0) return;
foreach (Transform tran in trans)
{
iterateChildren(tran);
}
}
// Update is called once per frame
void Update () {
}
}
如何将材质设置为四边形?我的预制件中有 6 个四边形。
【问题讨论】: