当前位置:网站首页>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的对象。这点务必搞清楚。
边栏推荐
- Which securities company is the most convenient, safe and reliable for opening an account
- Web crawler 2: crawl the user ID and home page address of Netease cloud music reviews
- Data governance does everything
- 线性模型LN、单神经网络SNN、深度神经网络DNN与CNN测试对比
- Is there any risk in opening a new bond registration account? Is it safe?
- curl: (35) LibreSSL SSL_ connect: SSL_ ERROR_ SYSCALL in connection
- Brief analysis of the self inspection contents of the blue team in the attack and defense drill
- In 2022, where will the medium and light-weight games go?
- 大龄程序员的一些出路
- leetcode:6107. 不同骰子序列的数目【dp六个状态 + dfs记忆化】
猜你喜欢

Using C to operate SQLSERVER database through SQL statement tutorial

Configuring assimp Library in QT environment (MinGW compiler)

The importance of using fonts correctly in DataWindow

会计要素包括哪些内容
![leetcode:6103. Delete the minimum score of the edge from the tree [DFS + connected component + value record of the subgraph]](/img/16/8dc63e6494b3f23e2685e287abc94c.png)
leetcode:6103. Delete the minimum score of the edge from the tree [DFS + connected component + value record of the subgraph]

Leetcode(452)——用最少数量的箭引爆气球

Word chess based on heuristic search

VB.net类库——4给屏幕截图,裁剪

Leetcode(763)——划分字母区间

Android IO, a first-line Internet manufacturer, is a collection of real questions for senior Android interviews
随机推荐
In 2022, where will the medium and light-weight games go?
DLA model (classification model + improved segmentation model) + deformable convolution
会计要素包括哪些内容
How to analyze financial expenses
The latest 2022 research review of "continuous learning, CL"
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)
CVPR 2022 | 美团技术团队精选论文解读
协同过滤进化版本NeuralCF及tensorflow2实现
Android IO, a first-line Internet manufacturer, is a collection of real questions for senior Android interviews
Hands on deep learning pytorch version 3 - Data Preprocessing
Using C to operate SQLSERVER database through SQL statement tutorial
Talk about my remote work experience | community essay solicitation
聊聊我的远程工作体验 | 社区征文
Is it safe for CICC fortune to open an account? I want to open an account to speculate in stocks.
MATLAB and MySQL database connection and data exchange (based on ODBC)
Simple Lianliankan games based on QT
数据治理啥都干
Different subsequence problems I
360手机助手首家接入APP签名服务系统 助力隐私安全分发
Brief analysis of the self inspection contents of the blue team in the attack and defense drill