县区网站建设运行汇报,wordpress 顶部图片,建站资源,有什么网站可以做问卷调查1.需求 给物体绑定一个脚本#xff0c;这个脚本实现物体的透明度渐变变化#xff0c;并且可以重置回原来的颜色。物体为Unity自带的材质Shader为Standard。
2.代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Transpare…1.需求 给物体绑定一个脚本这个脚本实现物体的透明度渐变变化并且可以重置回原来的颜色。物体为Unity自带的材质Shader为Standard。
2.代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class TransparentChanger : MonoBehaviour
{/// summary/// 保存原颜色/// /summaryprivate Color[] oldColors;/// summary/// 控制透明度变化/// /summary[Range(0, 1)]public float nalpha;private void Start(){Init();}private void Update(){SetOpacity(nalpha);}public void Init(){int num 0;foreach (Renderer r in GetComponentsInChildrenRenderer(true)){num r.materials.Length;}oldColors new Color[num];num 0;foreach (Renderer r in GetComponentsInChildrenRenderer(true)){foreach (Material m in r.materials){try{oldColors[num] m.color;}catch (System.Exception e){Debug.Log(e.Message);}}}}/// summary/// 重置回原来的颜色/// /summarypublic void Reset(){int num 0;foreach (Renderer r in GetComponentsInChildrenRenderer(true)){foreach (Material m in r.materials){try{m.color oldColors[num];if (m.color.a 1){ChangeMaterialType(0, m);m.renderQueue -1;}}catch (System.Exception e){Debug.Log(e.Message);}}}}public void SetOpacity(float alpha){int num 0;foreach (Renderer r in GetComponentsInChildrenRenderer(true)){foreach (Material m in r.materials){try{float mA Mathf.Min(alpha, oldColors[num].a);m.color new Color(m.color.r, m.color.g, m.color.b, mA);if (mA 0.98f){ChangeMaterialType(0, m);}else{ChangeMaterialType(1, m);}}catch (System.Exception e){Debug.Log(e.Message);}}}}/// summary/// 切换材质的Standard Shader 渲染类型/// /summary/// param namemType0-Opaque, 1-Transparent/param/// param namem材质/paramvoid ChangeMaterialType(int mType, Material m){if(mType 0){m.SetInt(_SrcBlend, (int)UnityEngine.Rendering.BlendMode.One);m.SetInt(_DstBlend, (int)UnityEngine.Rendering.BlendMode.Zero);m.SetInt(_ZWrite, 1);m.DisableKeyword(_ALPHATEST_ON);m.DisableKeyword(_ALPHABLEND_ON);m.DisableKeyword(_ALPHAPREMULTIPLY_ON);m.renderQueue -1;}else if(mType 1){m.SetInt(_SrcBlend, (int)UnityEngine.Rendering.BlendMode.One);m.SetInt(_DstBlend, (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);m.SetInt(_ZWrite, 0);m.DisableKeyword(_ALPHATEST_ON);m.DisableKeyword(_ALPHABLEND_ON);m.EnableKeyword(_ALPHAPREMULTIPLY_ON);m.renderQueue 3000;}}
}