unity 给materials赋值

2023-08-24 13:55:43 浏览数 (1)

代码语言:javascript复制
        for (int i = 0; i < trans.transform.GetComponent<Renderer>().materials.Length; i  )
        {
            trans.transform.GetComponent<Renderer>().materials[i] = PenMgr.mInstance.m_alphaMat;
        }

这样是赋值不了的 因为

代码语言:javascript复制
Note that like all arrays returned by Unity, this returns a copy of materials array. If you want to change some materials in it, get the value, change an entry and set materials back.

materials在unity中是值拷贝,要新建一个数组进行赋值

代码语言:javascript复制
            Material[] newBufMat = new Material[trans.transform.GetComponent<Renderer>().materials.Length];
            for (int i = 0; i < trans.transform.GetComponent<Renderer>().materials.Length; i  )
            {
                newBufMat[i] = PenMgr.mInstance.m_alphaMat;
            }


            trans.transform.GetComponent<Renderer>().materials = newBufMat;

0 人点赞