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


相关文章
|
安全 图形学
|
图形学
[Unity3D]深度相机 Depth Camera
作为3D世界里最重要的窗口,摄像机的应用就显得很重要,毕竟在屏幕上看到的一切都得用摄像机矩阵变换得来的嘛。论坛上看到了一篇帖子讲非天空盒的背景做法,让我想起其实很多界面合成画面可以用摄像机之间的交互来实现(避开用GUI,效率问题我没尝试过,但是貌似用深度相机比gui好?以后试验下)。
|
8月前
|
Java
创建java文件 自动添加作者、时间等信息 – IDEA 技巧
创建java文件 自动添加作者、时间等信息 – IDEA 技巧
89 0
|
图形学
如何阻止 iCloud 同步某个文件夹?
如何阻止 iCloud 同步某个文件夹?
308 0
如何阻止 iCloud 同步某个文件夹?
|
9月前
|
Ubuntu Linux Swift
安装Swift编程语言的工具
安装Swift编程语言的工具
164 4
|
图形学
Unity里实现Sprite Renderer的阴影
将以下脚本附到产生Shadow的物体上: voidOnEnable(){ GetComponent().receiveShadows =true; GetComponent().castShadows =true; } 但是这是不够的,还需要Shader帮忙,下面的Shader请放到产生Shado...
3199 0
|
图形学
[Unity Editor] 清理Prefab使用的特定组件
工具实现 修改Prefab有两种方式,1)实例化Prefab,修改GameObject的实例,然后再替换工程里的资源(SaveAsPrefabAsset)。2)通过SerializedObject直接修改Prefab资源。
2490 0

热门文章

最新文章