반응형
반응형
코드 작성 void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) { if (stream.IsWriting) { stream.SendNext(transform.rotation); } else { Quaternion rotation = (Quaternion)stream.ReceiveNext(); float lag = Mathf.Abs((float)(PhotonNetwork.Time - info.timestamp)); transform.rotation = Quaternion.Lerp(transform.rotation, rotation, lag); } }
코드 작성 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; } ..
입장조건이 있는 방 만들기 void JoinOrCreateRoom() { RoomOptions roomOption = new RoomOptions(); roomOption.MaxPlayers = 2; roomOption.CustomRoomPropertiesForLobby = new string[] { "difficulty" }; roomOption.CustomRoomProperties = new Hashtable() { { "difficulty", "easy" } }; PhotonNetwork.JoinOrCreateRoom("room name", roomOption, null); } 조건에 맞는 방 참여하기 public void JoinRandomRoom() { Hashtable roomPropertie..
유니티에서 제공하는 Instantiate가 아닌 포톤에서 제공하는 PhotonNetwork.Instantiate로 생성한뒤 Photon Transform View 또는 Photon Animator View 를 달아주게 되면 자동으로 동기화가 이루어집니다. 생성할 Prefab의 위치는 Resource 폴더 안에 있어야합니다. 코드 예시 using UnityEngine; using Photon.Pun; public class MyScript : MonoBehaviourPunCallbacks { public GameObject prefab; void Start() { PhotonNetwork.Instantiate(prefab.name, transform.position, transform.rotation, 0..
For문 for (int i = 0; i < 10; i++) { print("현재 값은 $i"); } Switch문 int grade = 80; switch (grade) { case 90: print("Grade is A"); break; case 80: print("Grade is B"); break; case 70: print("Grade is C"); break; default: print("Grade is not A, B, or C"); break; }
삼항 연산자 A = 조건문 ? : 참일때 : 거짓일때 예시 코드 using UnityEngine; public class TernaryOperatorExample : MonoBehaviour { private int health = 70; void Start() { // 만약 health가 50보다 크면 "건강한 상태"를 출력하고, // 그렇지 않으면 "위험한 상태"를 출력합니다. string healthStatus = (health > 50) ? "건강한 상태" : "위험한 상태"; Debug.Log(healthStatus); } } 예시 코드 2 using UnityEngine; public class TernaryOperatorExample : MonoBehaviour { private bool i..
코드 예시 int num = 0; if (num > 0) { print("0보다 큽니다."); } else if (num < 0) { print("0보다 작습니다."); } else { print("0입니다."); } 참고할만한 글
private 속성을 inspector에서 접근 가능하게 해줍니다. 코드 작성 using UnityEngine; using System; [Serializable] public class MyClass { [SerializeField] private int myPrivateInt; [SerializeField] private string myPrivateString; public MyClass(int myPrivateInt, string myPrivateString) { this.myPrivateInt = myPrivateInt; this.myPrivateString = myPrivateString; } }
유니티 C# 반올림, 올림, 내림, 소수점 2자리 버리기 간단 구현반올림 Roundfloat myFloat = 3.6f;int myInt = Mathf.RoundToInt(myFloat);Debug.Log(myInt); // 출력: 4 반올림 Ceilfloat myFloat = 3.2f;int myInt = Mathf.CeilToInt(myFloat);Debug.Log(myInt); // 출력: 4 내림 Floorfloat myFloat = 3.8f;int myInt = Mathf.FloorToInt(myFloat);Debug.Log(myInt); // 출력: 3 소수점 전부 버리기float myFloat = 3.14159f;int myInt = Mathf.FloorToInt(myFloat);Debug...
전체 수정 rectTransform.sizeDelta = new Vector2(width, height); Width 수정 private void SetWidth(float width) { rectTransform.sizeDelta = new Vector2(width, rectTransform.sizeDelta.y); } Height 수정 private void SetHeight(float height) { rectTransform.sizeDelta = new Vector2(rectTransform.sizeDelta.x, height); }