회원 로그인
|
![](http://dvframes.com/rb/_var/simbol/artsone.jpg)
Class (클래스)
Class (클래스)
SingleCharacterScript
Inventory
MovementControls
Shooting
http://unity3d.com/learn/tutorials/modules/beginner/scripting/classes
![]() | 클래스의 기본 의미는 여기서 다루진 않겠다. 클래스 설명은 Inventory 한에서 작성되었다. |
클래스는 스크립트를 생성할 때의 이름으로 자동으로 생성 되는 MonoBehaviour 클래스 기반으로 작성되어 진다.
파일명과 파일 안에 있는 MonoBehaviour의 클래스명과 같아야 하고, 스크립트명을 지을 때도 미리 신경을 써줘야 한다.
예제에서는 Inventory, MovementControls, Shooting 3개의 클래스를 분할 처리하는 것을 SingleCharacterScript 1개의 클래스로 변환하여 관리가 편리해지는 목적을 둔다. 다만 복잡하지 않게 충분히 클래스를 설계하여 작성하는 것을 권장한다.
*클래스 안의 서브 클래스
기본적으로 스크립트 파일을 생성할 때 클래스명이 생성되며 그 안에 클래스를 활용 할 수 있도록 서브 클래스가 존재한다.
서브 클래스 안엔 서브 클래스명과 같은 이름의 함수들이 존재해야 한다. 같은 이름의 함수들은 필요 시에 계속 생성 가능하고, 인스턴스 구분 또한 인자의 갯수로 적용된다.
그냥 작동해 보기
1. scene에 cube로 만든 바닥(floor)과 바닥위에 cube를 하나 생성한다.
2. 아래 bold된 이름으로 4개의 스크립트를 작성한다.
3. cube에 Inventory, MovementControls, Shooting 스크립트를 cube에 적용한다.
4. 실행하여 움직여 본다.
5. Inventory, MovementControls, Shooting 스크립트 끄고, SingleCharacterScript 적용하여 실행해 본다.
SingleCharacterScript
using UnityEngine; using System.Collections; public class SingleCharacterScript : MonoBehaviour { public class Stuff { public int bullets; public int grenades; public int rockets; public Stuff(int bul, int gre, int roc) { bullets = bul; grenades = gre; rockets = roc; } } public Stuff myStuff = new Stuff(10, 7, 25); public float speed; public float turnSpeed; public Rigidbody bulletPrefab; public Transform firePosition; public float bulletSpeed; void Update () { Movement(); Shoot(); } void Movement () { float forwardMovement = Input.GetAxis("Vertical") * speed * Time.deltaTime; float turnMovement = Input.GetAxis("Horizontal") * turnSpeed * Time.deltaTime; transform.Translate(Vector3.forward * forwardMovement); transform.Rotate(Vector3.up * turnMovement); } void Shoot () { if(Input.GetButtonDown("Fire1") && myStuff.bullets > 0) { Rigidbody bulletInstance = Instantiate(bulletPrefab, firePosition.position, firePosition.rotation) as Rigidbody; bulletInstance.AddForce(firePosition.forward * bulletSpeed); myStuff.bullets--; } } }
Inventory
using UnityEngine; using System.Collections; public class Inventory : MonoBehaviour { //서브 클래스 public class Stuff { //변수값들 선언 public int bullets; public int grenades; public int rockets; //추가된 변수 public float fuel; //서브 클래스와 그 안의 함수들의 이름이 같아야 한다. public Stuff(int bul, int gre, int roc) { //인자로 들어온 값들을 치환 bullets = bul; grenades = gre; rockets = roc; } //추가적인 변수를 수행할 때 따로 함수로 생성 public Stuff(int bul, float fu) { //인자로 들어온 값들을 치환 bullets = bul; fuel = fu; } // Constructor public Stuff () { //변수 초기화 bullets = 1; grenades = 1; rockets = 1; } } // Creating an Instance (an Object) of the Stuff class // 인스턴스 생성 // public 서브클래스명 인스턴스명 = new 서브클래스안의 함수 public Stuff myStuff = new Stuff(50, 5, 5); // Stuff(인자1,인자2) 함수의 인스턴스 생성 public Stuff myOtherStuff = new Stuff(50, 1.5f); void Start() { Debug.Log(myStuff.bullets); } }
MovementControls
usng UnityEngine; using System.Collections; public class MovementControls : MonoBehaviour { public float speed; public float turnSpeed; void Update () { Movement(); } void Movement () { float forwardMovement = Input.GetAxis("Vertical") * speed * Time.deltaTime; float turnMovement = Input.GetAxis("Horizontal") * turnSpeed * Time.deltaTime; transform.Translate(Vector3.forward * forwardMovement); transform.Rotate(Vector3.up * turnMovement); } }
Shooting
using UnityEngine; using System.Collections; public class Shooting : MonoBehaviour { public Rigidbody bulletPrefab; public Transform firePosition; public float bulletSpeed; private Inventory inventory; void Awake () { inventory = GetComponent참고(); } void Update () { Shoot(); } void Shoot () { if(Input.GetButtonDown("Fire1") && inventory.myStuff.bullets > 0) { Rigidbody bulletInstance = Instantiate(bulletPrefab, firePosition.position, firePosition.rotation) as Rigidbody; bulletInstance.AddForce(firePosition.forward * bulletSpeed); inventory.myStuff.bullets--; } } }
http://unity3d.com/learn/tutorials/modules/beginner/scripting/classes
![](/rb/modules/bbs/theme/_pc/list01/image/ico_comment.gif)
146개(1/8페이지)
번호 | 제목 | 글쓴이 | 조회 | 날짜 |
---|---|---|---|---|
![]() |
유니티 강좌 모음(영문) | 푸딩뱃살 | 61755 | 2013.08.28 12:02 |
![]() |
유니티 경고, 에러 모음 (재정리 예정)
![]() |
푸딩뱃살 | 73190 | 2013.08.12 00:09 |
144 |
[콘솔] 오류 : ~ multicast group
![]() |
푸딩뱃살 | 1130 | 2019.09.04 00:23 |
143 |
[콘솔] 오류 : NullReferenceException
![]() |
푸딩뱃살 | 1142 | 2019.07.23 00:26 |
142 | [유니티] Unite Seoul 2019 다시보기 | 푸딩뱃살 | 1286 | 2019.06.28 10:00 |
141 | [정보] GDC 2019 유니티 키노트 하이라이트 | 푸딩뱃살 | 1390 | 2019.03.26 10:45 |
140 |
[유니티] Unite Seoul 2018 아젠다 & 세션 다시보기
![]() ![]() |
푸딩뱃살 | 2775 | 2018.05.25 10:32 |
139 |
[VR/AR] Stereo 360 Image and Video Capture
![]() ![]() |
푸딩뱃살 | 2067 | 2018.02.02 10:53 |
138 | [VR/AR] ARKit - Face Tracking | 푸딩뱃살 | 1811 | 2018.01.17 16:57 |
137 | [참고] ADAM :Making 참고 | 푸딩뱃살 | 2060 | 2017.12.04 11:14 |
136 |
[정보] ARKit Face Tracking on iPhone X
![]() |
푸딩뱃살 | 3170 | 2017.11.08 09:59 |
135 | [VR/AR] Vive Full Body Tracking Demo | 푸딩뱃살 | 2172 | 2017.10.31 18:54 |
134 |
[애셋] 유니티 모델링 - UModeler
![]() ![]() |
푸딩뱃살 | 2214 | 2017.10.20 14:02 |
133 |
[유니티] 맥에서 안드로이드/자바 SDK 경로
![]() ![]() |
푸딩뱃살 | 3358 | 2017.10.04 23:58 |
132 |
[콘솔] 오류 : Fatal Error! The project is on case sensitive file
![]() ![]() |
푸딩뱃살 | 2817 | 2017.10.01 19:59 |
131 | [참고] Adam (Made with Unity) | 푸딩뱃살 | 2537 | 2017.10.01 18:33 |
130 | [정보] 보안 패치 | 푸딩뱃살 | 1946 | 2017.08.21 15:59 |
129 |
[콘솔] 오류: Failed to load '.dll', ......
![]() |
푸딩뱃살 | 2129 | 2017.08.20 00:57 |
128 |
[콘솔] 오류: [VRDevice] Initialization of device oculus failed.
![]() |
푸딩뱃살 | 1970 | 2017.08.19 03:05 |
127 |
[콘솔] 오류: MissingComponentException
![]() |
푸딩뱃살 | 3002 | 2017.08.19 02:05 |
126 |
[콘솔] 오류: Android Manifests
![]() |
푸딩뱃살 | 2772 | 2017.08.18 03:01 |
125 | [VR/AR] Google VR SDK for Unity | 푸딩뱃살 | 3589 | 2017.08.18 00:10 |