20160406 Unity 스터디

Unity3D.VR 2016. 4. 6. 10:13

http://docs.unity3d.com/kr/current/Manual/InstantiatingPrefabs.html

# prefeb을 통한 작업으로 오브젝트를 런타임 중에 인스턴스화 할 수 있고, 간단하며, 재사용성이 높다.




# 다양한 GUI 컨트롤 유형들.

http://docs.unity3d.com/kr/current/Manual/gui-Controls.html


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
 
public class Main_2 : MonoBehaviour {
    public Transform brick;
    public Rigidbody rocket;
    public float speed = 10f;
 
    public bool isSet = false;
    public int numberOfObjects = 20;
    public float radius = 5f;
 
    // Use this for initialization
    void Start () {
        initBricks();
    }
 
    void initBricks()
    {
        for (int y = 0; y < 5; y++)
        {
            for (int x = 0; x < 5; x++)
            {
                Instantiate(brick, new Vector3(x, y + 100), Quaternion.identity);
            }
        }
    }
    
    // Update is called once per frame
    void Update () {
        if(Input.GetKeyDown( KeyCode.Space))
        {
            FireRocket();
            setBricks();
        }
    }
 
    void FireRocket()
    {
        // 최소, 최대값 사이의 값을 랜덤으로 생성.
        int rand = Random.Range(-55);
 
        // 유니티에서 생성한 Rigidbody 콤포넌트를 가지는 rocket 오브젝트를 인스턴스화 함.
        Rigidbody rocketClone = (Rigidbody)Instantiate(rocket, transform.position, transform.rotation);
 
        // 오브젝트의 속도 설정.
        rocketClone.velocity = new Vector3(rand, 0, speed);
 
        // rocketClone에 추가된 스크립트 콤포넌트의 함수 호출.
        rocketClone.GetComponent<MyRocketScript>().DoSomething();
    }
 
    void setBricks()
    {
        if (isSet) return;
        isSet = true;
 
        for(int i=0; i<numberOfObjects; i++)
        {
            float angle = i * Mathf.PI * 2 / numberOfObjects;
            Vector3 pos = new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * radius;
            Instantiate(brick, pos, Quaternion.identity);
        }
    }
 
    // Update() 함수 뿐 아니라, 포함하는 스크립트가 활성화 될 때마다 호출.
    // GUI에 해당되는 것들만 여기서 반복 실행되도록.
    void OnGUI()
    {
        //if( Time.time % 2 < 1 )
        //{
        //    setGUIMenu();
        //}
 
        GUIControls();
        ScreenPosition();
        GUIContent();
 
        if( GUI.changed )
        {
            // 모든 gui 변경 감지.
            print("GUI changed");
        }
    }
 
    // Toggle
    private bool toggleBool = true;
 
    // Toolbar
    private int toolbarInt = 0;
    private string[] toolbarStrings = { "bar1""bar2""bar3" };
 
    // SelectionGrid
    private int selectionGridint = 0;
    private string[] selectionStrings = { "Grid 1""Grid 1""Grid 1""Grid 1""Grid 1" };
 
    // HorizontalSlider
    private float hSliderValue = 0;
 
    // VerticalSlider
    private float vSliderValue = 0;
 
    // ScrollView
    private Vector2 scrollViewVector = Vector2.zero;
    private string innerText = "I am inside the ScrollView";
 
    // Window
    private Rect windowRect = new Rect(4002012050);
 
    void GUIControls()
    {
        GUI.Box(new Rect(1001010090), "Loader Menu");
 
        // UnityGUI에서 버튼을 클릭하면 true를 리턴.
        // 클릭 한번에 반응.
        if (GUI.Button(new Rect(100708020), "Level 2"))
        {
            SceneManager.LoadScene(1);
        }
 
 
        // RepeaseButton 마우스 버튼이 눌려진 채 매 프레임마다 반응.
        // click-and-hold 기능을 만들수 있다.
        if( GUI.RepeatButton(new Rect(2002510030), "repeatButton"))
        {
            print("RepeatButton Hold");
        }
 
        // toggle버튼.
        toggleBool = GUI.Toggle(new Rect(2515010030), toggleBool, "Toggle");
 
 
        // Toolbar. Button을 행으로 나열한 것.
        // 다른 Button을 선택하기 전까지 선택되어 있음.
        toolbarInt = GUI.Toolbar(new Rect(2518025030), toolbarInt, toolbarStrings);
 
 
        // SelectionGrid
        selectionGridint = GUI.SelectionGrid(new Rect(2522025060), selectionGridint, selectionStrings, 3);
 
 
        // HorizontalSlider
        hSliderValue = GUI.HorizontalSlider(new Rect(2530010030), hSliderValue, 010);
        GUI.TextField(new Rect(2533010020), hSliderValue.ToString());
 
 
        // VerticalSlider
        vSliderValue = GUI.VerticalSlider(new Rect(2535010030), vSliderValue, 010);
        GUI.TextField(new Rect(2538010020), vSliderValue.ToString());
 
 
        // ScrollView -------------------------------------
        // Begin the ScrollView
        scrollViewVector = GUI.BeginScrollView(new Rect(2525100100), scrollViewVector, new Rect(00400400));
 
        // Put something inside the ScrollView
        innerText = GUI.TextArea(new Rect(00400400), innerText);
 
        // End the ScrollView
        GUI.EndScrollView();
 
 
 
        // Window
        // 제대로 동작하는 다른 함수를 필요로하는 유일한 컨트롤.
        windowRect = GUI.Window(0, windowRect, windowfunc, "Window GUI");
    }
 
    void windowfunc(int windowID)
    {
        // Draw any Controls inside the window here
    }
 
    void ScreenPosition()
    {
        // 화면 전체 크기를 가져다 계산할 수 있음.
        GUI.Box(new Rect(0010050), "Top-left");
        GUI.Box(new Rect(Screen.width - 100010050), "Top-right");
        GUI.Box(new Rect(0, Screen.height - 5010050), "Bottom-left");
        GUI.Box(new Rect(Screen.width - 100, Screen.height - 5010050), "Bottom-right");
    }
 
 
    public Texture2D icon;
 
    void GUIContent()
    {
        // text만 표시.
        GUI.Label(new Rect(300010050), "This is the text string for a Label Control");
 
        // Sprite를 대상으로 받아서 이미지만 표시.
        GUI.Label(new Rect(30010010050), icon);
 
        // text와 icon을 동시에 표시.
        GUI.Box(new Rect(30015010050), new GUIContent("this is text", icon));
 
 
        // This line feeds "This is the tooltip" into GUI.tooltip
        GUI.Button(new Rect(30021010020), new GUIContent("Click me""This is the tooltip"));
        GUI.Label(new Rect(30024010020), GUI.tooltip);
 
 
        // 텍스트, 이미지, tooltip을 동시에.
        GUI.Button(new Rect(30020010020), new GUIContent("Click me", icon, "This is the tooltip"));
        GUI.Label(new Rect(30029010020), GUI.tooltip);
    }
}
 
 
cs





# Collider의 isTrigger 옵션은 충돌은 감지하지만 물리적 충돌은 발생하지 않도록 해준다.




: