using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; public class Chapter3 : MonoBehaviour, IEnumerator { int count = 11; //±âº»ÀûÀ¸·Î Á¢±ÙÁöÁ¤ÀÚ´Â private // ÀÎÅÍÆäÀ̽º ±¸Çö public bool MoveNext()=> --count >=0;// count¸¦ 1°¨¼Ò ½Ã۰í 0ÀÌ»óÀ̸é true ¹Ýȯ public object Current => count;// ÇöÀç count °ª ¹Ýȯ public void Reset() => count = 11;// count °ªÀ» 11·Î ÃʱâÈ­ // enumÀº ¸í½ÃÀûÀ¸·Î ÀÛ¼ºÇÒ¼ö ÀÖ°í ¾Ï½ÃÀûÀ¸·Î ÀÛ¼ºÇÒ¼ö ÀÖ´Ù. public enum BorderSide : byte// enumÀÇ ±âº» ÀÚ·áÇüÀº int ÀÌ´Ù. { Left = 1, Right, // 2 Top = 4, Bottom // 5 ¹Ù·Î ¾ÕÀÇ °ªÀ» ±âÁØÀ¸·Î 1¾¿ Áõ°¡ } // [Flags] // ºñÆ® Ç÷¡±×·Î »ç¿ëÇϱâ À§ÇÑ ¾îÆ®¸®ºäÆ® // 2ÀÇ Á¦°ö¼ö·Î ÀúÀåÇÏ´Â °Ç Flags¸¦ »ç¿ëÇÏ´Â°Ô °ü·ÊÀÌ´Ù [Flags] // À־ µÇ°í ¾ø¾îµµ µÇÁö¸¸ ºñÆ® Ç÷¡±×¸¦ »ç¿ë½Ã ¸í½ÃÀûÀ¸·Î Ç¥½ÃÇϱâ À§ÇØ »ç¿ë public enum FlagsEnum : byte { None = 0, A = 1 << 0, // 1 B = 1 << 1, // 2 C = 1 << 2, // 4 D = 1 << 3 // 8 } void Start() { // FlagsEnum ¸ðµÎ Ãâ·Â Çϱâ foreach (FlagsEnum flag in Enum.GetValues(typeof(FlagsEnum))) { Debug.Log($"Flag: {flag}, Value: {(byte)flag}"); } // Stack Á¦³×¸¯ Ŭ·¡½º »ç¿ë ¿¹Á¦ Stack intStack = new Stack(); intStack.Push(10); intStack.Push(20); Debug.Log(intStack.Pop()); // 20 Ãâ·Â Debug.Log(intStack.Pop()); // 10 Ãâ·Â // Swap Á¦³×¸¯ ¸Þ¼­µå »ç¿ë ¿¹Á¦ int x = 5, y = 10; Debug.Log($"Before Swap: x = {x}, y = {y}"); Swap(ref x, ref y); Debug.Log($"After Swap: x = {x}, y = {y}"); // Initialize Á¦³×¸¯ Á¦¾àÁ¶°Ç ¸Þ¼­µå »ç¿ë ¿¹Á¦ MyClass[] myClassArray = new MyClass[3]; Initialize(myClassArray); foreach (var item in myClassArray) { Debug.Log(item != null ? $"{item}" : "Null"); } // Initialize2 Á¦³×¸¯ Á¦¾àÁ¶°Ç ¸Þ¼­µå »ç¿ë ¿¹Á¦ Initialize2(new string[] { "Hello", "World", "!" }); } /// /// Á¦³×¸¯À» »ç¿ëÇÑ Swap ¸Þ¼­µå /// /// /// /// void Swap(ref T a, ref T b) { T temp = a; a = b; b = temp; } /// /// Á¦³×¸¯ Á¦¾àÁ¶°ÇÀ» »ç¿ëÇÏ¿© new() »ý¼ºÀÚ¸¦ È£ÃâÇÒ¼ö ÀÖ´Â ¸Þ¼­µå /// /// /// void Initialize(T[] Array) where T : new() // class, struct µî ÂüÁ¶Çü½Ä, °ªÇü½Ä Á¦¾àÁ¶°Çµµ »ç¿ë °¡´É { for (int i = 0; i < Array.Length; i++) { Array[i] = new T(); } } void Initialize2(T[] Array) where T : notnull // null °ªÀ» Çã¿ëÇÏÁö ¾Ê´Â Á¦¾àÁ¶°Ç { for (int i = 0; i < Array.Length; i++) { Debug.Log(Array[i]); } } /// /// È®ÀÛµÈ Á¦³×¸¯ ¸Þ¼­µå »ç¿ë /// SpecialStack specialStack = new SpecialStack(); void Update() { if (Keyboard.current.wKey.isPressed) { specialStack.PushWithLog(UnityEngine.Random.Range(1, 100)); } if (Keyboard.current.sKey.isPressed) { specialStack.PopWithLog(); } } /// /// Àç±ÍÀû Á¦³×¸¯ ¸Þ¼­µå /// Balloon redBalloon = new Balloon { Color = "Red" }; Balloon blueBalloon = new Balloon { Color = "Blue" }; [ContextMenu("CompareBalloons")] /// À¯´ÏƼ ¿¡µðÅÍ¿¡¼­ ¿ìŬ¸¯ ½ÇÇà¿ë public void CompareBalloons() { bool areEqual = redBalloon.Equals(blueBalloon); Debug.Log($"Are balloons equal? {areEqual}");// False Ãâ·Â } } /// /// ÀÎÅÍÆäÀ̽º´Â ±âº»ÀûÀ¸·Î public ÀÌ´Ù º¯¼ö,ÇÔ¼ö... /// interface IEnumerator // ÀÎÅÍÆäÀ̽º´Â ±âº»ÀûÀ¸·Î public ÀÌ´Ù { bool MoveNext(); object Current { get; } void Reset(); } /// /// Á¦³×¸¯Àº ÅÆÇø´È­µÈ Ŭ·¡½º ¶Ç´Â ÇÔ¼öÀÌ´Ù. /// /// class Stack// Ŭ·¡½º´Â ±âº»ÀûÀ¸·Î internal ÀÌ´Ù { int position; T[] items = new T[100]; public void Push(T i) => items[position++] = i; public T Pop() => items[--position]; } /// /// ÆÄ»ýµÈ Á¦³×¸¯ Ŭ·¡½º¸¦ »ó¼ÓÇÏ´Â Á¦³×¸¯ Ŭ·¡½º /// /// class SpecialStack : Stack { public void PushWithLog(T value) { Debug.Log($"Push: {value}"); Push(value); } public T PopWithLog() { T value = Pop(); Debug.Log($"Pop: {value}"); return value; } } interface IEquatable { bool Equals (T obj ); }/// Á¦³×¸¯ ÀÎÅÍÆäÀ̽º class Balloon : IEquatable // Á¦³×¸¯ ÀÎÅÍÆäÀ̽º ±¸Çö { public string Color { get; set; } public bool Equals(Balloon other) { return this.Color == other.Color; } }