当前位置:网站首页>代码修改模型的渐隐和渐现
代码修改模型的渐隐和渐现
2022-07-21 05:16:00 【一梭键盘任平生】
using UnityEngine; public class ModelAlphaTest : MonoBehaviour { public GameObject model; public bool isChange; float alpha = 0; private Renderer[] allMaterial; void Awake() { allMaterial = ChangeModelMaterial(); } /// <summary> /// 改变模型透明度 /// </summary> /// <param name="alpha"></param> /// <returns></returns> public Renderer[] ChangeModelMaterial() { if (model == null) return null; Renderer[] allMaterial = model.GetComponentsInChildren<Renderer>(); //修改材质的RenderingMode // foreach (var renderer1 in allMaterial) // { // foreach (var renderer1Material in renderer1.materials) // { // if (renderer1Material.HasProperty("_Mode")) // { // renderer1Material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); // renderer1Material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); // renderer1Material.SetInt("_ZWrite", 0); // renderer1Material.DisableKeyword("_ALPHATEST_ON"); // renderer1Material.DisableKeyword("_ALPHABLEND_ON"); // renderer1Material.EnableKeyword("_ALPHAPREMULTIPLY_ON"); // renderer1Material.SetFloat("_Mode", 2); // } // } // } return allMaterial; } void Update() { if (isChange) { alpha += Time.deltaTime; alpha = alpha > 1f ? 1f : alpha; foreach (var render in allMaterial) { if (render.material.HasProperty("_Color")) render.material.color = render.material.color.SetAlpha(alpha); if (!render.material.HasProperty("_TintColor")) continue; Color cor = render.material.GetColor("_TintColor"); cor.a = alpha; render.material.SetColor("_TintColor", cor); } } else { alpha -= Time.deltaTime; alpha = alpha < 0 ? 0 : alpha; foreach (var render in allMaterial) { if (render.material.HasProperty("_Color")) render.material.color = render.material.color.SetAlpha(alpha); if (!render.material.HasProperty("_TintColor")) continue; Color cor = render.material.GetColor("_TintColor"); cor.a = alpha; render.material.SetColor("_TintColor", cor); } } } }
alpha=1
alpha = 0.2
alpha=0
本人也只是用过一次 如有错漏请指正
边栏推荐
- Creative programming / primary school group (grades 4-6) - Graphic Creativity
- 关于Hololens2上AssetBundle资源管理、热更新问题(个人Hololens2进阶开发小总结三)
- Winter vacation learning summary
- QT初学者
- Watermelon book chapter 2 Notes - Performance Measurement
- 004: print characters
- 寒假学习总结
- 437. Path sum III
- 10th week ACM training report
- Unity 使用 bvh 驱动骨骼动作
猜你喜欢
随机推荐
力扣记录:动态规划2背包问题(2)完全背包——518 零钱兑换II,377 组合总和IV,70 爬楼梯,322零钱兑换,279 完全平方数,139 单词拆分
如何用数组模拟栈(超简易代码)
Sixth week ACM training report
CocosCreator 3.x 环境搭建
創意編程/小學組(4-6年級)-圖形化創意
表达式求值
9th week ACM training report
n factorial
组队
Cocos Creator 3.2 中实现2D地图3D人物45度角RPG游戏完整效果
Unity 使用 bvh 驱动骨骼动作
How to complete the design of RGB lamp Bluetooth mesh module from 0 to 1
299. 猜数字游戏
Atcoder beginer contest 218 problem solution
快速排序
Cocos Creator 3. X physics engine usage notes
Probability theory - maximum likelihood estimation
【oops-framework】配套热更新插件
Characteristics and differences between PCB and integrated circuit
543. Diameter of binary tree