오늘도 어김없이 만드는...
빈 오브젝트를 하나 만들고 위치리셋 한 다음 CoroutineExample로 이름을 바꾼다. 그리고 당연히 동일이름의 오브젝트를 하나 만들고 넣는다.
그리고 위와 똑같이 CoroutuneCounter 라는 이름으로 오브젝트, 스크립트를 만들어줍니다.
그리고 이와같이 손으로 직접 넣어줍니다.
CoroutuneCounter.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoroutineCounter : MonoBehaviour
{
/*private void Start()
{
StartCoroutine(CounterCoroutine());
}*/
private void OnEnable()
{
Debug.Log("On Enable");
}
private void OnDisable()
{
Debug.Log("On Disable");
}
private void Reset()
{
Debug.Log("Reset");
}
private IEnumerator Start()
{
gameObject.SetActive(true);
bool isActive = gameObject.activeSelf;
this.enabled = true;
Debug.Log("Start1");
yield return new WaitForSeconds(1f);
Debug.Log("Start2");
}
private void OnMouseDown()
{
Debug.Log("On Mouse Down");
}
private IEnumerator OnMouseDrag()
{
Debug.Log("Drag");
return null;
}
private void Update()
{
Debug.Log("Counter");
}
public void CounterStart()
{
StartCoroutine(CounterCoroutine());
}
//코르틴은 스크립트를 꺼도 돌아간다.
private IEnumerator CounterCoroutine()
{
int cnt = 0;
while (true)
{
Debug.Log("cnt" + cnt++);
yield return new WaitForSeconds(1f);
}
}
}
CoroutuneExample.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoroutuneExample : MonoBehaviour
{
[SerializeField] private CoroutineCounter counter = null;
private Coroutine testCo = null;
private void Start()
{
testCo = StartCoroutine(TestCoroutine());
StartCoroutine(TestCoroutine());
StartCoroutine("ExampleCoroutine", 1);
//StartCoroutine("ExampleCoroutine", 2);
//StartCoroutine("ExampleCoroutine", 3);
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
//멈출수가 없다.
//StopCoroutine(TestCoroutine());
StopCoroutine(testCo);
}
if (Input.GetKeyDown(KeyCode.Alpha2))
{
//멈출수 있다.
StopCoroutine("ExampleCoroutine");
}
if (Input.GetKeyDown(KeyCode.Alpha3))
{
//멈출수 있다.
StopAllCoroutines();
}
if (Input.GetKeyDown(KeyCode.Space))
{
counter.CounterStart();
}
}
private IEnumerator TestCoroutine()
{
WaitForSeconds wait1Sec = new WaitForSeconds(1f);
while (true)
{
Debug.Log("1");
yield return wait1Sec;
Debug.Log("2");
yield return new WaitForEndOfFrame();
}
}
private IEnumerator ExampleCoroutine(int _num)
{
while (true)
{
Debug.Log(_num + "1");
yield return new WaitForSeconds(0.5f);
Debug.Log(_num + "2");
}
}
}
그리고 floor 프리팹을 가져온다
그리고 저번에 프리셋을 가져온다
그리고 머테리얼에 페이드 를 주고
그림자를 끕니다.
빈 프로젝트로 Patrolmanager을 만듭니다.
그리고 각각에 스크립트를 만듭니다.
PatrolManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PatrolManager : MonoBehaviour
{
[SerializeField] private PatrolChicken chicken = null;
[SerializeField] private PatrolPointerHolder pph = null;
private void Start()
{
Vector3 chickenPos = chicken.GetPosition();
Vector3 nearPointPos = pph.GetNearPoint(chickenPos);
chicken.PatrolStart(nearPointPos);
chicken.PatrolDoneCallback = PatrolDoneCallback;
}
private void PatrolDoneCallback()
{
//chicken.PatrolStart(pph.GetNearPoint(chicken.GetPosition()));
chicken.PatrolStart(pph.GetRandomPoint());
}
//게이른 초기화
//Lazy Initialization
/*public bool Init()
{
chicken.Init();
PatrolPointerHolder.Init();
return true;
}*/
}
PatrolChicken.cs
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
public class PatrolChicken : MonoBehaviour
{
public delegate void PatrolDoneDelegate();
private PatrolDoneDelegate patrolDoneCallback = null;
public PatrolDoneDelegate PatrolDoneCallback
{
set { patrolDoneCallback = value; }
}
private bool isMoving = false;
private float waitTime = 1f;
//Destination
public void PatrolStart(Vector3 _dest)
{
if(isMoving)
{
return;
}
StartCoroutine(PatrolCoroutine(_dest));
}
private IEnumerator PatrolCoroutine(Vector3 _dest)
{
Vector3 startPos = transform.position;
Vector3 endPos = _dest;
float t = 0f;
endPos.y = transform.position.y;
isMoving = true;
while (true)
{
t += Time.deltaTime;
transform.position = Vector3.Lerp(startPos, endPos, t);
yield return null;
}
isMoving = false;
yield return new WaitForSeconds(waitTime);
patrolDoneCallback?.Invoke();
}
public Vector3 GetPosition()
{
return transform.position;
}
}
PatrolPointerHolder.cs
using System.Collections;
using System.Collections.Generic;
using System.Net;
using UnityEngine;
public class PatrolPointerHolder : MonoBehaviour
{
private Transform[] patrolPoints = null;
private int lastPointIdx = 0;
private void Awake()
{
patrolPoints = GetComponentsInChildren<Transform>();
}
public Vector3 GetNearPoint(Vector3 _sour)
{
int nearIdx = 1;
if(nearIdx == lastPointIdx)
{
++nearIdx;
}
/* if (int i = nearIdx + 1; i < patrolPoints.Length; ++i){
if(i = lastPointIdx)
{
continue;
}
}*/
if (patrolPoints == null || patrolPoints.Length < 1)
{
return Vector3.zero;
}
for(int i = 2; i < patrolPoints.Length; ++i)
{
float nearDist = (_sour - patrolPoints[nearIdx].position).magnitude;
float curDist = (_sour - patrolPoints[i].position).magnitude;
if (curDist < nearDist)
{
nearIdx = i;
}
}
lastPointIdx = nearIdx;
return patrolPoints[nearIdx].position;
}
public Vector3 GetRandomPoint()
{
return patrolPoints[Random.Range(0, patrolPoints.Length)].position;
}
}