通过代码创建材料并将其分配给对象 [英] Create material from code and assign it to an object

查看:91
本文介绍了通过代码创建材料并将其分配给对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Unity3d非常陌生,我有一个预制板,其中包含6个四边形,使其成为一个立方体.我想向立方体的不同面添加图像纹理. 我正在从Web服务获取图像,因此,我必须在脚本中添加或更改材料. 我面临的问题是,我无法在gameObject中找到材料属性.

I am very new to Unity3d,I have a prefab which contains 6 quads making it a cube .I want to add image textures to different faces of cube . I am getting images from webservice so ,I have to add or change material in script. problem I am facing is , I am not able to find material property in gameObject .

我尝试了以下代码:

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个四边形.

How to set material to quads ? there are 6 quads inside my prefab .

推荐答案

您将无法再直接在Unity中访问某些组件.您必须使用GetComponent获取组件(Renderer),然后从中访问材料.

You can no longer access some components directly in Unity. You must use GetComponent to get the component(Renderer) then access the material from it.

trans.renderer.material = ....

应更改为

trans.GetComponent<Renderer>().material = yourNewMaterial;

最后,当在Unity中创建多维数据集或四边形时,MeshRenderer会自动附加到它们上,而不是Renderer.因此,使用GetComponent<Renderer>()可能会遇到运行时错误.改为使用MeshRenderer.

Finally, when Cube or Quad is created in Unity, MeshRenderer is automtatically attached to them not Renderer. So, you might get run-time error with GetComponent<Renderer>(). Use MeshRenderer instead.

trans.GetComponent<MeshRenderer>().material = yourNewMaterial;


要在运行时创建材料:

Material myNewMaterial = new Material(Shader.Find("Standard"));

下面的示例将创建一个材质,为其指定标准着色器,然后将其从myTexture变量更改为纹理,然后再将其应用于GameObject.

The example below will create a Material, assign standard shader to it then change the texture to the texture from the myTexture variable before applying it to a GameObject.

public Texture myTexture;
void Start()
{
    //Find the Standard Shader
    Material myNewMaterial = new Material(Shader.Find("Standard"));
    //Set Texture on the material
    myNewMaterial.SetTexture("_MainTex", myTexture);
    //Apply to GameObject
    trans.GetComponent<MeshRenderer>().material = myNewMaterial;
}

这篇关于通过代码创建材料并将其分配给对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