유니티(87)
-
[C#] Static 변수 / Static 함수 / Static 변수의 자기참조
public class Test : MonoBehaviour { public static int number; void Start() { Test.number = 20; print(Test.number); MyStaticFunc(10); } public static void MyStaticFunc(int a) { print(Test.number + a); } } static 변수 number를 생성하였고, static 함수 myStaticFunc()를 생성하였다. (**static 함수 안에서는 멤버변수를 사용할 수 없고 함수 입력 인자, 함수 내부 변수 또는 static 변수가 들어가야 한다. 외부 변수는 class의 메모리에 생성되기 때문) Static 변수의 자기참조 Public class Mana..
2023.08.24 -
[Unity Project] <ShootOut2D> 2D 슈팅 게임 만들기(1) 플레이어 설정
간단한 횡 스크롤 2D 슈팅 게임을 만들어 보고자 한다. Asset Store에서 무료 에셋을 다운받을 수 있다. Warped Space Shooter | 2D 캐릭터 | Unity Asset Store Elevate your workflow with the Warped Space Shooter asset from Ansimuz. Find this & more 캐릭터 on the Unity Asset Store. assetstore.unity.com 반복될 필요 없는 스프라이트 이미지이기 때문에[Wrap Mode] - [Clamp]로 설정하고, 선형 보간이 필요 없기 때문에 [Filter Mode] - [Point]로 설정하고 [Max size]는 스프라이트 크기에 맞게 32로 설정, [Compressi..
2023.08.23 -
[Unity] 오브젝트 통과와 충돌, 제거(Collider / Rigid Body / Destroy)
오브젝트 통과 하나의 게임 오브젝트가 다른 오브젝트와 충돌하면 OnTriggerEnter를 호출하게 되며 둘 다 Collider가 있어야 한다. 플레이어 오브젝트에 Rigid Body가 있어야 하며 Is Kinematic 체크가 활성화되어있어야 한다. 두 오브젝트 모두 Is Trigger 체크박스를 활성화하면 충돌이 발생하지 않으므로 하나에만 체크해야 한다. (**Is Kinematic 체크 활성화는 물리 계산을 하지 않는다는 뜻이다. Kinematic 뜻 자체가 운동학이라는 뜻이라 조금 헷갈린다.) public class MoveScript : MonoBehaviour { public float speed = 2; private void OnTriggerEnter(Collider other) { pri..
2023.08.17 -
[Unity] GameObject.Find
GameObject.Find public class Test : MonoBehaviour { GameObject other; void Start() { other = GameObject.Find("Obj2"); other.GetComponent().material.color = Color.red; } } Hierarchy에서 "Obj2"라는 이름의 오브젝트를 찾은 후 색을 바꿀 수 있다. public class Test : MonoBehaviour { public GameObject other; void Start() { other.GetComponent().material.color = Color.red; } } public GameObject other; 을 이용하면 오브젝트를 직접 끌어다 놓아서 색..
2023.08.16 -
[Unity] 오브젝트의 위치와 이동 (transform.position / transform.Translate / normalized)
오브젝트의 위치 읽어오기 public class Test : MonoBehaviour { void Start() { Vector3 pos; pos = transform.position; print(pos); } } 해당 오브젝트의 현재 위치를 읽어오려면 위의 코드를 사용하면 되며, x, y, z 특정 위치를 알고자 한다면 pos = transform.position.x의 형식으로 지정해 주면 된다. 오브젝트 위치 입력하기 transform은 프로퍼티 get, set을 쓰기 때문에 transform.position.x = 10; 은 오류가 발생한다. Vector3 값을 transform.position에 대입하면 된다. 아래와 같은 예시로 살펴볼 수 있다. public class Test : MonoBeh..
2023.08.14 -
[Unity] 변수를 보호하는 방법(캡슐화) - 접근 제한자 / 프로퍼티
접근 제한자 이용 public class Test : MonoBehaviour { public class Player { private int hp; public int GetHP() { return hp; } public void SetHP(int value) { hp = value; } } void Start() { Player player = new Player(); player.SetHP(50); int hp = player.GetHP(); print(hp); } } Player 클래스를 하나 만들고 hp라는 변수를 private으로 설정하였다. GetHP, SetHP로만 hp 변수를 변경하고 값을 받아올 수 있으며 직접적으로 hp 변수에 값을 대입할 수는 없다. 프로퍼티 사용 public cla..
2023.08.14