当前位置:网站首页>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的对象。这点务必搞清楚。
边栏推荐
- MATLAB与Mysql数据库连接并数据交换(基于ODBC)
- 【图像处理基础】基于matlab GUI图像曲线调整系统【含Matlab源码 1923期】
- Convolutional neural network (CNN) explanation and tensorflow2 code implementation
- 360 mobile assistant is the first to access the app signature service system to help distribute privacy and security
- Shiniman household sprint A shares: annual revenue of nearly 1.2 billion red star Macalline and incredibly home are shareholders
- 花店橱窗布置【动态规划】
- [bug feedback] the problem of message sending time of webim online chat system
- DAST black box vulnerability scanner part 5: vulnerability scanning engine and service capability
- 俞敏洪:新东方并不存在倒下再翻身,摧毁又雄起的逆转
- Test comparison of linear model LN, single neural network SNN, deep neural network DNN and CNN
猜你喜欢

关于appium踩坑 :Encountered internal error running command: Error: Cannot verify the signature of (已解决)

传纸条【动态规划】

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

YuMinHong: New Oriental does not have a reversal of falling and turning over, destroying and rising again

Simple Lianliankan games based on QT

Some ways out for older programmers

Module 5 operation

用C#通过sql语句操作Sqlserver数据库教程

Netease Yunxin officially joined the smart hospital branch of China Medical Equipment Association to accelerate the construction of smart hospitals across the country

BN(Batch Normalization) 的理论理解以及在tf.keras中的实际应用和总结
随机推荐
MATLAB and MySQL database connection and data exchange (based on ODBC)
尚硅谷DolphinScheduler视频教程发布
What is the “ How to remove a custom form list?
Solution of valuenotifier < list < t > > monitoring problem in fluent
[solution] sword finger offer 15 Number of 1 in binary (C language)
证券注册开户有没有什么风险?安全吗?
VB.net类库(进阶——2 重载)
诗尼曼家居冲刺A股:年营收近12亿 红星美凯龙与居然之家是股东
Icml2022 | neurotoxin: a lasting back door to federal learning
这个算BUG吗?乱填的字母是否可以关闭
用C#通过sql语句操作Sqlserver数据库教程
Application and Optimization Practice of 100 million level monthly live national karaoke feed service in Tencent cloud mongodb
俞敏洪:新东方并不存在倒下再翻身,摧毁又雄起的逆转
SAP commerce cloud project Spartacus getting started
Configure redis master-slave and sentinel sentinel in the centos7 environment (solve the problem that the sentinel does not switch when the master hangs up in the ECS)
Centos7编译安装Redis
curl: (35) LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection
大龄程序员的一些出路
360 mobile assistant is the first to access the app signature service system to help distribute privacy and security
Yolov6: un cadre de détection de cibles rapide et précis est Open Source