Renderer.material与Renderer.sharedMaterial的区别

简介: 此函数自动实例化材质并使它们对于此渲染器是唯一的。在销毁游戏对象时销毁材料是您的责任。 Resources.UnloadUnusedAssets 也会破坏材质,但通常仅在加载新关卡时调用。

Renderer.materialRenderer.sharedMaterial的区别

material

Returns the first instantiated Material assigned to the renderer.

Modifying material will change the material for this object only.

If the material is used by any other renderers, this will clone the shared material and start using it from now on.

Note:This function automatically instantiates the materials and makes them unique to this renderer. It is your responsibility to destroy the materials when the game object is being destroyed. Resources.UnloadUnusedAssets also destroys the materials but it is usually only called when loading a new level.

返回分配给渲染器的第一个实例化材质。

修改材质只会更改此对象的材质。

如果该材质被任何其他渲染器使用,这将克隆共享材质并从现在开始使用它。

笔记: 此函数自动实例化材质并使它们对于此渲染器是唯一的。在销毁游戏对象时销毁材料是您的责任。 Resources.UnloadUnusedAssets 也会破坏材质,但通常仅在加载新关卡时调用。

usingUnityEngine;

usingSystem.Collections;

 

publicclassExampleClass : MonoBehaviour

{

   Materialm_Material;

 

   voidStart()

   {

       //从游戏对象的渲染器中获取材质

       m_Material=GetComponent<Renderer>().material;

       print("Materials "+Resources.FindObjectsOfTypeAll(typeof(Material)).Length);

   }

 

   voidUpdate()

   {

       if (Input.GetKeyDown(KeyCode.A))

       {

           //输出游戏对象被销毁前的材质数量

           print("Materials "+Resources.FindObjectsOfTypeAll(typeof(Material)).Length);

           //销毁游戏对象

           Destroy(gameObject);

       }

   }

 

   voidOnMouseOver()

   {

       // 当鼠标悬停在游戏对象上时更改游戏对象的颜色

       m_Material.color=Color.red;

   }

 

   voidOnMouseExit()

   {

       //当鼠标退出游戏对象时将颜色改回白色

       m_Material.color=Color.white;

   }

 

   voidOnDestroy()

   {

       //销毁实例

       Destroy(m_Material);

       //输出材料数量以显示实例是否被删除

       print("Materials "+Resources.FindObjectsOfTypeAll(typeof(Material)).Length);

   }

}

sharedMaterial

Modifying sharedMaterial will change the appearance of all objects using this material, and change material settings that are stored in the project too.

It is not recommended to modify materials returned by sharedMaterial. If you want to modify the material of a renderer use material instead.

修改 sharedMaterial 将改变所有使用此材质的对象的外观,并更改存储在项目中的材质设置。 不建议修改 sharedMaterial 返回的材质。

如果要修改渲染器的材质,请改用material

总结

Renderer.sharedMaterial:当多个Renderer共用一个材质时,修改Renderer.sharedMaterial将修改所有引用它的Renderer。Renderer.material:修改Renderer.material只会影响Renderer本身。

运行实例

 

usingSystem.Collections;

usingSystem.Collections.Generic;

usingUnityEngine;

usingUnityEngine.EventSystems;

 

publicclassChange_T : MonoBehaviour

{

 

    publicGameObjectCube;

    //申请GameObject类型的变量 储存Cube模型

 

    publicTextureCard_01;

    //申请Texture类型的变量  储存Card_01图片

 

    // Use this for initialization

    voidStart()

    {

       

    }

 

    // Update is called once per frame

    voidUpdate()

    {

       

    }

 

    //换贴图的按钮函数

    publicvoidButton_T()

    {  

        Cube.GetComponent<Renderer>().sharedMaterial.mainTexture=Card_01;

        //将Cube模型材质的主贴图替换为Card_01

    }

}

 

由图可见,此时Cube模型的材质是Mat_Red,是没有贴图的;我们运行一次并按下Button按钮可见,Mat_Red的贴图已经换成了Card_01


相关文章
|
9月前
|
编解码 边缘计算 vr&ar
注视点渲染Foveated rendering是什么
Foveated rendering听起来好像是非常复杂的技术。但实际上注视点(foveation)的底层概念非常直接。使用人注视屏幕的信息,可以减少生成场景的运算资源,方式是使用高分辨率渲染人眼观看的小区哉,而场景外的其它区域(人的四周)采用更小的分辨率和更少的细节。注视点渲染主要用于显示技术,如VR头显和AR眼镜,对些场景资源优化至关重要。
176 1
|
11月前
249Echarts - 3D 曲面(Simple Surface)
249Echarts - 3D 曲面(Simple Surface)
177 0
|
iOS开发
Core Animation - 图层几何学<一>
Core Animation - 图层几何学<一>
92 0
Core Animation - 图层几何学<一>
|
算法 iOS开发
Core Animation - 视觉效果<三>
Core Animation - 视觉效果<三>
64 0
|
iOS开发
Core Animation - 视觉效果<一>
Core Animation - 视觉效果<一>
87 0
Core Animation - 视觉效果<二>
Core Animation - 视觉效果<二>
44 0
|
图形学
Core Animation -关键帧动画
Core Animation -关键帧动画
106 0
|
图形学
Unity里实现Sprite Renderer的阴影
将以下脚本附到产生Shadow的物体上: voidOnEnable(){ GetComponent().receiveShadows =true; GetComponent().castShadows =true; } 但是这是不够的,还需要Shader帮忙,下面的Shader请放到产生Shado...
3146 0
|
异构计算 编解码 开发者
Shader、Draw Call和渲染管线(Rendering Pipeline)
翻阅了很多资料,也做了不少笔记,决定还是对渲染进行一个总结,以巩固所学的东西。   《Real-Time Rendering, Third Edition》   (PDF的配图链接)将一个渲染流程分为三个阶段: 即 应用阶段(PApplication Stage)、几何阶段(Geometry St...
1665 0