茫茫網海中的冷日 - 對這文章發表回應
茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1675162 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
F09_033.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

對這文章發表回應

發表限制: 非會員 可以發表

發表者: 冷日 發表時間: 2015/4/22 8:35:49
Unity Fade in Unity 6.4 UI, animations...etc

I would like to make a unity fade in , so that when my game starts the game fades in, and i can have this maybe as a scene?, so i can have it before each of my scenes. Would i need to use the animation feature to change the alpha of a black panel. I dont know but if anyone has any ideas, then please share :D, Thanks
c#animationfadefadeinfade out

Answer:
Sure, If a static panel is OK you can put a canvas add a panel and put a CavasGroup on the base canvas.
You can fade from 0 Alpha to 1 Alpha by changing the CanvasGroup value.
     using UnityEngine.UI;  // add this at the top

     public CanvasGroup myCanvasGroup;
     private bool fadeIn;

     void Start()
     {
         fadeIn =true;
         myCanvasGroup.alpha = 0f;
     }

     void Update()
     {
         if(fadeIn)
         {
             myCanvasGroup.alpha = myCanvasGroup.alpha + Time.deltaTime;
             if(myCanvasGroup.alpha >= 1)
             {
                  myCanvasGroup.alpha = 1;
                  fadeIn = false;
             }
         }
     }


Thank you, the code works but it fades out, which i wanted aswell, but what values would i change to make in fade in? Thanks

No problem, Start at 1 and do
     myCanvasGroup.alpha = myCanvasGroup.alpha - Time.deltaTime;
     if(myCanvasGroup.alpha <= 0)
     {
         myCanvasGroup.alpha = 0;
         fadeIn = false;
     }

Also if you wanted it to take 2 seconds the divide Time.deltaTime by how many seconds you want so
 myCanvasGroup.alpha = myCanvasGroup.alpha - (Time.deltaTime / 2.4f);

that example is 2.4 second.

You should also untick the interactable and blocksraycast if only using for image effects.
and to do at any time have both a fadeIn bool and a fadeOut bool, make them public and just set them when you want a menu to either fade in or out.
You'll need both the fadeIn and fadeOut sections in Update for that to work but you should have enough code to get it working.

I have this code for the fade out:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class PanelFadeOut : MonoBehaviour {

 public CanvasGroup myCanvasGroup;
 private bool fadeOut;
 public bool globalFadeOut;

 void Start()
 {
     fadeOut = true && globalFadeOut;
     myCanvasGroup.alpha = 0f;
 }

 void Update()
 {
     if(fadeOut)
     {
         myCanvasGroup.alpha = myCanvasGroup.alpha + Time.deltaTime;
         if(myCanvasGroup.alpha >= 1)

         {
             myCanvasGroup.alpha = 1;
             fadeOut = false;
         }
     }
 }

}

And this for the fade in:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class PanelFadeIn : MonoBehaviour {

 public CanvasGroup myCanvasGroup;
 private bool fadeIn;
 public bool globalFadeIn;

 void Start()
 {
     fadeIn = true && globalFadeIn;
     myCanvasGroup.alpha = 1f;
 }

 void Update()
 {
     if(fadeIn)
     {
         myCanvasGroup.alpha = myCanvasGroup.alpha - Time.deltaTime;
         if(myCanvasGroup.alpha <= 0)

         {
             myCanvasGroup.alpha = 0;
             fadeIn = false;
         }
     }
 }

}

I put the globalFadeIn and globalFadeOut so that if they are checked then the fade in or out plays? It dose not work but is it the right way to go about it?

Not really. In the other script you need to reference your fade script so lets call the script Fade. Put the script on an empty gameObject and rename that MenuObject.

In any script that you want to set it do this:
 private Fade myFadeScript;

 void Start(){
     myFadeScript = GameObject.Find("MenuObject").GetComponent<Fade>();
 }

 // when you want to access the Fade bools
 myFadeScript.fadeIn = true;
 //
 myFadeScript.fadeOut = true;

Then set your fade script like this, hastily copied and pasted from your two scripts, so sorry if errors occur!
 public CanvasGroup myCanvasGroup;
 public bool fadeIn;
 public bool fadeOut;

  void Start()
  {
      fadeIn = true;
      fadeOut = false;
      myCanvasGroup.alpha = 1f;
  }

  void Update()
  {
      if(fadeIn)
      {
          myCanvasGroup.alpha = myCanvasGroup.alpha - Time.deltaTime;
          if(myCanvasGroup.alpha <= 0)

          {
              myCanvasGroup.alpha = 0;
              fadeIn = false;
          }
      }

      if(fadeOut)
      {
          myCanvasGroup.alpha = myCanvasGroup.alpha + Time.deltaTime;
          if(myCanvasGroup.alpha >= 1)

          {
              myCanvasGroup.alpha = 1;
              fadeOut = false;
          }
      }
  }
 }



原文出處:Unity Fade in Unity 6.4 UI, animations...etc - Unity Answers
內容圖示
url email imgsrc image code quote
樣本
bold italic underline linethrough   












 [詳情...]
validation picture

注意事項:
預覽不需輸入認證碼,僅真正發送文章時才會檢查驗證碼。
認證碼有效期10分鐘,若輸入資料超過10分鐘,請您備份內容後,重新整理本頁並貼回您的內容,再輸入驗證碼送出。

選項

Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|