Unity
-
Unity - PrefabsUnity 2021. 3. 25. 21:30
필요성 반복된 재사용, 미리 만들어둔 object유형을 쉽게 만들 수 있다. 폴더위치 Assets/Resources/Prefabs Prefab생성 https://docs.unity3d.com/kr/2018.4/Manual/CreatingPrefabs.html Hierachy에 만들어 둔 Gameobject를 Prefabs폴더로 드래그해서 넣으면 Prefab을 만든 후 해당 Gameobject는 적용된 상태로 변경된다. (Hierachy에 아이콘이 회색에서 파랑색으로 변한다.) Prefab 인스턴스화 https://docs.unity3d.com/Manual/InstantiatingPrefabs.html Object prefabObject = Resources.Load("Prefabs/PfTest2"); D..
-
Unity - MonoBehaviour 생성Unity 2021. 3. 25. 21:25
MonoBehaviour는 관리되는 Object로, 직접 생성해서는 안된다. //MonoBehaviourClass생성 //mb1 = new Cmb1(); 으로 생성하면 경고 발생. //경고: You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all UnityEngine.MonoBehaviour:.ctor () public class Cmb1 : MonoBehav..
-
Unity - Script Component얻기Unity 2021. 3. 25. 20:54
Unity - Script Component얻기 //Unity - GameObject와 부모 클래스 변환 일반적 사용 구조 MonoBehavior //Player1.cs class Player1 : MonoBehavior - 부모중에 GameObject gameObject;를 소유 - Player1에 기능에 필요한 여러함수들은 gameObject를 호출하여 wrapping하는 방식으로 구현. //UnityEditorUI(hierarchy, root)에서 생성한 GameObject class GameObject : Object - Components정보들에 script가 있다. Player1은 GameObject의 Component일 뿐이다. //GameObject는 MonoBehavior에 소속된 Mem..
-
Unity - VertexUnity 2021. 3. 22. 22:19
기존의 vertex를 random으로 뒤튼다. using UnityEngine; public class Example : MonoBehaviour { Mesh mesh; Vector3[] vertices; void Start() { mesh = GetComponent().mesh; vertices = mesh.vertices; } void Update() { for (var i = 0; i < vertices.Length; i++) { vertices[i] += vertices[i] * Random.Range(-100, 100)/100.0f * Time.deltaTime; } // assign the local vertices array into the vertices array of the Mesh. ..
-
Unity - 튜플 리턴 타입(tuple return type)Unity 2021. 3. 22. 21:49
(int, int, float) f1() { return (1,2,1.5f);} (int j, int k, float l) f2() { return (1,2,1.5f);} void fmain() { (int a, int b, float f)r = f1(); (int c, int d, float g) = f1(); Debug.Log("type1: " + r.a); Debug.Log("type2: " + c); Debug.Log("type3: " + f2().k); }
-
Unity - Renderer.material 수정Unity 2021. 3. 21. 20:57
코드로 Material 설정을 변경할 수 있다. public Material mMaterial; void Start() { mMaterial = GetComponent().material; } void OnMouseDown() { mMaterial.color = Color.white; } void OnMouseUp() { mMaterial.color = Color.red; } void OnDestroy() { //Destroy the instance Destroy(mMaterial); } Material 수량 확인. Resources.FindObjectsOfTypeAll(typeof(Material)).Length GetComponent().material을 사용하는 시점에 Material 수량이 증가한..
-