当前位置:网站首页>Unity 设置Material、Shader的方法
Unity 设置Material、Shader的方法
2022-06-26 21:54:00 【Peter_Gao_】
设置贴图的基本方法
// 设置贴图
Texture mTexture = Resources.Load("myTexture", typeof(Texture )) as Texture;
material.SetTexture("_MainTex", mTexture );
// 设置整数
material.SetInt("_Int", 1);
// 设置浮点
material.SetFloat("_Float", 0.1f);
// 设置颜色 rgba
material.SetColor("_Color", Color.white);
// 设置向量 xyzw
material.SetVector("_Vector", new Vector4());
// shader的正常接口是没有bool类型的。但通过#pragma multi_compile __ UNITY_name可以实现
// 设置shader的UNITY_name为true
material.EnableKeyword("UNITY_name");
// 设置shader的UNITY_name为true
material.DisableKeyword("UNITY_name");
设置控制shader的几种基本方法
1 直接设置Material文件
使用unity编辑器直接定位包含目标shader的Material,并在脚本中访问。所有引入该Material的物体都会发生改变.
1,在脚本中建立公共Material参数
2,将脚本绑定到某物体上
3,在unity edtor中找到包含Shader的Material文件并拖拽到刚才的脚本对应参数上
public class shaderController : MonoBehaviour
{
public Material material;
void Update()
{
material.SetFloat("_Float", Mathf.Sin(Time.frameCount * 0.01f));
}
}
2 设置资源中的Material文件
直接设置Material文件中的Shader,所有引入该Material的物体都会发生改变
目标Material必须在Asset/Resources文件夹下。
public class shaderController : MonoBehaviour
{
public Material material;
void Start()
{
// typeof(Material)是为了防止不同类型文件重名
material = Resources.Load("rshader", typeof(Material)) as Material;
}
void Update()
{
//设置浮点数值
material.SetFloat("_Float", Mathf.Sin(Time.frameCount * 0.01f));
}
}
3 通过指定shader设置
该设置只会影响带有当前新建的Material的物体,不会影响其他带有该shader的物体。
1,读取shader资源
2,在内存中创建Material并设置相关参数
3,将新建的Material赋给目标物体
public class shaderController : MonoBehaviour
{
public Material material;
void Start()
{
// 获取物体的MeshRenderer组件
MeshRenderer mr = GameObject.Find("Cube222").GetComponent<MeshRenderer> ();
// 根据渲染器路径找到渲染器。跟资源目录无关
Shader shader = Shader.Find("Custom/3d");
// 新建Material
material = new Material(shader);
// 将新材质赋给物体
mr.material = material;
}
void Update()
{
material.SetFloat("_Float", Mathf.Sin(Time.frameCount * 0.1f));
}
}
4 通过目标物体直接设置,影响所有的物体
该设置会影响所有引用与该物体相同Material的物体
1,获取目标物体
2,获取物体sharedMaterial属性
3,设置
public class shaderController : MonoBehaviour
{
public MeshRenderer mr;
void Start()
{
// 获取包含Material的MeshRenderer组件
mr = GameObject.Find("Cube222").GetComponent<MeshRenderer> ();
}
void Update()
{
//sharedMaterial代表设置原始的Material,所有引用该材质的对象都会被影响。
//material代表当前的Material,实际上它等于在内存中新建了一个Mertairl。如果不手动释放它会造成内存泄漏。该修改只会影响当前对象
mr.sharedMaterial.SetFloat("_Float", Mathf.Sin(Time.frameCount * 0.1f));
}
}注意是: sharedMaterial
5 通过目标物体直接设置,只影响当前的物体
该设置只会影响当前设置Material的物体
1,获取目标物体
2,获取物体Material属性
3,设置
public class shaderController : MonoBehaviour
{
public MeshRenderer mr;
void Start()
{
// 获取包含Material的MeshRenderer组件
mr = GameObject.Find("Cube222").GetComponent<MeshRenderer> ();
}
void Update()
{
//sharedMaterial代表设置原始的Material,所有引用该材质的对象都会被影响。
//material代表当前的Material,实际上它等于在内存中新建了一个Mertairl。如果不手动释放它会造成内存泄漏。该修改只会影响当前对象
//material赋值的过程:
//1.Material m = mr.materia;
//2.设置m.set....;
//3.mr.material = m;
mr.material.SetFloat("_Float", Mathf.Sin(Time.frameCount * 0.1f));
}
}注意是: material
如果设置无效要注意的问题
目标shader的渲染器路径是否有重名。
如果是通过资源文件读取,需要确保它在Asset/Resources文件夹下
区分materil和sharemateril的区别。实际上直接设置materil等于实例化了一个新的materil并赋给对象,如果不手动销毁,这个新的materil会一直在内存中驻留。 而sharemateril则是修改已有的materil,但这个修改会改变所有引用了该materil的对象。这点务必搞清楚。
边栏推荐
- Netease Yunxin officially joined the smart hospital branch of China Medical Equipment Association to accelerate the construction of smart hospitals across the country
- 【图像处理基础】基于matlab GUI图像曲线调整系统【含Matlab源码 1923期】
- Matrix derivation and its chain rule
- Android mediacodec hard coded H264 file (four), ByteDance Android interview
- 股票炒股注册开户有没有什么风险?安全吗?
- leetcode:152. 乘积最大子数组【考虑两个维度的dp】
- MacOS环境下使用HomeBrew安装[email protected]
- leetcode:1567. Length of the longest subarray whose product is a positive number [dp[i] indicates the maximum length ending with I]
- JupyterLab 常用配置
- Different subsequence problems I
猜你喜欢
![[solution] sword finger offer 15 Number of 1 in binary (C language)](/img/ab/149775ae8ed94464efdf6921c1022a.png)
[solution] sword finger offer 15 Number of 1 in binary (C language)

leetcode:141. 环形链表【哈希表 + 快慢指针】

What are the accounting elements

CVPR 2022 - Interpretation of selected papers of meituan technical team

Vi/vim editor

DAST black box vulnerability scanner part 5: vulnerability scanning engine and service capability

In 2022, where will the medium and light-weight games go?

CVPR 2022 | 美团技术团队精选论文解读

Y48. Chapter III kubernetes from introduction to mastery -- pod status and probe (21)

Implementation of collaborative filtering evolution version neuralcf and tensorflow2
随机推荐
Centos7 compiling and installing redis
vulnhub之dc8
Common concurrent testing tools and pressure testing methods
Is there any risk for flush to register and open an account? Is it safe?
「连续学习Continual learning, CL」最新2022研究综述
花店橱窗布置【动态规划】
尚硅谷DolphinScheduler视频教程发布
leetcode:152. Product maximum subarray [consider DP of two dimensions]
SAP Spartacus 中的依赖注入 Dependency Injection 介绍
Which securities company is the most convenient, safe and reliable for opening an account
这个算BUG吗?乱填的字母是否可以关闭
用C#通过sql语句操作Sqlserver数据库教程
Kdd2022 𞓜 unified session recommendation system based on knowledge enhancement prompt learning
numpy中mgrid的用法
The importance of using fonts correctly in DataWindow
VB.net类库,获取屏幕内鼠标下的颜色(进阶——3)
Parsing complex JSON in fluent
Yolov6: the fast and accurate target detection framework is open source
【图像处理基础】基于matlab GUI图像直方图均衡化系统【含Matlab源码 1924期】
Brief analysis of the self inspection contents of the blue team in the attack and defense drill