유니티 화면 실행 화면
유니티 기본 설명 및 이동 코드
Materials를 만들때는 Mat를 앞에 붙여씀
player.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
[Range(0.0f, 100f)] // 스크롤 바
[SerializeField] // 프라이빗으로 지정하고 유니티에 띄울 수 있도록 해줌
private float speed = 0f; //접근 지정자를 생략 시 기본 private
//유니티에선 변수를 퍼블릭으로 할 시 유니티 상에 수정 가능하게 표기됨
// 코드상에서 변수명을 바꿀 시 유니티에서 입력한 값이 초기화됨
// c#은 m_ 전역벽수가 없으므로 멤버변수 구분이 필요 없음
private void Update()
{
if (Input.GetKey(KeyCode.W))
MoveToDir(Vector3.forward);
if (Input.GetKey(KeyCode.S))
MoveToDir(Vector3.back);
if (Input.GetKey(KeyCode.A))
MoveToDir(Vector3.left);
if (Input.GetKey(KeyCode.D))
MoveToDir(Vector3.right);
}
public void MoveToDir(Vector3 _dir)
{
//Transform이란 유니티가 만들어논 클래스를 가져와서 사용
//Vector3 pos = this.GetComponent<Transform>().position; // 밑에 껄 풀어 씀
Vector3 newPos = this.transform.position;
newPos = newPos + (_dir * speed * Time.deltaTime); //업뎃에서 가저올 필요 없이 Time에서 가져옴
transform.position = newPos;
// int val = new int(); // 동적 할당 c++과는 () 차이
// Garbage Collector로 인해 C#은 따로 동적할당 해체가 필요없음.
// 대신 포퍼먼스가 떨어지며 정리 타이밍의 컨트롤이 불가함
// 메모리가 부족할때 작동하며
// 레벨이라는 개념이 있고 그건 돌때마다 살아있으면 레벨업해서 메모리 해제 우선순위에서 제외함
// 우선순위가 잘못 매겨져 메모리가 너무 부족하면 대규모 공사함
}
}
[Range(0.0f, 100f)]
위 코드 중 일부 인스펙터 속성 유니티 상 UI를 띄우는 방법
유니티 인스펙터 속성(Unity Inspect.. : 네이버블로그 (naver.com)
추격 컴퍼넌트를 작성
MoveToDir.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveToDir : MonoBehaviour
{
private Vector3 dir = Vector3.zero;
public float speed{ get; set; } // 자신에게 넘길때
//Getter / Setter C++ , C#
//Property C#
public Vector3 Dir
{
get { return dir; }
set { dir = value; }
}
// 받아서 다른 곳에 넘길때
private void Update()
{
MoveToDirection();
}
private void MoveToDirection()
{
Vector3 newPos = transform.position;
newPos += dir * speed * Time.deltaTime;
transform.position = newPos;
}
}
C#에선 get; set;을 위와 같이 두 부류로 사용
추격 컴퍼넌트를 사용한 코드
Enemy.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
[SerializeField] private GameObject targetGo = null; //게임 오브젝트 받음
[SerializeField] private Transform targetTr = null; //트랜스 폼을 바로 받음
[SerializeField] private float speed = 7f;
//private readonly float speed = 7f; 상수 const 도 가능
private MoveToDir moveToDir = null;
private void Awake() // Startup 과정에서 Start보다 빨리 호출되는 객체
{
moveToDir = GetComponent<MoveToDir>();
moveToDir.speed = speed;
}
private void Update()
{
if (targetTr == null) return;
//Transform tr = targetGo.GetComponent<Transform>();
//타켓 포지션(플레이어) - 몬스터(this)
Vector3 dir = targetTr.position - transform.position;
dir.Normalize(); //벡터 정규화
//MoveToDir(dir);
moveToDir.Dir = dir;
}
//private void MoveToDir(Vector3 _dir)
//{
// Vector3 newPos = transform.position;
// newPos += _dir * speed * Time.deltaTime;
// transform.position = newPos;
//}
}
유니티 라이프 사이클(Unity Lifecycle) 호출 되는 순서이며
유니티는 한개를 컴파일하는 형식이 아닌 다 호출해놓고 실행 시킴, 객체지향
'수업 일기장' 카테고리의 다른 글
Unity.3 기초 및 Prefabs (0) | 2021.02.24 |
---|---|
Unity.2 UI/UX 및 버튼 이동 (0) | 2021.02.24 |
수업 일기장 #C++(2) (0) | 2021.01.25 |
수업 일기장 #클래스(Class) (0) | 2021.01.25 |
수업 일기장 #참조(Reference) (0) | 2021.01.25 |