多米诺 发表于 2012-6-14 23:48

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

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

多米诺 发表于 2012-6-14 23:50

var colorStart = Color.red;
var colorEnd = Color.green;
var duration = 1.0;
var minimum = 0.0;
var maximum = 200.0;
function Update () {
    var lerp = Mathf.PingPong (Time.time, duration) / duration;
    renderer.material.color = Color.Lerp (colorStart, colorEnd, lerp);
   transform.position.x = Mathf.Lerp(minimum, maximum, lerp);
}如果是改变摄像机背景,将 renderer.material.color = Color.Lerp (colorStart, colorEnd, lerp);
更改为:camera.backgroundColor = Color.Lerp (colorStart, colorEnd, lerp);
绑定到摄像机上即可实现渐变效果

多米诺 发表于 2012-6-15 13:16

版本3:
var showGUI:boolean = true;
var alpha:float;
var gui_:GUISkin;
function Update()
{
   if(showGUI)
   {
      FadeIn();
   }
   else {
      FadeOut();
   }
}
function FadeOut(){
// alpha = Mathf.Lerp(1,0,Time.time*0.2);//用这句话也行让alpha 在Time.time*0.5 秒内值从1到0改变
alpha += -1 * 0.3 * Time.deltaTime;
    alpha = Mathf.Clamp01(alpha);   
   
    GUI.color.a = alpha;
   
    GUI.depth = -1000;
}

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

function OnGUI(){
      GUI.skin=gui_;
    GUI.color.a = alpha;
      if(GUI.Button(Rect(0,0,Screen.width,Screen.height),"Made by domino."))
      {
      showGUI=false;
      }
}

多米诺 发表于 2012-6-15 13:17

版本4:定点渐变var showGUI:boolean = true;
var alpha:float;
var gui_:GUISkin;
var time_:float;

function Update()
{time_+=Time.deltaTime*2;
   if(showGUI)
   {
      FadeIn();
   }
   else {
      FadeOut();
   }
   print(time_.ToString());
}
function FadeOut(){
alpha += -1 * 0.5* Time.deltaTime;
    alpha = Mathf.Clamp01(alpha);   
}

function FadeIn(){
   alpha += 1 * 0.5 * Time.deltaTime;
    alpha = Mathf.Clamp01(alpha);   
}
function OnGUI(){
      GUI.skin=gui_;
    GUI.color.a = alpha;
      if(GUI.Button(Rect(0,0,Screen.width,Screen.height),"Made by domino."))
      {
      
      }
                if(time_>5&&time_<=6)
                {
                showGUI=false;
               
                }
                if(time_>10&&time_<=11)
                {
                        showGUI=true;
                }
}

多米诺 发表于 2012-6-15 13:18

版本5. DrawTexture渐入渐出using UnityEngine;
using System.Collections;

public class New: MonoBehaviour {

    private Texture2D txr;
    private Rect rct;
      void Start () {
      txr = new Texture2D(10,10);
      rct = new Rect(0, 0, Screen.width, Screen.height);
      Color[] clr = new Color;
      for (int i = 0; i < clr.Length; i++)
      {
            clr = new Color(0, 0, 0, 1);
      }
      txr.SetPixels(clr);
      txr.Apply();
      }

    // Update is called once per frame
    void Update()
    {
            
      }
    void OnGUI()
    {
      GUI.DrawTexture(rct, txr);
      if (GUI.Button(new Rect(10, 10, 100, 50), "show"))
      {
            StartCoroutine(showScene());
      }
      if (GUI.Button(new Rect(10, 70, 100, 50), "hide"))
      {
            StartCoroutine(hideScene());
      }
    }
    IEnumerator showScene()
    {
      Color[] clr = txr.GetPixels();
      while (txr.GetPixel(0, 0).a > 0)
      {
            for (int i = 0; i < clr.Length; i++)
            {
                clr.a -= Time.deltaTime;
            }
            txr.SetPixels(clr);
            txr.Apply();
            yield return new WaitForEndOfFrame();
      }
    }
    IEnumerator hideScene()
    {
      Color[] clr = txr.GetPixels();
      while (txr.GetPixel(0, 0).a < 1)
      {
            for (int i = 0; i < clr.Length; i++)
            {
                clr.a += Time.deltaTime;
            }
            txr.SetPixels(clr);
            txr.Apply();
            yield return new WaitForEndOfFrame();
      }
    }
}

aaabbbsss12345 发表于 2014-5-8 15:52

好 学习了 学习了

tracylong2008 发表于 2014-6-11 16:40


不错 不错 不错{:soso__3922851084632044791_6:}

客串大师 发表于 2015-9-13 17:08


感谢楼主的无私分享!{:soso__11402694654016840197_7:}

pkl8577192 发表于 2017-2-15 19:03

很不错

lovehou922 发表于 2017-2-15 18:59

好帖就是要顶
页: [1]
查看完整版本: Unity3d 相机指定 图片 渐入渐出