회원 로그인
|
Struct (구조체) (with Unity)
C# Struct
[구조체]
참조 형식이 아닌 값 형식으로 객체를 생성하고 싶을때 사용하는 문법 형식
[클래스와의 차이]
- 선언만으로도 생성 가능
- 매개변수 없는 생성자 사용 불능
- 상속, 정적 형식으로 사용할 수 없음
- 반드시 초기화 해줘야 함
- 값을 보관 저장
[구조체]
참조 형식이 아닌 값 형식으로 객체를 생성하고 싶을때 사용하는 문법 형식
[클래스와의 차이]
- 선언만으로도 생성 가능
- 매개변수 없는 생성자 사용 불능
- 상속, 정적 형식으로 사용할 수 없음
- 반드시 초기화 해줘야 함
- 값을 보관 저장
using UnityEngine; using System.Collections; // struct 카피가 필요할 때 // prefab은 GameObject의 정보를 가지고 있는 파일 (게임 오브젝트가 아님) public struct SAttackInfo { public string _attackName; public int _damage; public int _time; public SAttackInfo(string attackName, int damage, int time) { this._attackName = attackName; this._damage = damage; this._time = time; } public void PrintAttackInfo() { Debug.Log("공격명 : " + _attackName); Debug.Log("공격력 : " + _damage); Debug.Log("공격시간 : " + _time); } } public class CStructEx : MonoBehaviour { void Start () { SAttackInfo attackInfo1; attackInfo1._attackName = "기본 공격"; attackInfo1._damage = 100; attackInfo1._time = 5; // 주석 처리시 에러 발생 attackInfo1.PrintAttackInfo(); AddAttackInfoValue(attackInfo1); attackInfo1.PrintAttackInfo(); SAttackInfo attackInfo2 = new SAttackInfo("특수공격", 200, 3); attackInfo2.PrintAttackInfo(); AddAttackInfoValue(attackInfo2); attackInfo2.PrintAttackInfo(); // SendMessage로 복수개의 데이터를 보낼때 gameObject.SendMessage("AddAttackInfoValue", attackInfo1); } public void AddAttackInfoValue(SAttackInfo attackInfo) { attackInfo._damage += 10; attackInfo._time += 2; } public void ShowAttackInfoValue(SAttackInfo attackInfo) { Debug.Log("공격명 : " + attackInfo._attackName); Debug.Log("공격력 : " + attackInfo._damage); Debug.Log("공격시간 : " + attackInfo._time); } }
댓글 0개
| 엮인글 0개
285개(1/15페이지)
번호 | 제목 | 글쓴이 | 조회 | 날짜 |
---|---|---|---|---|
285 | [Python] 동적 import - 모듈을 변수로 받아오기 | 푸딩뱃살 | 427 | 2022.10.27 10:45 |
284 | [Python] 파이썬 3.7.7과 3.9.7의 os.path.expanduser() 차이 | 푸딩뱃살 | 469 | 2022.08.18 12:22 |
283 | [Python] error: Microsoft Visual C++ 9.0 is required. | 푸딩뱃살 | 706 | 2022.08.03 13:35 |
282 | [Python] pyscript | 푸딩뱃살 | 476 | 2022.06.09 11:21 |
281 | [Python] float is / float not is | 푸딩뱃살 | 604 | 2022.03.02 15:03 |
280 | [Python] 이터널 문자열 f | 푸딩뱃살 | 852 | 2022.01.27 16:35 |
279 | [Python] is와 ==의 차이 | 푸딩뱃살 | 489 | 2021.11.25 15:54 |
278 |
[Python] Error: ImportError: file |
푸딩뱃살 | 947 | 2021.11.16 11:24 |
277 | [Python] 파이썬 디컴파일 - uncompyle6 | 푸딩뱃살 | 789 | 2021.11.10 14:46 |
276 | [Python] 파이썬 확장자 설명 | 푸딩뱃살 | 563 | 2021.11.03 14:38 |
275 | [참고] 웹 fbx 뷰어 | 푸딩뱃살 | 482 | 2021.10.19 15:46 |
274 | [Python] enumerate() | 푸딩뱃살 | 508 | 2021.10.13 14:44 |
273 | [Python] 아나콘다에서 가상 환경 | 푸딩뱃살 | 746 | 2020.11.21 00:26 |
272 | [Python] pip로 설치 때 퍼미션 에러 | 푸딩뱃살 | 1280 | 2020.06.06 17:13 |
271 | [Python] OpenCV 10-3. 이미지 Thresholding - Otsu's Binarizatio | 푸딩뱃살 | 681 | 2020.06.05 14:01 |
270 | [Python] OpenCV 10-2. 이미지 Thresholding - Adaptive Threshold | 푸딩뱃살 | 704 | 2020.06.05 13:58 |
269 | [Python] OpenCV 10-1. 이미지 Thresholding | 푸딩뱃살 | 585 | 2020.06.05 13:56 |
268 | [Python] OpenCV 9-2. 색 추적 | 푸딩뱃살 | 796 | 2020.06.02 23:29 |
267 | [Python] OpenCV 9-1. 색공간 바꾸기 | 푸딩뱃살 | 646 | 2020.06.02 23:27 |
266 | [Python] OpenCV 8-3. 이미지 비트 연산 | 푸딩뱃살 | 537 | 2020.06.02 23:21 |