라벨이 Unity인 게시물 표시

비주얼 스튜디오 유니코드로 저장하기

이미지
.editorconfig로 파일명으로 프로젝트 폴더 안에 저장한다. root = true [*] charset = utf-8 다음과 같이 저장한다.

유니티 AddComponent를 사용하여 오브젝트에 스크립트 붙이기

이미지
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 public   class  MyExam : MonoBehaviour {      // Start is called before the first frame update      void  Start()     {          // GameObject ob = new GameObject("TestAddCom");           GameObject ob  =  GameObject.Find( "TestAddCom" );         TestAddCom addCom  =  ob.AddComponent < TestAddCom > ();     }       }   Colored by Color Scripter cs TestAddCom 오브젝트를 찾은 후  AddComponent를 사용하여 스크립트를 붙여준다.

유니티 필수 폴더

이미지
유니티 폴더에는 Assets Library Logs obj Packages ProjectSettings 등 여러 폴더등이 있는데 용량등의 문제로 필수 폴더만 유지 할려면  Assets 폴더와 ProjectSettings 폴더만 있으면 된다.

[유니티] Deterministic compilation failed. You can disable Deterministic builds in Player Settings 에러

이미지
 패키지관리자에서 Multiplayer HLAP 에셋을 업데이트 한다

유니티 코루틴(Coroutine)

이미지
리턴형으로 IEnumerater를 사용한다 yield return을 사용해서 IEnumerater함수에서 빠져나올 수 있다. 또한 WaitForseconds함수를 사용하여 매개값으로 주어진 시간이 지난 후에 다시 복귀하게 할 수 있다. public class CRTest : MonoBehaviour { private void Start() { StartCoroutine("TestCoroutine"); } IEnumerator TestCoroutine() { Debug.Log("코루틴 시작"); yield return new WaitForSeconds(3.0f); Debug.Log("3초 후 빠져나감"); } } 특정 오브젝트를 페이드 아웃 시키는 예제 public class FadeOut : MonoBehaviour { private SpriteRenderer spriteRenderer; void Start () { spriteRenderer = GetComponent (); } void Update () { Color color = spriteRenderer.color; if (color.a > 0.0f) { color.a -= 0.1f; spriteRenderer.color = color; } } } 특정 오브젝트를 1초동안 페이드아웃 public class FadeOut : MonoBehaviour { private SpriteRenderer spriteRenderer; void Start () { spriteRenderer = GetComponent (); StartCoroutine ("RunFadeOut"); } void Update () { } IEnumerator RunFad...

유니티 싱글톤

이미지
public class Singleton: MonoBehaviour { public static Singleton Instance; //Awake is always called before any Start functions void Awake() { if (Instance) { DestroyImmediate(gameObject); } else { Instance = this; DontDestroyOnLoad(gameObject); } } }

유니티 씬 이동

using UnityEngine.SceneManagement; public class MoveScene: MonoBehaviour { public void GotoScene() { SceneManager.LoadScene("SceneName"); } }

유니티 OnEnable() , OnDisable()

OnEnable() 함수는 오브젝트가 활성화될 경우 자동으로 호출한다. Awake() 함수는 한 번만 호출되지만 OnEnable() 함수는 비활성화되어 있다가 다시 활성화될 때마다 호출이 계솔 이우루어진다. ExamOnEnable01, ExamOnEnable02 를 사용해 오브젝트 활성 비활성 public class ExamOnEnable01 : MonoBehaviour { private void Awake() { Debug.Log("I'm here Awake"); } private void OnEnable() { Debug.Log("I'm here OnEnable"); } private void OnDisable() { Debug.Log("I'm here OnDisable"); } private void Start() { Debug.Log("I'm here Start"); } } public class ExamOnEnable02 : MonoBehaviour { public GameObject exam01; private void Update() { if (Input.GetKeyDown(KeyCode.UpArrow)) { exam01.SetActive(false); } if (Input.GetKeyDown(KeyCode.DownArrow)) { exam01.SetActive(true); } } }

유니티 Awake

Awake 함수는 스크립트가 비활성화되어 있어도 호출되는 함수이며 오브젝트를 초기화할 때 많이 사용된다. public class ExAwake : MonoBehaviour { private void Awake() { Debug.Log("Awake Exam"); } private void Start() { Debug.Log("Start Exam"); } }

유니티 애니메이션 속도

이미지
애니메이터 Parameter에서 float형의 AttSpeed를 만들어준다. 속도를 조절하고자 하는 Animation State에서 Parameter를 체크한 후 추가한 AttSpeed를 선택한다. 속도변경을 위해 코드를 수정한다. using UnityEngine; public class Exsample : MonoBehaviour { public float speed= 1.0f; public Animator animator; public void AniExsample() { animator.SetFloat("AttSpeed", speed); } }

유니티 씬 불러오기

using UnityEngine.SceneManagement; // 씬 불러오기 SceneManager.LoadScene ("Home"); // 씬 확인하기 if (SceneManager.GetActiveScene().name === "Home") // PlayerPrefs 정보모두삭제 PlayerPrefs.DeleteAll(); //안드로이드 백키 종료 if(Input.GetKeyDown(KeyCode.Escape)) { Application.Quit(); }

유니티 디바이스 디버깅

cmd창에서 android sdk 폴더에 sdk/platform-tools 경로로 이동한다. 휴대폰에서 발생하는 모든 이벤트 -> adb logcat 유니티에서 발생하는 모든 이벤트 -> adb logcat -s Unity 유니티에서 발생하는 log 이벤트 -> adb logcat Unity:I Native:I *:S

유니티 의존 컴포넌트를 자동추가

using UnityEngine; // PlayerScript requires the GameObject to have a Rigidbody component [RequireComponent (typeof (Rigidbody))] public class PlayerScript : MonoBehaviour { Rigidbody rb; void Start() { rb = GetComponent (); } void FixedUpdate() { rb.AddForce(Vector3.up); } } 출처: https://docs.unity3d.com/ScriptReference/RequireComponent.html

유니티 파티클 이펙트 사이즈 변경 코드

// This script will only work in editor mode. You cannot adjust the scale dynamically in-game! using UnityEngine; using System.Collections; #if UNITY_EDITOR using UnityEditor; #endif [ExecuteInEditMode] public class ParticleScaler : MonoBehaviour { public float particleScale = 1.0f; public bool alsoScaleGameobject = true; float prevScale; void Start() { prevScale = particleScale; } void Update() { #if UNITY_EDITOR // check if we need to update if (prevScale != particleScale && particleScale > 0) { if (alsoScaleGameobject) transform.localScale = new Vector3(particleScale, particleScale, particleScale); float scaleFactor = particleScale / prevScale; // scale legacy particle systems ScaleLegacySystems(scaleFactor); // scale shuriken particle systems ScaleShurikenSystems(scaleFactor); // scale trail renders ScaleTrailRenderers(scaleFactor); prevScale = particleScale; } #endif } void ScaleShurikenSystems(float scaleFactor...

Unity3D PlayerPrefs 삭제

public class EditorTools { [MenuItem("Tools/Reset PlayerPrefs", false)] public static void ResetPlayerPref() { PlayerPrefs.DeleteAll(); Debug.Log("*** PlayerPrefs was reset! ***"); } }

Unity3D Singleton

case : 1 using UnityEngine; using System.Collections; public class Singleton: MonoBehaviour { public static Singleton Instance; //Awake is always called before any Start functions void Awake() { if (Instance) { DestroyImmediate(gameObject); } else { Instance = this; DontDestroyOnLoad(gameObject); } } } case 2 : public class MyClass { private static MyClass _instance; public static MyClass Instance { get { if (!_instance) { _instance = GameObject.FindObjectOfType(typeof(MyClass)); if (!_instance) { GameObject container = new GameObject(); container.name = "My...

유니티 카메라 초기값

이미지
3D Camera Light 2D Camera

유니티 오브젝트 서서히 사라지게

private SpriteRenderer _spriteRenderer; // Use this for initialization void Start() { StartCoroutine(FadeAway()); } IEnumerator FadeAway() { yield return new WaitForSeconds(1); while (_spriteRenderer.color.a > 0) { var color = _spriteRenderer.color; //color.a is 0 to 1. So .5*time.deltaTime will take 2 seconds to fade out color.a -= (.5f * Time.deltaTime); _spriteRenderer.color = color; //wait for a frame yield return null; } Destroy(gameObject); }

유니티 파티클

ParticleSystem testParticle = null; // 버튼이 눌러졌을때 if (Input.GetMouseButtonUp(0) == true) { // 파티클이 있고 if (testParticle) { // 파티클이 재생중이면 재생을 멈추고 지워줍니다 if (testParticle.isPlaying == true) { testParticle.Stop(); testParticle.Clear(); //Debug.Log("STOP"); } // 재생중이 아니라면 재생해주고요 else { testParticle.Play(); //Debug.Log("PLAY"); } } // 파티클이 없다면 새로 만들어주구요 else { Vector3 pos = Vector3.zero; pos.y = 3; pos.x = 3 - 30.0f; Transform particleObject = (Transform)Instantiate(Resources.Load("Prefabs/Effects/pfBlockBomb", typeof(Transform)), pos, Quaternion.identity); testParticle = (ParticleSystem)particleObject.GetComponent(typeof(ParticleSystem)); //Debug.Log("CREATE"); } return; } 출...

유니티 오브젝트풀

https://www.youtube.com/watch?v=nzHzd7B22Zc&t=99s http://hyunity3d.tistory.com/195