辣条 发表于 2012-8-12 22:47

Unity3D 相机淡入淡出特效


一、新建一个js脚本命名为FadeInOut.js
加入如下代码:

---------------------------------------------------------------------------------

// FadeInOut
//
//--------------------------------------------------------------------
//                        Public parameters
//--------------------------------------------------------------------

public var fadeOutTexture : Texture2D;
public var fadeSpeed = 0.3;

var drawDepth = -1000;

//--------------------------------------------------------------------
//                     Private variables
//--------------------------------------------------------------------

private var alpha = 1.0;

private var fadeDir = -1;

//--------------------------------------------------------------------
//                     Runtime functions
//--------------------------------------------------------------------

//--------------------------------------------------------------------

function OnGUI(){
    alpha += fadeDir * fadeSpeed * Time.deltaTime;
    alpha = Mathf.Clamp01(alpha);   
   
    GUI.color.a = alpha;
   
    GUI.depth = drawDepth;
   
    GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeOutTexture);
}

//--------------------------------------------------------------------

function fadeIn(){
    fadeDir = -1;   
}

//--------------------------------------------------------------------

function fadeOut(){
    fadeDir = 1;   
}

function Start(){
    alpha=1;
    fadeIn();
}


-------------------------------------------------------------------------------------------


把FadeInOut.js添加到你的摄像机下

二,然后自己做一个1x1像素,黑色背景的图像文件(例如PNG)
把该图像文件添加到FadeInOut中

三,想执行淡入淡出的时候只要执行
Camera.main.SendMessage("fadeOut");

Camera.main.SendMessage("fadeIn");
就可以了。
官方wiki(还包含C#版本):
http://www.unifycommunity.com/wiki/index.php?title=FadeInOut


陈亮 发表于 2012-11-12 22:17

这个效果真不错

jstones 发表于 2012-12-14 18:08

坎坷坷看看坎坷坷看看

kj5555 发表于 2012-12-14 18:17

赞了赞了~~~~

隐忍的微笑 发表于 2012-12-15 18:51

看看吧加加

WZ╄→其实属于 发表于 2013-1-21 15:07

试啦下,效果不错,多谢楼主

Asuka_ 发表于 2013-1-31 13:08

这效果不错,支持一下

lovesky4 发表于 2013-4-6 22:23

{:5_424:}{:5_424:}{:5_424:}{:5_424:}

asdf 发表于 2013-4-20 09:24

GUI.color.a = alpha;
Assets/text/FadeInOut.cs(17,21): error CS1612: Cannot modify a value type return value of `UnityEngine.GUI.color'. Consider storing the value in a temporary variable
这是为什么啊?

飞鸽 发表于 2017-2-26 17:53

顶顶多好
页: [1]
查看完整版本: Unity3D 相机淡入淡出特效