Unity 设置材质属性事件
来源:互联网 发布:微商和淘宝的区别 编辑:程序博客网 时间:2024/05/02 01:06
材质着色器属性
在过场动画时,可能需要动态修改材质属性的事件,Unity 的材质Material
通过SetColor、SetVector等接口来更改设置其属性值。在编辑器代码中,有个MaterialProperty
公开类,作为材质属性设置和获取的接口。
它将材质属性类型分为5种:
public enum PropType{ Color, Vector, Float, Range, Texture}
将纹理类型分为3种:
public enum TexDim{ Unknown = -1, None, Tex2D = 2, Tex3D, Cube, Any = 6}
可获取和设置值包括colorValue、floatValue、vectorValue 和 textureValue,那么设计更改材质属性事件也可按此来设计。
材质属性更改事件
对事件的物体,做材质属性的修改。
using System;using UnityEngine;namespace CinemaDirector{ [CutsceneItemAttribute("Material", "Set Material Propery", CutsceneItemGenre.ActorItem)] public class SetMaterialProperyEvent : CinemaActorEvent { public enum PropType { Color, Vector, Float, Range, Texture } [Flags] public enum TextureModifyType { Texture = 1, TextureOffset = 2, TextureScale = 4 } public string shaderName = String.Empty; public PropType propType = PropType.Texture; public string propName = "_MainTex"; public Color colorValue; public Vector4 vectorValue; public float floatValue; public Texture textureValue; public int textureModifyType = (int)TextureModifyType.Texture; public Vector2 textureOffsetValue; public Vector2 textureScaleValue = Vector2.one; public override void Trigger(GameObject Actor) { if (!Application.isPlaying || string.IsNullOrEmpty(propName) || !Actor) { return; } Renderer[] mrs = Actor.GetComponentsInChildren<Renderer>(); foreach (var mr in mrs) { ModifyMaterialProp(mr); } } private void ModifyMaterialProp(Renderer mr) { if (!mr) { return; } Material mat = mr.material; if (!mat || !mat.shader) { return; } if (mat.shader.name != shaderName) { return; } switch (propType) { case PropType.Color: mat.SetColor(propName, colorValue); break; case PropType.Vector: mat.SetVector(propName, vectorValue); break; case PropType.Float: case PropType.Range: mat.SetFloat(propName, floatValue); break; case PropType.Texture: if (((TextureModifyType)textureModifyType & TextureModifyType.Texture) != 0) { mat.SetTexture(propName, textureValue); } if (((TextureModifyType)textureModifyType & TextureModifyType.TextureOffset) != 0) { mat.SetTextureOffset(propName, textureOffsetValue); } if (((TextureModifyType)textureModifyType & TextureModifyType.TextureScale) != 0) { mat.SetTextureScale(propName, textureScaleValue); } break; } } }}
SetMaterialProperyEvent 的检视器设计
虽然SetMaterialProperyEvent
脚本可以达到运行时修改材质属性,但是在编辑时,需要手动去查看此时材质所拥有的属性,是比较麻烦的。可以做个编辑器类,自动获取当前所有可更改的材质属性,让用户去选择。
新建SetMaterialProperyEventInspector
脚本,在OnEnable的时候,通过MaterialEditor.GetMaterialProperties来获得材质的所有属性。接着,将所有的属性描述列出来,让用户选择即可。
0 0
- Unity 设置材质属性事件
- 【Unity】关于一个材质球有多个属性
- unity修改材质属性和更换shader
- 【Unity】 Unity设置材质的渲染模式(RenderingMode)
- unity标准材质球动态改值和共用材质不同设置问题
- unity双面材质攻略
- unity材质的切换
- Unity材质动画
- Unity材质清理器
- Unity材质动画
- Unity Custom PBR材质
- [cnblogs镜像]unity 模型 材质 贴图 关系;着色器属性
- unity中把材质截图当材质的matcap材质
- 材质脚本属性
- Ogre材质脚本属性
- 材质脚本属性
- 材质脚本属性
- ogre材质脚本属性
- 不要QUARTZ一直输出信息
- [Android 知识点] 网络请求框架Retrofit使用
- 9. OpenGL--抗锯齿
- Javascript中"=="和"==="的区别
- 比努力更重要的是学会“简单思考”
- Unity 设置材质属性事件
- linux二进制信号量的概念
- 2016 年谷歌开源了这些超酷炫的项目
- 使用CMake编译代码教程之1
- git小分支
- 微信公众号开发入门
- 10. OpenGL--雾
- 设置对话框、static和group的背景色和字体颜色
- Android Shape应用