Unity3D 动态加载材质
//设置材质
Public GameObject _goItme Texture2D _requestTexture = null; try { _requestTexture =getTexture2DByResourcePath(_strTexturePath); foreach (Component _comTemp in _goItme.GetComponentsInChildren(typeof(Transform))) { _comTemp.gameObject.AddComponent("BoxCollider");//添加碰撞器 if (_comTemp.renderer != null) { _comTemp.renderer.material.SetTexture("_MainTex", _requestTexture); Resources.UnloadUnusedAssets(); GC.Collect();//释放内存 } } } catch (Exception _e) { Debug.LogError("加载家居材质出错 !" + _e.GetBaseException()); }
public Texture2D getTexture2DByResourcePath(string f_strPath) { Texture2D _texture2D = new Texture2D(1, 1); Debug.Log(" Texture2D " + f_strPath); byte[] _bs = File.ReadAllBytes(f_strPath); _texture2D.LoadImage(_bs); return _texture2D; }