누군가2의 코드분석
=================================
App.Class
더보기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class App : MonoBehaviour //App 클래스
{
public Button btn; //public 으로 버튼 뚫어서 넣음
void Start()
{
//버튼 클릭시 인게임으로 넘어감(람다식)
this.btn.onClick.AddListener(() => { SceneManager.LoadScene("Ingame"); });
}
void Update()
{
}
}
|
App클래스에서 바로 InGame으로 넘어가는것이 아니라, 버튼을 클릭해야 넘어감.
InGame.Class
더보기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Ingame : MonoBehaviour
{
public Transform targetPoint; //public으로 뚫어서 targetPoint 넣음
private GameObject modelPrefab; //모델 프리팹 넣을 게임오브젝트.
private Hero hero; //히어로를 넣어둘 공간
public Button btn; //public으로 뚫어서 버튼 넣음
void Start()
{
this.btn.onClick.AddListener(() => //버튼 클릭시 실행
{
if (this.hero != null) //히어로가 null이 아니면?
{
//hero 클래스 Move 메서드를 호출, 매개변수 targetPoing의 position값
this.hero.Move(this.targetPoint.position);
}
});
this.modelPrefab = Resources.Load<GameObject>("ch_05_01"); //프리팹 불러옴
this.CreateHero(); //CreateHero 메서드 호출
}
private void CreateHero() //CreateHero 메서드
{
var shell = new GameObject(); //껍데기 생성
//프리팹을 매개로 modelgo라는 clone 게임오브젝트를 실체화
var modelgo = Instantiate<GameObject>(this.modelPrefab);
//Hero컴포넌트 붙여서 Ingame 클래스의 멤버변수 hero에 할당
this.hero = shell.AddComponent<Hero>();
this.hero.Init(modelgo); //hero 클래스의 Init 메서드 호출, 매개변수 modelgo 게임오브젝트
}
// Update is called once per frame
void Update()
{
}
}
|
Hero.Class
더보기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class Hero : MonoBehaviour
{
public Animation anim; //애니메이션을 컨트롤하기 위해 선언
private bool isMove; //움직이는거 판단
private bool isAttack; //어택했는지 안했는지 판단
public GameObject model; //model 자체를 가져와서 애니메이션을 저장하기 위해 선언
private Vector3 targetposition; //position값을 가져와서 targetposition에 저장하기 위해 선언
public AnimationState animState; // AnimationState 이펙트 효과를 공격모션에 넣기위해 선언
public UnityAction OnAttackComplete; //대리자 OnAttackComplete 선언
private float elapsedTime; //경과시간 저장하기 위해 선언
private float speed = 1f; //속도 저장
void Start()
{
}
public void Init(GameObject model) //Init 함수, 매개변수 model을 받아옴
{
this.model = model; //현재 클래스 멤버변수 model에 매개변수 model 할당
//현재 클래스 멤버변수 model의 애니메이션 컴포넌트를 가져와서
//먼저 선언해둔 현재클래스의 anim에 할당
this.anim = this.model.GetComponent<Animation>();
//매개변수로 가져온 model을 현재 클래스의 자식으로 넣음
this.model.transform.SetParent(this.transform, false);
}
public void Move(Vector3 position) //Move메서드, 매개변수 position을 받아옴
{
this.isMove = true; //isMove = true
//매개변수로 받아온 position 값을 현재 멤버변수의 targetposition에 할당
this.targetposition = position;
this.anim.Play("run@loop"); //run@loop 애니메이션 실행
}
public void Idle()
{
this.isMove = false; //isMove = true
this.anim.Play("idle@loop"); //idle@loop 애니메이션 실행
}
void Update()
{
if (isMove) //isMove가 true일때
{
//현재 Hero 컴포넌트가 붙어 있는 오브젝트의 transform의 값을 변경
//앞으로 계속 전진
//여기서 하나 배웠습니다.
//여지껏 나는 컴포넌트와 게임오브젝트를 분리해서 생각했는데,
//현재 Hero 클래스의 컴포넌트가 붙어있는 오브젝트자체를 움직여도 되는구나라는 생각의 전환이듬.
transform.Translate(Vector3.forward * this.speed * Time.deltaTime);
//targetposition과 transform.position의 값의 차를 구해서
var dis = Vector3.Distance(targetposition, transform.position);
if (dis <= 0.02f) //그 차가 0.02f 작거나 같다면
{
this.Idle(); //멤버 메서드 Idle을 호출
}
}
}
}
|
===============================================
다른분들의 코드를 보면서 결과는 같아도 방법은 여러가지라는 것을 깨달았습니다.
여기서도 하나 배워갑니다.
감사합니다ㅎㅎ
'Unity > 코드분석' 카테고리의 다른 글
2020-05-16 코드분석 1-1 [게임플랫폼평가] (0) | 2020.05.16 |
---|