博客
关于我
UGUIImage透明渐变
阅读量:95 次
发布时间:2019-02-26

本文共 2863 字,大约阅读时间需要 9 分钟。

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'//参考https://blog.csdn.net/qq_34537249/article/details/78264955Shader "Unlit/ImageAlpha"{	Properties	{		[PerRendererData]_MainTex ("Texture", 2D) = "white" {}		_AlphaLX("RangeAlphaLX",Float) = 0		_AlphaRX("RangeAlphaRX",Float) = 1		_AlphaTY("RangeAlphaTY",Float) = 1		_AlphaBY("RangeAlphaBY",Float) = 0		_AlphaPower("Power",Float) = 0 //透明度变化范围	}	SubShader	{		Tags { "RenderType"="Transparent" }		Blend SrcAlpha OneMinusSrcAlpha		Cull Back		Pass		{			CGPROGRAM			#pragma vertex vert			#pragma fragment frag						#include "UnityCG.cginc"			struct appdata			{				float4 vertex : POSITION;				float2 uv : TEXCOORD0;			};			struct v2f			{				float2 uv : TEXCOORD0;				float4 vertex : SV_POSITION;			};			sampler2D _MainTex;			float4 _MainTex_ST;			float _AlphaPower;			sampler2D _AlphaTex;			float _AlphaLX;			float _AlphaRX;			float _AlphaTY;			float _AlphaBY;			v2f vert (appdata v)			{				v2f o;				o.vertex = UnityObjectToClipPos(v.vertex);				o.uv = TRANSFORM_TEX(v.uv, _MainTex);				return o;			}			//此方法取自Unity默认Sprite的Shader			fixed4 SampleSpriteTexture (float2 uv)			{				fixed4 color = tex2D (_MainTex, uv);#if ETC1_EXTERNAL_ALPHA				// get the color from an external texture (usecase: Alpha support for ETC1 on android)				color.a = tex2D (_AlphaTex, uv).r;#endif //ETC1_EXTERNAL_ALPHA				return color;			}			fixed4 frag (v2f i) : SV_Target			{				// sample the texture				fixed4 col = SampleSpriteTexture(i.uv);				//利用透明度阈值和uv坐标的差来计算透明的程度和是否控制其半透				//四个方向只是对坐标的取值和正反方向不同,原理一致				fixed alphalx = col.a * lerp(1,_AlphaPower,(_AlphaLX-i.uv.x));				col.a = saturate(lerp(alphalx,col.a,step(_AlphaLX,i.uv.x)));				fixed alpharx = col.a * lerp(1,_AlphaPower,(i.uv.x-_AlphaRX));				col.a = saturate(lerp(col.a,alpharx,step(_AlphaRX,i.uv.x)));				fixed alphaby = col.a * lerp(1,_AlphaPower,(_AlphaBY-i.uv.y));				col.a = saturate(lerp(alphaby,col.a,step(_AlphaBY,i.uv.y)));				fixed alphaty = col.a * lerp(1,_AlphaPower,(i.uv.y-_AlphaTY));				col.a = saturate(lerp(col.a,alphaty,step(_AlphaTY,i.uv.y)));				return col;			}			ENDCG		}	}}

初始_AlphaLX("RangeAlphaLX",Float) = 0

        _AlphaRX("RangeAlphaRX",Float) = 1
        _AlphaTY("RangeAlphaTY",Float) = 1
        _AlphaBY("RangeAlphaBY",Float) = 0

修改_AlphaTY可以达到从上到下逐渐渐变 其他也是如此需要

接着跟代码  位0置挂上钩

using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;public class NewBehaviourScript : MonoBehaviour{    public float A = 0;    public Material _Material;    // Start is called before the first frame update    void Start()    {        _Material = GetComponent().material;    }    // Update is called once per frame    void Update()    {        //关联坐标  进行透明渐变        if (transform.localPosition.y > 0)        {            _Material.SetFloat("_AlphaRX", (50 - transform.localPosition.y)/50);        }            }}

 

 

你可能感兴趣的文章
MySQL
查看>>
MySQL
查看>>
mysql
查看>>
MTK Android 如何获取系统权限
查看>>
MySQL - 4种基本索引、聚簇索引和非聚索引、索引失效情况、SQL 优化
查看>>
MySQL - ERROR 1406
查看>>
mysql - 视图
查看>>
MySQL - 解读MySQL事务与锁机制
查看>>
MTTR、MTBF、MTTF的大白话理解
查看>>
mt_rand
查看>>
mysql /*! 50100 ... */ 条件编译
查看>>
mudbox卸载/完美解决安装失败/如何彻底卸载清除干净mudbox各种残留注册表和文件的方法...
查看>>
mysql 1264_关于mysql 出现 1264 Out of range value for column 错误的解决办法
查看>>
mysql 1593_Linux高可用(HA)之MySQL主从复制中出现1593错误码的低级错误
查看>>
mysql 5.6 修改端口_mysql5.6.24怎么修改端口号
查看>>
MySQL 8.0 恢复孤立文件每表ibd文件
查看>>
MySQL 8.0开始Group by不再排序
查看>>
mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
查看>>
multi swiper bug solution
查看>>
MySQL Binlog 日志监听与 Spring 集成实战
查看>>