找回密码
 立即注册
楼主: 多米诺

[特效] Unity3d 相机指定 图片 渐入渐出

  [复制链接]
发表于 2012-6-14 23:48 | 显示全部楼层 |阅读模式

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

  1. // FadeInOut
  2. //
  3. //--------------------------------------------------------------------
  4. //                        Public parameters
  5. //--------------------------------------------------------------------

  6. public var fadeOutTexture : Texture2D;
  7. public var fadeSpeed = 0.3;

  8. var drawDepth = -1000;

  9. //--------------------------------------------------------------------
  10. //                       Private variables
  11. //--------------------------------------------------------------------

  12. private var alpha = 1.0;

  13. private var fadeDir = -1;

  14. //--------------------------------------------------------------------
  15. //                       Runtime functions
  16. //--------------------------------------------------------------------

  17. //--------------------------------------------------------------------

  18. function OnGUI(){
  19.     alpha += fadeDir * fadeSpeed * Time.deltaTime;  
  20.     alpha = Mathf.Clamp01(alpha);   
  21.    
  22.     GUI.color.a = alpha;
  23.    
  24.     GUI.depth = drawDepth;
  25.    
  26.     GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeOutTexture);
  27. }

  28. //--------------------------------------------------------------------

  29. function fadeIn(){
  30.     fadeDir = -1;   
  31. }

  32. //--------------------------------------------------------------------

  33. function fadeOut(){
  34.     fadeDir = 1;   
  35. }

  36. function Start(){
  37.     alpha=1;
  38.     fadeIn();
  39. }
复制代码
把FadeInOut.js添加到你的摄像机下

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

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

Camera.main.SendMessage("fadeIn");
就可以了。

