【3D技术宅公社】XR数字艺术论坛  XR技术讨论 XR互动电影 定格动画

 找回密码
 立即注册

QQ登录

只需一步,快速开始

调查问卷
论坛即将给大家带来全新的技术服务,面向三围图形学、游戏、动画的全新服务论坛升级为UTF8版本后,中文用户名和用户密码中有中文的都无法登陆,请发邮件到324007255(at)QQ.com联系手动修改密码

3D技术论坛将以计算机图形学为核心,面向教育 推出国内的三维教育引擎该项目在持续研发当中,感谢大家的关注。

查看: 1488|回复: 0

4-6 Screen Fader 场景切换的渐变效果

[复制链接]
发表于 2013-9-30 22:28:28 | 显示全部楼层 |阅读模式
利用GUI纹理实现游戏场景切换时的渐隐效果

C#脚本
  1. using UnityEngine;
  2. using System.Collections;

  3. public class SceneFadeInOut : MonoBehaviour
  4. {
  5.     public float fadeSpeed = 1.5f;          // 屏幕黑色幕布渐隐的速度.      
  6.     private bool sceneStarting = true;      // 游戏场景是否开始运行.
  7.                
  8.     void Awake ()
  9.     {
  10.         // 设置纹理大小跟屏幕的大小一致
  11.         guiTexture.pixelInset = new Rect(0f, 0f, Screen.width, Screen.height);
  12.     }
  13.    
  14.    
  15.     void Update ()
  16.     {
  17.         // 如果场景开始了...
  18.         if(sceneStarting)
  19.             // ... 就运行StartScene().
  20.             StartScene();
  21.     }
  22.    
  23.    
  24.     void FadeToClear ()
  25.     {
  26.         // 这个函数主要是做:从纹理本身的颜色向透明做插值,即逐渐变淡.
  27.         guiTexture.color = Color.Lerp(guiTexture.color, Color.clear, fadeSpeed * Time.deltaTime);
  28.     }
  29.    
  30.    
  31.     void FadeToBlack ()
  32.     {
  33.         //这个函数主要是做:从纹理本身的颜色向黑色做插值,即逐渐变黑.
  34.         guiTexture.color = Color.Lerp(guiTexture.color, Color.black, fadeSpeed * Time.deltaTime);
  35.     }
  36.    
  37.    
  38.     void StartScene ()
  39.     {
  40.         // 执行渐隐函数.
  41.         FadeToClear();
  42.         
  43.         // 判断当前的纹理颜色是不是接近透明了...
  44.         if(guiTexture.color.a <= 0.05f)
  45.         {
  46.             // ...如果接近透明就直接将纹理从场景中设为不可用.
  47.             guiTexture.color = Color.clear;
  48.             guiTexture.enabled = false;
  49.             
  50.             // 设置场景开始标志位为false.
  51.             sceneStarting = false;
  52.         }
  53.     }
  54.    
  55.    
  56.     public void EndScene ()
  57.     {
  58.         // 设置纹理可用.
  59.         guiTexture.enabled = true;
  60.         
  61.         // 执行从正常逐渐变黑的函数.
  62.         FadeToBlack();
  63.         
  64.         // 如果场景已经足够的黑了...
  65.         if(guiTexture.color.a >= 0.95f)
  66.             //加载下一关的地图场景.
  67.             Application.LoadLevel(0);
  68.     }
  69. }
复制代码
JS脚本
  1. #pragma strict

  2. public var fadeSpeed : float = 1.5f;            // Speed that the screen fades to and from black.


  3. private var sceneStarting : boolean = true;     // Whether or not the scene is still fading in.


  4. function Awake ()
  5. {
  6.     // Set the texture so that it is the the size of the screen and covers it.
  7.     guiTexture.pixelInset = new Rect(0f, 0f, Screen.width, Screen.height);
  8. }


  9. function Update ()
  10. {
  11.     // If the scene is starting...
  12.     if(sceneStarting)
  13.         // ... call the StartScene function.
  14.         StartScene();
  15. }


  16. function FadeToClear ()
  17. {
  18.     // Lerp the colour of the texture between itself and transparent.
  19.     guiTexture.color = Color.Lerp(guiTexture.color, Color.clear, fadeSpeed * Time.deltaTime);
  20. }


  21. function FadeToBlack ()
  22. {
  23.     // Lerp the colour of the texture between itself and black.
  24.     guiTexture.color = Color.Lerp(guiTexture.color, Color.black, fadeSpeed * Time.deltaTime);
  25. }


  26. function StartScene ()
  27. {
  28.     // Fade the texture to clear.
  29.     FadeToClear();
  30.    
  31.     // If the texture is almost clear...
  32.     if(guiTexture.color.a <= 0.05f)
  33.     {
  34.         // ... set the colour to clear and disable the GUITexture.
  35.         guiTexture.color = Color.clear;
  36.         guiTexture.enabled = false;
  37.         
  38.         // The scene is no longer starting.
  39.         sceneStarting = false;
  40.     }
  41. }


  42. public function EndScene ()
  43. {
  44.     // Make sure the texture is enabled.
  45.     guiTexture.enabled = true;
  46.    
  47.     // Start fading towards black.
  48.     FadeToBlack();
  49.    
  50.     // If the screen is almost black...
  51.     if(guiTexture.color.a >= 0.95f)
  52.         // ... reload the level.
  53.         Application.LoadLevel(0);
  54. }
复制代码

手机版|小黑屋|3D数字艺术论坛 ( 沪ICP备14023054号 )

GMT+8, 2024-9-28 00:20

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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