【Unity】透明度渐变
写给美术大佬的脚本,还要继续改,github地址:TransEffect【github】
效果图如下:
Ver.1源码,针对3d Object:
using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class TransEffect : MonoBehaviour
{ public List<GoInfo> GoList;
public float varifySpeed = 0.5f;
public float aTime = 5f;//每个物体保持出现的时间
public float dTime = 5f; private float minAlpha = 0.0f;
private float maxAlpha = .9f;
private float curAlpha = 1.0f;
private float nextAlpha = 0.0f;
private int i = ; public void OnEnable()
{
LoadGo();
} // Use this for initialization
void Start()
{
//初始化全List隐形
foreach (GoInfo go in GoList)
{
Color c = go.rend.material.color;
c.a = ;
go.rend.material.color = c; }
} // Update is called once per frame
public void Update()
{
Trans();
} void LoadGo()
{
GoList = new List<GoInfo>();
GoList.Add(new GoInfo("Cylinder", , transform.Find("Cylinder").GetComponent<GameObject>(), transform.Find("Cylinder").GetComponent<MeshRenderer>()));
GoList.Add(new GoInfo("Cube", , transform.Find("Cube").GetComponent<GameObject>(), transform.Find("Cube").GetComponent<MeshRenderer>()));
GoList.Add(new GoInfo("Sphere", , transform.Find("Sphere").GetComponent<GameObject>(), transform.Find("Sphere").GetComponent<MeshRenderer>()));
GoList.Add(new GoInfo("Capsule", , transform.Find("Capsule").GetComponent<GameObject>(), transform.Find("Capsule").GetComponent<MeshRenderer>()));
} private void Trans()
{
GoInfo go = GoList[i];
GoInfo nextgo;
Color c = go.rend.material.color;
Color nextc = go.rend.material.color; if (i <= GoList.Count)
{
if (i == GoList.Count - )
{
nextgo = GoList[];
}
else
{
nextgo = GoList[i + ];
} Debug.Log(nextAlpha);
Debug.Log(curAlpha); if (Time.time < aTime)//当前物体保持显形
{
c.a = ;
go.rend.material.color = c;
}
else if (Time.time >= aTime)
{
curAlpha += Time.deltaTime * varifySpeed * (-);//当前物体逐渐消失
nextAlpha += Time.deltaTime * varifySpeed;//下一个物体逐渐现形 if (curAlpha <= minAlpha)//当前物体渐变到不透明时
{
c.a = ;//设置当前obj保持透明
go.rend.material.color = c;
i++;
//设置数据为下一物体做准备
curAlpha = ;
nextAlpha = ;
} else//当前物体逐渐透明,下一物体逐渐现形
{
curAlpha = Mathf.Clamp(curAlpha, minAlpha, maxAlpha);
nextAlpha = Mathf.Clamp(nextAlpha, minAlpha, maxAlpha);
c.a = curAlpha;
nextc.a = nextAlpha;
go.rend.material.color = c;
nextgo.rend.material.color = nextc; } if (curAlpha >= maxAlpha)//下一物体完全显形
{
Debug.Log(nextAlpha);
Debug.Log(curAlpha);
aTime = Time.time + dTime; //设置新一轮时间限制
Debug.Log(aTime); }
}
}
else
{
i = ;
}
} } [System.Serializable]
public class GoInfo
{
public string ID;
public int index;
public MeshRenderer rend;
public GameObject[] obj;
public GameObject curObj;
private Color co; public GoInfo(string id0, int index0, GameObject obj0, MeshRenderer rend0)
{
ID = id0;
index = index0;
curObj = obj0;
rend = rend0; } }
创建物体:
写完才发现是要用在UI Image上的...不过其实差别也不大,还略简单点。
Ver.2源码,针对UI Image:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; public class TransEffect : MonoBehaviour
{
public Transform lib;
public List<GoInfo> GoList;
public float varifySpeed = 0.5f;
public float aTime = 5f;//每个物体保持出现的时间
public float dTime = 5f;//第一张图片第一轮循环时出现时间 private float minAlpha = 0.0f;
private float maxAlpha = .9f;
private float curAlpha = 1.0f;
private float nextAlpha = 0.0f;
private int i = ; public void OnEnable()
{
LoadGo();
} // Use this for initialization
void Start()
{
//初始化全List隐形
foreach (GoInfo go in GoList)
{
Color c = go.curImg.color;
c.a = ;
go.curImg.color = c; }
} // Update is called once per frame
public void Update()
{
Trans();
} void LoadGo()
{
//添加图片列表
GoList = new List<GoInfo>();
for (int i = ; i < lib.childCount; i++) {
GoList.Add(new GoInfo(lib.GetChild(i).name.ToString(),lib.transform.GetChild(i).GetComponent<Image>()));
}
Debug.Log(GoList.Count);
} private void Trans()
{ Debug.Log(i);
GoInfo go;
GoInfo nextgo; if (i >= GoList.Count - )
{
go = GoList[i];
nextgo = GoList[];
}
else
{
go = GoList[i];
nextgo = GoList[i + ];
} Color c = go.curImg.color;
Color nextc = go.curImg.color; if (Time.time < aTime)//当前物体保持显形
{
c.a = ;
go.curImg.color = c;
}
else if (Time.time >= aTime)
{
curAlpha += Time.deltaTime * varifySpeed * (-);//当前物体逐渐消失
nextAlpha += Time.deltaTime * varifySpeed;//下一个物体逐渐现形 if (curAlpha <= minAlpha)//当前物体渐变到不透明时
{
c.a = ;//设置当前obj保持透明
go.curImg.color = c; if (i == GoList.Count - )
i = -;
i++; //设置数据为下一物体做准备
curAlpha = ;
nextAlpha = ;
} else//当前物体逐渐透明,下一物体逐渐现形
{
curAlpha = Mathf.Clamp(curAlpha, minAlpha, maxAlpha);
nextAlpha = Mathf.Clamp(nextAlpha, minAlpha, maxAlpha);
c.a = curAlpha;
nextc.a = nextAlpha;
go.curImg.color = c;
nextgo.curImg.color = nextc; } if (curAlpha >= maxAlpha)//下一物体完全显形
{
aTime = Time.time + dTime; //设置新一轮时间限制
}
} } } [System.Serializable]
public class GoInfo
{
public string ID;
public Image[] imgList;
public Image curImg; private Color co; public GoInfo(string id0,Image img)
{
ID = id0;
curImg = img;
} }
直接把存放图片子物体的父物体拖到Lib变量中,再调整所需渐变速度和显示时间即可。
设置如下:
【over】
【Unity】透明度渐变的更多相关文章
- android 背景透明度渐变动画
button.setVisibility(View.VISIBLE); // 背景透明度渐变动画 ObjectAnimator alpha = ObjectAnimator.ofFloat(butto ...
- Android 旋转、平移、缩放和透明度渐变的补间动画
补间动画就是通过对场景里的对象不断进行图像变化来产生动画效果.在实现补间动画时,只需要定义开始和结束的“关键帧”,其他过渡帧由系统自动计算并补齐.在Android中,提供了以下4种补间动画. **1. ...
- view渐变色,透明度渐变
1 功能描述 开发中经常遇到这样的需求:view2显示在view1上面,透过view2可以渐渐的看到view1.效果如图1所示:view1是一个imageView,view2是一个普通view.vie ...
- Duilib 实现右下角弹出像QQ新闻窗口,3秒后窗口透明度渐变最后关闭,若在渐变过程中鼠标放到窗口上,窗口恢复最初状态(二)
效果: 1.定义两个个定时器ID #define ID_TIMER_DISPLAY_DELAY 30 #define ID_TIMER_DISPLAY_CLOSE 40 2.添加一个成员函数和成员变量 ...
- iOS中为控件设置颜色渐变和透明度渐变
项目中用到地图设置渐变色,查找资料找到两种方法:一种设置颜色,一种设置透明度: //为颜色设置渐变效果: UIView *view = [[UIView alloc] initWithFrame:CG ...
- css3颜色+透明度渐变
.linear { width: 630px; height: 120px; line-height: 150px; text-align: center; font-size: 26px; posi ...
- Android之绚丽的图片游览效果--有点像W7效果,透明的倒影,层叠的图片,渐变的颜色透明度
这里转载一个牛人的博客:http://www.cnblogs.com/tankaixiong/archive/2011/02/24/1964340.html 下面,是我参照他的博客实现的一个效果图.这 ...
- JS实现简单的图片透明度循环变化(渐变)
找了好多,都是由100到0就结束了,到头来自己魔改,以下就是源码. div中加入img,js添加函数,完事(调用),取名后面加个1是为了避免冲突 <!DOCTYPE HTML> <h ...
- Unity Shader 知识点总结(二)
紧接着上一篇文章的shader入门知识的总结,本文主要总结shader中的纹理贴图.透明度混合.顶点动画.后期特效处理等操作.如果有什么地方有错,请指出更正,谢谢.本文的代码主要来自开源书:unity ...
随机推荐
- leetcode题解-122买卖股票的最佳时期
题目 leetcode题解-122.买卖股票的最佳时机:https://www.yanbinghu.com/2019/03/14/30893.html 题目详情 给定一个数组,它的第 i 个元素是一支 ...
- bilibili弹幕爬取
随便进入一个视频页面,打开开发者工具,清空network空间,进入XHR,刷新抓包. 双击查看弹幕
- weblogic补丁下载与安装补丁的方法
文章目录1.根据漏洞报告下载补丁2.补丁包上传解压到Linux3.关于OPatch4.安装补丁4.1单个补丁安装4.2查看已安装的补丁4.3多个补丁安装4.4单个补丁回滚4.5多个补丁回滚4.6验证补 ...
- Riemann流形上的梯度,散度与Laplace算子
今天(准确地说是昨天)被学物理的同学问到Stokes定理,想起来我还有一个知道但没有细看的东西,下面整理成提示完整的习题记录一下. 这部分内容将会加进几何学观止,敬请期待.目前正在纂写代数几何簇的部分 ...
- 菜鸟学IT之简易四则运算程序开发
作业要求来源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2166 作业要求: 任何编程语言都可以,命令行程序接受一个数字输入,然后 ...
- NW.js使用及打包
简介 NW.js (原名 node-webkit)是一个结合了 Chromium 和 node.js 的应用运行时,通过它可以用 HTML 和 JavaScript 编写原生应用程序.它还允许开发者从 ...
- Go 目录
Go语言 go语言初识 基本数据类型和操作符 字符串,时间,流程控制,函数 GOROOT,GOPATH,GOBIN,project目录 数组和切片 指针和内置函数 排序和查找 map
- Java的selenium代码随笔(8)
Selenium截图方法一: Selenium中截图类TakeScreenshout,这个类主要是获取浏览器窗体内的内容,不包括浏览器的菜单和桌面的任务栏区域,我们用百度首页来截图,看看截图效果. F ...
- Django-6 Django ORM层
ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人员的 ...
- vue 倒计时组件
<template> <span> <i v-text="msg"></i> </span></template& ...