Explain.
Unity 2D Sprite Renderer
+
2D Physics
+
Animator
를 활용하여 오브젝트를 제작시 사용.
AutoDepth, ColorFade 기능구현
AutoDepth, ColorFade 기능구현
using UnityEngine; using System.Collections; [RequireComponent(typeof(BoxCollider2D))] [RequireComponent(typeof(SpriteRenderer))] [RequireComponent(typeof(Animator))] [RequireComponent(typeof(Rigidbody2D))] public class MyObject : MonoBehaviour { private SpriteRenderer mSpriteRenderer; private BoxCollider2D mBoxCollider2D; private Animator mAnimator; private Transform mTransform; public void FadeOut() { StartCoroutine(fadeOutCoroutine()); } public void ONObject() { _BoxCollider2D.enabled = true; _SpriteRenderer.enabled = true; _Animator.enabled = true; } public void OFFOjbect() { _BoxCollider2D.enabled = false; _SpriteRenderer.enabled = false; _Animator.enabled = false; } private IEnumerator fadeOutCoroutine(float duration = 3f) { float startTime = Time.time; while (Time.time - startTime < duration) { mSpriteRenderer.color = Color.Lerp(Color.white, Color.clear, (Time.time - startTime) / duration); yield return true; } _SpriteRenderer.color = Color.clear; } public void ColorFade(Color fadeColor) { StartCoroutine("colorFade", fadeColor); } public void ColorFade(Color fadeColor, float duration, int rc) { StartCoroutine(colorFade(fadeColor, duration, rc)); } public void StartAutoDepth() { StartCoroutine("depthCoroutine"); } public void SetDepth() { _SpriteRenderer.sortingOrder = (int)(-_LocalPosition.y * 200f); } public void StopAutoDepth() { StopCoroutine("depthCoroutine"); } private IEnumerator depthCoroutine() { yield return new WaitForSeconds(.1f); _SpriteRenderer.sortingOrder = (int)(-_LocalPosition.y * 200f); StartCoroutine("depthCoroutine"); } private IEnumerator colorFade(Color fadeColor, float duration = 2f, int rc = 3) { float once = duration / (rc * 2); float startTime = Time.time; for (int i = 0; i < rc; i++) { while (Time.time - startTime < once) { mSpriteRenderer.color = Color.Lerp(Color.white, fadeColor, (Time.time - startTime) / once); yield return null; } mSpriteRenderer.color = fadeColor; startTime = Time.time; while (Time.time - startTime < once) { mSpriteRenderer.color = Color.Lerp(fadeColor, Color.white, (Time.time - startTime) / once); yield return null; } mSpriteRenderer.color = Color.white; startTime = Time.time; } } public Vector2 _LocalPosition { get { if (mTransform == null) mTransform = transform; return mTransform.localPosition; } set { if (mTransform == null) mTransform = transform; mTransform.localPosition = value; } } public Vector2 _LocalScale { get { if (mTransform == null) mTransform = transform; return mTransform.localScale; } set { if (mTransform == null) mTransform = transform; mTransform.localScale = value; } } public Animator _Animator { get { if (mAnimator == null) mAnimator = GetComponent <Animator>(); return mAnimator; } set { mAnimator = value; } } public SpriteRenderer _SpriteRenderer { get { if (mSpriteRenderer == null) mSpriteRenderer = GetComponent<SpriteRenderer>(); return mSpriteRenderer; } set { mSpriteRenderer = value; } } public BoxCollider2D _BoxCollider2D { get { if (mBoxCollider2D == null) mBoxCollider2D = GetComponent<BoxCollider2D>(); return mBoxCollider2D; } set { mBoxCollider2D = value; } } }