
THAM GIA GROUP CỦA TTC TRÊN FACEBOOK
ScreenFlasher.instance.StartFlashScreen (color,startDuration, holdDuration, endDuration);
Với:
ScreenFlasher.instance.StartFlashScreen (Color.white, 0.2f, 0.2f, 0.2f);
[/thank]using UnityEngine;
using System.Collections;
//====================================================================================================
/// <summary>
/// Screen flasher.
/// </summary>
//====================================================================================================
public class ScreenFlasher : MonoBehaviour
{
private static ScreenFlasher _instance;
public Color color = Color.white;
public Coroutine behaviorFlash;
private Texture2D sampleTexture;
//----------------------------------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Gets the instance.
/// </summary>
/// <value>The instance.</value>
//----------------------------------------------------------------------------------------------------------------------------------------------------
public static ScreenFlasher instance {
get {
if (_instance == null) {
var obj = new GameObject ("ScreenFlasher");
_instance = obj.AddComponent<ScreenFlasher> ();
}
return _instance;
}
}
//----------------------------------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Awake this instance.
/// </summary>
//----------------------------------------------------------------------------------------------------------------------------------------------------
public void Awake ()
{
sampleTexture = new Texture2D (1, 1);
color.a = 0;
// Refresh ();
}
//----------------------------------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Raises the destroy event.
/// </summary>
//----------------------------------------------------------------------------------------------------------------------------------------------------
public void OnDestroy ()
{
Destroy (sampleTexture);
}
//----------------------------------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Refresh this instance.
/// </summary>
//----------------------------------------------------------------------------------------------------------------------------------------------------
// private void Refresh ()
// {
//
// }
//
//----------------------------------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Raises the GU event.
/// </summary>
//----------------------------------------------------------------------------------------------------------------------------------------------------
public void OnGUI ()
{
if (behaviorFlash != null) {
sampleTexture.SetPixel (0, 0, color);
sampleTexture.Apply ();
GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), sampleTexture);
}
}
//----------------------------------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Starts the flash screen.
/// </summary>
/// <returns>The flash screen.</returns>
/// <param name="color">Color.</param>
/// <param name="flashInDuration">Flash in duration.</param>
/// <param name="holdDuration">Hold duration.</param>
/// <param name="flashOutDuration">Flash out duration.</param>
//----------------------------------------------------------------------------------------------------------------------------------------------------
public Coroutine StartFlashScreen (Color color, float startDuration = 0.2f, float holdDuration = 0.2f, float endDuration = 0.2f)
{
return behaviorFlash = StartCoroutine (FlashScreen (color, startDuration, holdDuration, endDuration));
}
//----------------------------------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Stops the flash screen.
/// </summary>
//----------------------------------------------------------------------------------------------------------------------------------------------------
public void StopFlashScreen ()
{
if (behaviorFlash != null) {
StopCoroutine (behaviorFlash);
color.a = 0;
}
behaviorFlash = null;
}
//----------------------------------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Routines the flash screen.
/// </summary>
/// <returns>The flash screen.</returns>
//----------------------------------------------------------------------------------------------------------------------------------------------------
protected IEnumerator FlashScreen (Color color, float startDuration, float holdDuration, float endDuration)
{
this.color = color;
yield return StartCoroutine (FlashIn (startDuration));
yield return new WaitForSeconds (holdDuration);
yield return StartCoroutine (FlashOut (endDuration));
yield break;
}
//----------------------------------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Flashs the in.
/// </summary>
/// <returns>The in.</returns>
/// <param name="flashInDuration">Flash in duration.</param>
//----------------------------------------------------------------------------------------------------------------------------------------------------
public IEnumerator FlashIn (float flashInDuration)
{
float t = 0;
color.a = 0;
// Refresh ();
while (t < 1) {
yield return 0;
t += Time.deltaTime / flashInDuration;
color.a = (Mathf.Lerp (0, 1, t));
// Refresh ();
}
yield break;
}
//----------------------------------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Flashs the out.
/// </summary>
/// <returns>The out.</returns>
/// <param name="flashOutDuration">Flash out duration.</param>
//----------------------------------------------------------------------------------------------------------------------------------------------------
public IEnumerator FlashOut (float flashOutDuration)
{
float t = 0;
color.a = 1;
// Refresh ();
while (t < 1) {
yield return 0;
t += Time.deltaTime / flashOutDuration;
color.a = (Mathf.Lerp (1, 0, t));
// Refresh ();
}
yield break;
}
}
[/hide]
Comments
Không xài thì thôi, để các bạn khác xài nghe hơm
THAM GIA GROUP CỦA TTC TRÊN FACEBOOK
Để tét coi sao đã
Từ VX nhảy qua Unity rồi sao
:P :P :P
THAM GIA GROUP CỦA TTC TRÊN FACEBOOK
THAM GIA GROUP CỦA TTC TRÊN FACEBOOK
:v Bác gần giống em, tuy code hay nhưng ram đang yếu nên sẽ lục lại khi ram đủ,
giờ xài mỗi RPG MAKER thì không lag, sài Unity thì lagg vãi đái ý :v
Mà sắp nâng ram rồi xD
Có khi nào lag là do dùng Visual Studio không, thằng đấy nặng lắm
:< :< :<
THAM GIA GROUP CỦA TTC TRÊN FACEBOOK
Chắc vậy ::v Mà nâng ram lên vẫn lag thì thôi em nghỉ làm Unity