官方wiki(还包含C#版本):
http://www.unifycommunity.com/wiki/index.php?title=FadeInOut

附件中的CameraTestScript.js是FadeInOut功能的执行程序,将它添加到一个自定义的GameObject中并按下左Ctrl键,看看效果吧^_^

评分

参与人数 1鲜花 +1 收起 理由
qwer4650987 + 1

查看全部评分

 楼主| 发表于 2012-6-14 23:50 | 显示全部楼层
  1. var colorStart = Color.red;  
  2. var colorEnd = Color.green;  
  3. var duration = 1.0;  
  4. var minimum = 0.0;  
  5. var maximum = 200.0;  
  6. function Update () {  
  7.     var lerp = Mathf.PingPong (Time.time, duration) / duration;  
  8.     renderer.material.color = Color.Lerp (colorStart, colorEnd, lerp);  
  9.    transform.position.x = Mathf.Lerp(minimum, maximum, lerp);  
  10. }  
复制代码
如果是改变摄像机背景,将 renderer.material.color = Color.Lerp (colorStart, colorEnd, lerp);  
更改为:camera.backgroundColor = Color.Lerp (colorStart, colorEnd, lerp);  
绑定到摄像机上即可实现渐变效果
 楼主| 发表于 2012-6-15 13:16 | 显示全部楼层
版本3:

  1. var showGUI:boolean = true;
  2. var alpha:float;
  3. var gui_:GUISkin;
  4. function Update()
  5. {
  6.    if(showGUI)
  7.    {
  8.       FadeIn();
  9.    }
  10.    else {
  11.       FadeOut();
  12.    }
  13. }
  14. function FadeOut(){
  15.   // alpha = Mathf.Lerp(1,0,Time.time*0.2);//  用这句话也行  让alpha 在Time.time*0.5 秒内值从1到0改变
  16.   alpha += -1 * 0.3 * Time.deltaTime;  
  17.     alpha = Mathf.Clamp01(alpha);   
  18.    
  19.     GUI.color.a = alpha;
  20.    
  21.     GUI.depth = -1000;
  22. }

  23. function FadeIn(){
  24.    alpha = Mathf.Lerp(0,1,Time.time*0.5);
  25. }

  26. function OnGUI(){
  27.         GUI.skin=gui_;
  28.     GUI.color.a = alpha;
  29.         if(GUI.Button(Rect(0,0,Screen.width,Screen.height),"Made by domino."))
  30.         {
  31.         showGUI=false;
  32.         }
  33. }
复制代码
 楼主| 发表于 2012-6-15 13:17 | 显示全部楼层
版本4:定点渐变
  1. var showGUI:boolean = true;
  2. var alpha:float;
  3. var gui_:GUISkin;
  4. var time_:float;

  5. function Update()
  6. {time_+=Time.deltaTime*2;
  7.    if(showGUI)
  8.    {
  9.       FadeIn();
  10.    }
  11.    else {
  12.       FadeOut();
  13.    }
  14.    print(time_.ToString());
  15. }
  16. function FadeOut(){
  17.   alpha += -1 * 0.5* Time.deltaTime;  
  18.     alpha = Mathf.Clamp01(alpha);   
  19. }

  20. function FadeIn(){
  21.    alpha += 1 * 0.5 * Time.deltaTime;  
  22.     alpha = Mathf.Clamp01(alpha);   
  23. }
  24. function OnGUI(){
  25.         GUI.skin=gui_;
  26.     GUI.color.a = alpha;
  27.         if(GUI.Button(Rect(0,0,Screen.width,Screen.height),"Made by domino."))
  28.         {
  29.       
  30.         }
  31.                 if(time_>5&&time_<=6)
  32.                 {
  33.                 showGUI=false;
  34.                
  35.                 }
  36.                 if(time_>10&&time_<=11)
  37.                 {
  38.                         showGUI=true;
  39.                 }
  40. }
复制代码
 楼主| 发表于 2012-6-15 13:18 | 显示全部楼层
版本5. DrawTexture渐入渐出
  1. using UnityEngine;
  2. using System.Collections;

  3. public class New: MonoBehaviour {

  4.     private Texture2D txr;
  5.     private Rect rct;
  6.         void Start () {
  7.         txr = new Texture2D(10,10);
  8.         rct = new Rect(0, 0, Screen.width, Screen.height);
  9.         Color[] clr = new Color[100];
  10.         for (int i = 0; i < clr.Length; i++)
  11.         {
  12.             clr[i] = new Color(0, 0, 0, 1);
  13.         }
  14.         txr.SetPixels(clr);
  15.         txr.Apply();
  16.         }

  17.     // Update is called once per frame
  18.     void Update()
  19.     {
  20.             
  21.         }
  22.     void OnGUI()
  23.     {
  24.         GUI.DrawTexture(rct, txr);
  25.         if (GUI.Button(new Rect(10, 10, 100, 50), "show"))
  26.         {
  27.             StartCoroutine(showScene());
  28.         }
  29.         if (GUI.Button(new Rect(10, 70, 100, 50), "hide"))
  30.         {
  31.             StartCoroutine(hideScene());
  32.         }
  33.     }
  34.     IEnumerator showScene()
  35.     {
  36.         Color[] clr = txr.GetPixels();
  37.         while (txr.GetPixel(0, 0).a > 0)
  38.         {
  39.             for (int i = 0; i < clr.Length; i++)
  40.             {
  41.                 clr[i].a -= Time.deltaTime;
  42.             }
  43.             txr.SetPixels(clr);
  44.             txr.Apply();
  45.             yield return new WaitForEndOfFrame();
  46.         }
  47.     }
  48.     IEnumerator hideScene()
  49.     {
  50.         Color[] clr = txr.GetPixels();
  51.         while (txr.GetPixel(0, 0).a < 1)
  52.         {
  53.             for (int i = 0; i < clr.Length; i++)
  54.             {
  55.                 clr[i].a += Time.deltaTime;
  56.             }
  57.             txr.SetPixels(clr);
  58.             txr.Apply();
  59.             yield return new WaitForEndOfFrame();
  60.         }
  61.     }
  62. }
复制代码
发表于 2014-5-8 15:52 | 显示全部楼层
好 学习了 学习了
发表于 2014-6-11 16:40 | 显示全部楼层

不错 不错 不错{:soso__3922851084632044791_6:}
发表于 2015-9-13 17:08 | 显示全部楼层

感谢楼主的无私分享!{:soso__11402694654016840197_7:}
发表于 2017-2-15 19:03 | 显示全部楼层
很不错
发表于 2017-2-15 18:59 | 显示全部楼层
好帖就是要顶
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-4-28 01:55 , Processed in 0.132220 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表