Pico fades in and out

Posted by swasheck on Thu, 10 Oct 2019 01:14:19 +0200

On Unity's fade-in and fade-out capabilities for Pico (for beginners like me)

Say less nonsense and go straight to the process

Create an empty object FadeInOutControl under Pvr_UnitySDK--Head
Create two sub-objects under FadeInOutControl: FadeIn and FadeOut
Add Sprite Renderer to two sub-objects with a picture
FadeIn's Color is set to (0, 0, 0, 255) FadeOut's Color is set to (0, 0, 0, 0)

Add "FadeIn" script to FadeIn object as follows

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 the transparency is greater than zero, it decreases. On this premise, if the last execution is less than zero, it is equal to zero.
        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
                );
                //Close the object after the last execution
                this.gameObject.SetActive(false);
            }
        }

    }
}

Add the "FadeOut" script script to the FadeOut object as follows

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 the transparency is greater than zero, it decreases. On this premise, if the last execution is less than zero, it is equal to zero.
        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
                );
                //Close the object after the last execution
                //this.gameObject.SetActive(false);
            }
        }
    }
}

Add the "Control_FadeInOut_Panel" script to the FadeInOutControl object and drag two child objects into it. The script is as follows

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 () {
		
	}
    //Open and execute a fade in
    public void OpenFade_In_Panel() {
        FadeInPanel.SetActive(true);
    }
    //Open and execute a fade-out
    public void OpenFade_Out_Panel()
    {
        FadeOutPanel.SetActive(true);
    }
    //Close fade-in interface
    public void CloseFade_In_Panel()
    {
        FadeInPanel.SetActive(false);
    }
    //Close fade-out interface
    public void CloseFade_Out_Panel()
    {
        FadeOutPanel.SetActive(false);
    }

}

Use the method to call the method in Control_FadeInOut_Panel.
Control_FadeInOut_Panel.Instance.CloseFade_Out_Panel();
Control_FadeInOut_Panel.Instance.OpenFade_In_Panel();

Topics: less Unity