• 카테고리

    질문 & 답변
  • 세부 분야

    게임 프로그래밍

  • 해결 여부

    미해결

manager들 사이의 호출순서가 궁금해요

23.03.04 22:38 작성 23.03.04 22:56 수정 조회수 413

0

manager

 

public class Managers : MonoBehaviour

{

static Managers s_Instance; //유일성이 보장된다

public static Managers instance { get { Init(); return s_Instance; } } // 유일한 매니저를 갖고온다

InputManager _input = new InputManager();

public static InputManager input { get { return instance._input; } }

// Start is called before the first frame update

void Start()

{

Init();

}

// Update is called once per frame

void Update()

{

_input.OnUpdate();

}

static void Init()

{

if (s_Instance == null)

{

GameObject go = GameObject.Find("@Managers");

if (go == null)

{

go = new GameObject { name = "@Managers" };

go.AddComponent<Managers>();

}

DontDestroyOnLoad(go);

s_Instance = go.GetComponent<Managers>();

}

}

}

==================================

playercontroller.cs

 

public class PlayerController : MonoBehaviour

{

[SerializeField]

float _speed = 10.0f;

// Start is called before the first frame update

void Start()

{

Managers.input.KeyAction -= OnKeyboard; //실수 방지용으로 미리 한번 -함

Managers.input.KeyAction += OnKeyboard;

}

// Update is called once per frame

void Update()

{

 

 

}

 

void OnKeyboard()

{

if (Input.GetKey(KeyCode.W))

{

transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.forward), 0.2f);

transform.position += Vector3.forward Time.deltaTime _speed;

}

if (Input.GetKey(KeyCode.S))

{

transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.back), 0.2f);

transform.position += Vector3.back Time.deltaTime _speed;

}

if (Input.GetKey(KeyCode.A))

{

transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.left), 0.2f);

transform.position += Vector3.left Time.deltaTime _speed;

}

if (Input.GetKey(KeyCode.D))

{

transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.right), 0.2f);

transform.position += Vector3.right Time.deltaTime _speed;

}

}

}

==================================

public class InputManager

{

public Action KeyAction = null;

 

 

public void OnUpdate()

{

if (Input.anyKey == false)

return;

if (KeyAction != null)

KeyAction.Invoke();

}

}

==================================

player.cs

 

public class Player : MonoBehaviour

{

 

// Start is called before the first frame update

void Start()

{

Managers mg = Managers.instance;

}

// Update is called once per frame

void Update()

{

 

}

}

 

지금 unitychan 오브잭트에 playercontroller 컴포넌트를 inspector창에다 추가한뒤에 실행을 하는데 input값을 입력받을떄마다 어떤식으로 작동이되는지 순서가 이해가 안가요

playercontroller 에 update()에 아무것도 없는데 지속적으로 어떻게 key값을 받는지 궁금해요

start()는 1회실행되는 함수인데

player.cs도 지금 존재 이유가 유일한 manager 컴포넌트를 쓰게하기 위해서 만들었는데[ 점점 갈수록 어렵네요

 

답변 1

답변을 작성해보세요.

0

delegate와 콜백에 대해 리서치를 해보시기 바랍니다.

_input.OnUpdate(); 이 부분의 KeyAction.Invoke();에서,
콜백으로 등록된 함수들이 딸려서 실행되고 있는 것입니다.

하지만 질문자 분처럼 너무 어려워 하는 분들이 많아서
이 부분은 추후 제거되었으니 참고 바랍니다