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


相关文章
|
1月前
|
前端开发 开发者
Flutter Canvas 属性详解与实际运用
Flutter Canvas 属性详解与实际运用
53 1
|
前端开发 Android开发 iOS开发
react native 实现图片预览 图片保存 react-native-image-zoom-viewer
图片 预览,和保存 功能 应该是很常见的APP 功能 。实现起来也很简单。 这里用到的组件是:https://github.com/ascoders/react-native-image-viewer 看下新效果图: [图片上传中.
6435 0
|
JavaScript
vue中svg的使用,可改变icon颜色(svg-sprite-loader)
今天来和小伙伴们分享下之前改 UI 的笔记 , 由于项目需要,要在项目中使用设计师给的 icon 图标,还要改变 icon 的颜色。 幸好找到这么一个神器~ 插件:svg-sprite-loader 版本:@vue/cli 4.3.1 使用: 1. npm install -D svg-sprite-loader 2. 在 src/components/ 下创建 SvgIcon 组件 配置SvgIcon.vue <template> <svg :class="svgClass" aria-hidden="true"> <use :xlink:href="iconNa
2083 0
|
图形学
Unity里实现Sprite Renderer的阴影
将以下脚本附到产生Shadow的物体上: voidOnEnable(){ GetComponent().receiveShadows =true; GetComponent().castShadows =true; } 但是这是不够的,还需要Shader帮忙,下面的Shader请放到产生Shado...
3102 0
|
前端开发 Android开发 iOS开发
react native android 实现图片预览 图片保存 react-native-image-zoom-viewer
上一篇 介绍了ios 的图片预览 和图片保存 ,Android 实现起来就稍微复杂点, image.png android 的 CameraRoll 只支持 本地文件,解决方式就是把图片下载到本地 ,然后调用这个保存到相册的方法。
2266 0
|
图形学 前端开发
|
图形学 前端开发