我正在开发Unity3D的一个小项目。在项目中有一些键。当我点击一个键时,键的透明度将平滑地变为50%到100%,此更改将花费0.5秒。所以我需要选项透明度的动画。在Unity3D中是否可以平滑地动画对象的透明度?
答案 0 :(得分:2)
您应该在Update
循环内Lerp更改颜色。使用Time
类来衡量时间。查看Lerp
文档以获取示例。
我也找到了这段代码,它使用Lerp
改变了透明度,但不完全按照你想要的方式改变了透明度,不幸的是, unityscript :
#pragma strict
var duration : float = 1.0;
var alpha : float = 0;
function Update(){
lerpAlpha();
}
function lerpAlpha () {
var lerp : float = Mathf.PingPong (Time.time, duration) / duration;
alpha = Mathf.Lerp(0.0, 1.0, lerp) ;
renderer.material.color.a = alpha;
}
答案 1 :(得分:1)
如果您的脚本是c#脚本:
using UnityEngine;
using System.Collections;
public class WebPlayerController : MonoBehaviour {
public bool selected = false;
//Setting the colors like this, you can change them via inspector
public Color enabledColor = new Color(1,1,1,1);
public Color disabledColor = new Color(1,1,1,0.5f);
public float transitionTime = 0.5f;
private float lerp = 0;
void Start(){
lerp = selected ? 1 : 0;
}
//You can set by this method the button state! :)
public void SetSelected(bool isSelected){
selected = isSelected;
}
void Update(){
lerp += (isSelected ? 1 : -1) * Time.deltaTime/transitionTime;
lerp = Mathf.Clamp01 (lerp);
renderer.material.color = Color.Lerp(disabledColor,enabledColor,lerp);
}
}