반응형
반응형
코드 작성 using UnityEngine; public class MathFunctions : MonoBehaviour { public int Factorial(int num) { if (num == 0) { return 1; } else { return num * Factorial(num - 1); } } }
Substring string myString = "Hello World!"; string subString = myString.Substring(6, 5); Debug.Log(subString); // Output: "World" Split string myString = "Hello, World!"; string[] substrings = myString.Split(','); // Output: "Hello", " World!" Replace string myString = "Hello World!"; string replacedString = myString.Replace("Hello", "Hi"); Debug.Log(replacedString); // Output: "Hi World!" Index..
코드 작성 using UnityEngine; using UnityEngine.UI; public class UIOverlapChecker : MonoBehaviour { public RectTransform rectTransform1; public RectTransform rectTransform2; private Rect rect1; private Rect rect2; void Update() { rect1 = new Rect(rectTransform1.position.x - rectTransform1.rect.width / 2, rectTransform1.position.y - rectTransform1.rect.height / 2, rectTransform1.rect.width, rectTransf..
코드 작성 using UnityEngine; public class PlayerController : MonoBehaviour { public float jumpForce = 10f; public float moveSpeed = 5f; public float groundCheckRadius = 0.2f; public LayerMask whatIsGround; public Transform groundCheck; private Rigidbody2D rb; private bool isGrounded = false; void Start() { rb = GetComponent(); } void FixedUpdate() { isGrounded = Physics2D.OverlapCircle(groundCheck.p..
코드 작성using UnityEngine;using System.Collections;public class Roulette : MonoBehaviour{ public int[] numbers; //당첨될 것들 private int winningNumber; //당첨 번호 private bool spinning = false; public float spinTime = 5.0f; void Start() { winningNumber = -1; } void Update() { if (spinning) { transform.Rotate(Vector3.up, 10.0f); } } ..
코드 작성 using UnityEngine; public class Enemy : MonoBehaviour { public float speed = 5f; private Transform player; void Start() { // 태그로 플레이어 찾기 player = GameObject.FindGameObjectWithTag("Player").transform; } void Update() { Vector3 direction = player.position - transform.position; direction.Normalize(); transform.position += direction * speed * Time.deltaTime; } }
숫자 정렬using System.Linq;using UnityEngine;public enum MyEnum{ First, Second, Third}public class MyObject{ public MyEnum EnumValue { get; set; } public int SomeValue { get; set; }}public class Example : MonoBehaviour{ private List objects; private void Start() { objects = new List() { new MyObject() { EnumValue = MyEnum.Third, SomeValue = 5 }, ..
옵저버 패턴 객체 간에 일대다 종속 관계를 정의하여 한 객체의 상태 변경이 다른 객체에 자동으로 통지되는 패턴. 게임 내에서 이벤트 시스템 또는 UI 업데이트와 같은 상황에서 유용합니다. using UnityEngine; using System; // 이벤트에 대한 데이터 public class EventData { public string eventName; public int eventValue; } // 옵서버 인터페이스 public interface IObserver { void OnNotify(EventData data); } // 옵서버를 관리하는 클래스 public class Subject { private List _observers = new List(); // 옵서버 등록 public ..
코드 작성 using UnityEngine; public class AngleCalculator : MonoBehaviour { public Transform pointA; public Transform pointB; void Start() { Vector2 direction = pointB.position - pointA.position; float angle = Vector2.Angle(Vector2.right, direction); Debug.Log("Angle between the two points: " + angle); } } 다른 예시 using UnityEngine; public class AngleCalculator : MonoBehaviour { public Transform point..
코드 작성 using UnityEngine; using System.Collections.Generic; public class RandomNumberGenerator : MonoBehaviour { private List exclusionList = new List() { 2, 3, 5, 6 }; //제외할 값 private int GenerateRandomNumber(int min, int max) { int randomValue = Random.Range(min, max); while (exclusionList.Contains(randomValue)) { randomValue = Random.Range(min, max); } return randomValue; } }
코드 작성 using UnityEngine; using System.Collections; using System.IO.Ports; public class BluetoothController : MonoBehaviour { public string deviceName = "MyBluetoothDevice"; public int baudRate = 9600; private SerialPort serialPort; void Start() { string[] ports = SerialPort.GetPortNames(); foreach (string port in ports) { if (port.Contains("Bluetooth") && port.Contains(deviceName)) { serialPort ..
코드 작성using UnityEngine;public class GyroController : MonoBehaviour{ private bool gyroEnabled; private Gyroscope gyro; private Quaternion rot; void Start() { gyroEnabled = EnableGyro(); } private bool EnableGyro() { if (SystemInfo.supportsGyroscope) { gyro = Input.gyro; gyro.enabled = true; return true; } ..