Adventure Creator Wikia
Advertisement

This script allows 3D objects to be faded in and out.

The script is intended to work with the Standard shader in Unity's default render pipeline - it will need to be adapted to work with othe pipelines or shaders.

To use it:

  1. Set the material's Shader to Standard, and set the Rendering Mode to Fade.
  2. Copy/paste the code below into a C# script named SetMaterialAlpha.
  3. Attach the new Set Material Alpha component to the object to fade.
  4. Use either the Object: Send message or Object: Call event Actions to call the component's TurnOff and TurnOn functions to hide / show the object respectively.

SetMaterialAlpha.cs:

using UnityEngine;

public class SetMaterialAlpha : MonoBehaviour
{

	public float speed = 5f;
	private float targetAlpha = 1f;

	public void TurnOn ()
	{
		targetAlpha = 1f;
	}

	public void TurnOff ()
	{
		targetAlpha = 0f;
	}

	void Update ()
	{
		Color color = GetComponent<Renderer> ().material.color;
		float alpha = color.a;
		alpha = Mathf.MoveTowards (alpha, targetAlpha, Time.deltaTime * speed);
		color.a = alpha;
		GetComponent<Renderer> ().material.color = color;
	}
}
Advertisement