关于Unity 开发Pico 的淡入淡出功能 (适用于和我一样刚入门的新手)
废话少说 直接上流程
在Pvr_UnitySDK--Head--下面创建个空物体FadeInOutControl(名字可以随意取)
在FadeInOutControl下创建两个子物体 一个叫FadeIn 另一个叫FadeOut
将两个子物体上添加Sprite Renderer 并附一张图片
FadeIn 的Color 设置为(0,0,0,255) FadeOut的Color 设置为(0,0,0,0)
在FadeIn物体添加“FadeIn”脚本 脚本如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FadeIn : MonoBehaviour {
private void OnEnable()
{
this.GetComponent<SpriteRenderer>().color = new Color
(this.GetComponent<SpriteRenderer>().color.r,
this.GetComponent<SpriteRenderer>().color.g,
this.GetComponent<SpriteRenderer>().color.b,
1
);
}
void Update()
{
//如果透明度大于零,减少,在此前提下,最后一次执行如果小于零,等于零
if (this.GetComponent<SpriteRenderer>().color.a > 0)
{
this.GetComponent<SpriteRenderer>().color = new Color
(this.GetComponent<SpriteRenderer>().color.r,
this.GetComponent<SpriteRenderer>().color.g,
this.GetComponent<SpriteRenderer>().color.b,
this.GetComponent<SpriteRenderer>().color.a - 0.02f
);
if (this.GetComponent<SpriteRenderer>().color.a < 0)
{
this.GetComponent<SpriteRenderer>().color = new Color
(this.GetComponent<SpriteRenderer>().color.r,
this.GetComponent<SpriteRenderer>().color.g,
this.GetComponent<SpriteRenderer>().color.b,
0
);
//在最后一次执行后,关闭物体
this.gameObject.SetActive(false);
}
}
}
}
在FadeOut物体添加“FadeOut”脚本 脚本如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FadeOut : MonoBehaviour {
private void OnEnable()
{
this.GetComponent<SpriteRenderer>().color = new Color
(this.GetComponent<SpriteRenderer>().color.r,
this.GetComponent<SpriteRenderer>().color.g,
this.GetComponent<SpriteRenderer>().color.b,
0
);
}
void Update()
{
//如果透明度大于零,减少,在此前提下,最后一次执行如果小于零,等于零
if (this.GetComponent<SpriteRenderer>().color.a < 1)
{
this.GetComponent<SpriteRenderer>().color = new Color
(this.GetComponent<SpriteRenderer>().color.r,
this.GetComponent<SpriteRenderer>().color.g,
this.GetComponent<SpriteRenderer>().color.b,
this.GetComponent<SpriteRenderer>().color.a + 0.02f
);
if (this.GetComponent<SpriteRenderer>().color.a > 1)
{
this.GetComponent<SpriteRenderer>().color = new Color
(this.GetComponent<SpriteRenderer>().color.r,
this.GetComponent<SpriteRenderer>().color.g,
this.GetComponent<SpriteRenderer>().color.b,
1
);
//在最后一次执行后,关闭物体
//this.gameObject.SetActive(false);
}
}
}
}
在FadeInOutControl物体添加“Control_FadeInOut_Panel”脚本 并将两个子物体拖拽进去 脚本如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Control_FadeInOut_Panel : MonoBehaviour {
public static Control_FadeInOut_Panel Instance;
public GameObject FadeInPanel;
public GameObject FadeOutPanel;
private void Awake()
{
Instance = this;
}
void Update () {
}
//开启执行一次淡入
public void OpenFade_In_Panel() {
FadeInPanel.SetActive(true);
}
//开启执行一次淡出
public void OpenFade_Out_Panel()
{
FadeOutPanel.SetActive(true);
}
//关闭淡入界面
public void CloseFade_In_Panel()
{
FadeInPanel.SetActive(false);
}
//关闭淡出界面
public void CloseFade_Out_Panel()
{
FadeOutPanel.SetActive(false);
}
}
使用方法 调用Control_FadeInOut_Panel 里的方法就可以了 如:
Control_FadeInOut_Panel.Instance.CloseFade_Out_Panel();
Control_FadeInOut_Panel.Instance.OpenFade_In_Panel();
@Liam:有用→收藏→关注 听说长得好看的人都这么做!