UI要素をScriptから作成
Scriptでゲームオブジェクトを作成し、そのオブジェクトに必要なコンポーネントを追加することで、 新たにUI要素を作成します。しかし、この方法でUI要素を作成すると、デフォルトの画像を使用する際に問題が生じます。Editorで実行する場合に限れば以下のコードからデフォルトの画像を取得できます。
1 |
UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("ファイルパス"); |
当然、Build後には使用できません。そのため、public変数を用意した後にInspectorからデフォルトの画像を指定し、その変数を用いてUI要素に画像を設定する必要があります。以下に掲載されているScriptでは、Editorで実行する場合はUnityのリソースから、Build後は変数に入れられている画像を使用します。
Canvas
Canvasを作成するScriptです。
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 |
GameObject AddCanvas() { GameObject obj = new GameObject("Canvas"); obj.layer = LayerMask.NameToLayer("UI"); Canvas canvas = obj.AddComponent<Canvas>(); canvas.renderMode = RenderMode.ScreenSpaceOverlay; canvas.sortingOrder = 0; canvas.targetDisplay = 0; canvas.additionalShaderChannels = AdditionalCanvasShaderChannels.None; canvas.pixelPerfect = false; CanvasScaler canvas_scalar = obj.AddComponent<CanvasScaler>(); canvas_scalar.uiScaleMode = CanvasScaler.ScaleMode.ConstantPixelSize; canvas_scalar.scaleFactor = 1; canvas_scalar.referencePixelsPerUnit = 100; obj.AddComponent<GraphicRaycaster>(); RectTransform rect = obj.GetComponent<RectTransform>(); rect.anchorMin = Vector2.zero; rect.anchorMax = Vector2.one; rect.anchoredPosition = Vector2.zero; rect.sizeDelta = Vector2.zero; return obj; } |
Text
Textを作成するScriptです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
GameObject AddText(GameObject obj_parent, Vector2 pos, string text_name) { GameObject obj_text = new GameObject(text_name); obj_text.transform.SetParent(obj_parent.transform); obj_text.layer = LayerMask.NameToLayer("UI"); RectTransform rect_text = obj_text.AddComponent<RectTransform>(); rect_text.anchorMin = new Vector2(0.5f, 0.5f); rect_text.anchorMax = new Vector2(0.5f, 0.5f); rect_text.anchoredPosition = pos; rect_text.sizeDelta = new Vector2(160f, 30f); Text text = obj_text.AddComponent<Text>(); text.font = Resources.GetBuiltinResource<Font>("Arial.ttf"); text.text = "New Text"; float c = 50f / 255f; text.color = new Color(c, c, c, 1f); return obj_text; } |
Image
Imageを作成するScriptです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
GameObject AddImage(GameObject obj_parent, Vector2 pos, string image_name) { GameObject obj_image = new GameObject(image_name); obj_image.transform.SetParent(obj_parent.transform); obj_image.layer = LayerMask.NameToLayer("UI"); RectTransform rect_image = obj_image.AddComponent<RectTransform>(); rect_image.anchoredPosition = pos; rect_image.sizeDelta = new Vector2(100f, 100f); Image image = obj_image.AddComponent<Image>(); image.color = Color.white; return obj_image; } |
RawImage
RawImageを作成するScriptです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
GameObject AddRawImage(GameObject obj_parent, Vector2 pos, string image_name) { GameObject obj_image = new GameObject(image_name); obj_image.transform.SetParent(obj_parent.transform); obj_image.layer = LayerMask.NameToLayer("UI"); RectTransform rect_image = obj_image.AddComponent<RectTransform>(); rect_image.anchoredPosition = pos; rect_image.sizeDelta = new Vector2(100f, 100f); obj_image.AddComponent<RawImage>(); return obj_image; } |
Button
Buttonを作成するScriptです。
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 |
GameObject AddButton(GameObject obj_parent, Vector2 pos, string button_name) { GameObject obj_button = new GameObject(button_name); obj_button.transform.SetParent(obj_parent.transform); obj_button.layer = LayerMask.NameToLayer("UI"); RectTransform rect_button = obj_button.AddComponent<RectTransform>(); rect_button.anchoredPosition = pos; rect_button.sizeDelta = new Vector2(160f, 30f); Image image_button = obj_button.AddComponent<Image>(); image_button.color = Color.white; #if UNITY_EDITOR image_button.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/UISprite.psd"); #else image_button.sprite = sprite_UISprite; #endif image_button.type = Image.Type.Sliced; Button button = obj_button.AddComponent<Button>(); button.targetGraphic = image_button; //text GameObject obj_text = new GameObject("Text"); obj_text.transform.SetParent(obj_button.transform); RectTransform rect_text = obj_text.AddComponent<RectTransform>(); rect_text.anchorMin = Vector2.zero; rect_text.anchorMax = Vector2.one; rect_text.anchoredPosition = Vector3.zero; rect_text.sizeDelta = Vector2.zero; Text text_button = obj_text.AddComponent<Text>(); text_button.color = Color.black; text_button.font = Resources.GetBuiltinResource<Font>("Arial.ttf"); text_button.text = "Button"; text_button.alignment = TextAnchor.MiddleCenter; return obj_button; } |
Toggle
Toggleを作成するScriptです。
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 |
GameObject AddToggle(GameObject obj_parent, Vector2 pos, string toggle_name) { GameObject obj_toggle = new GameObject(toggle_name); obj_toggle.transform.SetParent(obj_parent.transform); obj_toggle.layer = LayerMask.NameToLayer("UI"); RectTransform rect_toggle = obj_toggle.AddComponent<RectTransform>(); rect_toggle.anchoredPosition = pos; rect_toggle.sizeDelta = new Vector3(160f, 20f); Toggle toggle = obj_toggle.AddComponent<Toggle>(); //background GameObject obj_bg = new GameObject("Background"); obj_bg.transform.SetParent(obj_toggle.transform); obj_bg.layer = LayerMask.NameToLayer("UI"); RectTransform rect_bg = obj_bg.AddComponent<RectTransform>(); rect_bg.anchorMin = new Vector2(0f, 1f); rect_bg.anchorMax = new Vector2(0f, 1f); rect_bg.anchoredPosition = new Vector2(10f, -10f); rect_bg.sizeDelta = new Vector2(20f, 20f); Image image_bg = obj_bg.AddComponent<Image>(); image_bg.type = Image.Type.Sliced; #if UNITY_EDITOR image_bg.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/UISprite.psd"); #else image_bg.sprite = sprite_UISprite; #endif //checkmark GameObject obj_mark = new GameObject("Checkmark"); obj_mark.transform.SetParent(obj_bg.transform); obj_mark.layer = LayerMask.NameToLayer("UI"); RectTransform rect_mark = obj_mark.AddComponent<RectTransform>(); rect_mark.anchorMin = new Vector2(0.5f, 0.5f); rect_mark.anchorMax = new Vector2(0.5f, 0.5f); rect_mark.anchoredPosition = Vector2.zero; rect_mark.sizeDelta = new Vector2(20f, 20f); Image image_mark = obj_mark.AddComponent<Image>(); #if UNITY_EDITOR image_mark.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/Checkmark.psd"); #else image_mark.sprite = sprite_checkmark; #endif //label GameObject obj_label = new GameObject("Label"); obj_label.transform.SetParent(obj_toggle.transform); RectTransform rect_label = obj_label.AddComponent<RectTransform>(); rect_label.anchorMin = Vector2.zero; rect_label.anchorMax = Vector2.one; rect_label.offsetMin = new Vector2(23f, 1f); rect_label.offsetMax = new Vector2(-5f, -2f); Text text_label = obj_label.AddComponent<Text>(); text_label.text = toggle_name; text_label.color = Color.black; text_label.font = Resources.GetBuiltinResource<Font>("Arial.ttf"); toggle.targetGraphic = image_bg; toggle.graphic = image_mark; return obj_toggle; } |
Slider
Sliderを作成するScriptです。
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 |
GameObject AddSlider(GameObject obj_parent, Vector2 pos, string slider_name) { GameObject obj_slider = new GameObject(slider_name); obj_slider.transform.SetParent(obj_parent.transform); obj_slider.layer = LayerMask.NameToLayer("UI"); RectTransform rect_slider = obj_slider.AddComponent<RectTransform>(); rect_slider.anchorMin = new Vector2(0.5f, 0.5f); rect_slider.anchorMax = new Vector2(0.5f, 0.5f); rect_slider.anchoredPosition = pos; rect_slider.sizeDelta = new Vector2(160f, 20f); Slider slider = obj_slider.AddComponent<Slider>(); //background GameObject obj_bg = new GameObject("Background"); obj_bg.transform.SetParent(obj_slider.transform); obj_bg.layer = LayerMask.NameToLayer("UI"); RectTransform rect_bg = obj_bg.AddComponent<RectTransform>(); rect_bg.anchorMin = new Vector2(0f, 0.25f); rect_bg.anchorMax = new Vector2(1f, 0.75f); rect_bg.anchoredPosition = Vector2.zero; rect_bg.sizeDelta = Vector2.zero; Image image_bg = obj_bg.AddComponent<Image>(); #if UNITY_EDITOR image_bg.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/Background.psd"); #else image_bg.sprite = sprite_background; #endif image_bg.type = Image.Type.Sliced; //fill area GameObject obj_fillarea = new GameObject("Fill Area"); obj_fillarea.transform.SetParent(obj_slider.transform); obj_fillarea.layer = LayerMask.NameToLayer("UI"); RectTransform rect_fillarea = obj_fillarea.AddComponent<RectTransform>(); rect_fillarea.anchorMin = new Vector2(0f, 0.25f); rect_fillarea.anchorMax = new Vector2(1f, 0.75f); rect_fillarea.anchoredPosition = new Vector3(-5f, 0f, 0f); rect_fillarea.sizeDelta = new Vector2(-20f, 0f); //fill GameObject obj_fill = new GameObject("Fill"); obj_fill.transform.SetParent(obj_fillarea.transform); obj_fill.layer = LayerMask.NameToLayer("UI"); RectTransform rect_fill = obj_fill.AddComponent<RectTransform>(); rect_fill.anchorMin = new Vector2(0f, 0f); rect_fill.anchorMax = new Vector2(0f, 1f); rect_fill.anchoredPosition = Vector2.zero; rect_fill.sizeDelta = new Vector2(10f, 0f); Image image_fill = obj_fill.AddComponent<Image>(); #if UNITY_EDITOR image_fill.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/UISprite.psd"); #else image_bg.sprite = sprite_UISprite; #endif image_fill.type = Image.Type.Sliced; //handle side area GameObject obj_hsarea = new GameObject("Handle Slide Area"); obj_hsarea.transform.SetParent(obj_slider.transform); obj_hsarea.layer = LayerMask.NameToLayer("UI"); RectTransform rect_hsarea = obj_hsarea.AddComponent<RectTransform>(); rect_hsarea.anchorMin = Vector2.zero; rect_hsarea.anchorMax = new Vector2(1f, 1f); rect_hsarea.anchoredPosition = Vector2.zero; rect_hsarea.sizeDelta = new Vector2(-20f, 0f); //handle GameObject obj_handle = new GameObject("Handle"); obj_handle.transform.SetParent(obj_hsarea.transform); obj_handle.layer = LayerMask.NameToLayer("UI"); RectTransform rect_handle = obj_handle.AddComponent<RectTransform>(); rect_handle.anchorMin = Vector2.zero; rect_handle.anchorMax = new Vector2(0f, 1f); rect_handle.anchoredPosition = Vector2.zero; rect_handle.sizeDelta = new Vector2(20f, 0f); Image image_handle = obj_handle.AddComponent<Image>(); #if UNITY_EDITOR image_handle.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/Knob.psd"); #else image_handle.sprite = sprite_knob; #endif slider.targetGraphic = image_handle; slider.fillRect = rect_fill; slider.handleRect = rect_handle; return obj_slider; } |
Scrollbar
Scrollbarを作成するScriptです。
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 |
GameObject AddScrollbar(GameObject obj_parent, Vector2 pos, string scrollbar_name) { //scrollbar GameObject obj_bar = new GameObject(scrollbar_name); obj_bar.transform.SetParent(obj_parent.transform); obj_bar.layer = LayerMask.NameToLayer("UI"); RectTransform rect_bar = obj_bar.AddComponent<RectTransform>(); rect_bar.anchoredPosition = pos; rect_bar.sizeDelta = new Vector3(160f, 20f); Image image_bar = obj_bar.AddComponent<Image>(); #if UNITY_EDITOR image_bar.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/Background.psd"); #else image_bar.sprite = sprite_background; #endif image_bar.type = Image.Type.Sliced; Scrollbar scrollbar = obj_bar.AddComponent<Scrollbar>(); //sliding area GameObject obj_sa = new GameObject("Sliding Area"); obj_sa.transform.SetParent(obj_bar.transform); obj_sa.layer = LayerMask.NameToLayer("UI"); RectTransform rect_sa = obj_sa.AddComponent<RectTransform>(); rect_sa.anchoredPosition = Vector2.zero; rect_sa.anchorMin = Vector2.zero; rect_sa.anchorMax = Vector2.one; rect_sa.sizeDelta = new Vector2(-20f, -20f); //handle GameObject obj_handle = new GameObject("Handle"); obj_handle.transform.SetParent(obj_sa.transform); obj_handle.layer = LayerMask.NameToLayer("UI"); RectTransform rect_handle = obj_handle.AddComponent<RectTransform>(); rect_handle.anchorMin = Vector2.zero; rect_handle.anchorMax = new Vector2(0.2f, 1f); rect_handle.anchoredPosition = Vector2.zero; rect_handle.sizeDelta = new Vector2(20f, 20f); Image image_handle = obj_handle.AddComponent<Image>(); #if UNITY_EDITOR image_handle.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/UISprite.psd"); #else image_handle.sprite = sprite_UISprite; #endif image_handle.type = Image.Type.Sliced; scrollbar.targetGraphic = image_handle; scrollbar.handleRect = rect_handle; return obj_bar; } |
Dropdown
Dropdownを作成するScriptです。Scrollbarを作成するScriptが必要となります。
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 211 212 213 |
GameObject AddDropdown(GameObject obj_parent, Vector2 pos, string drop_name) { GameObject obj_drop = new GameObject(drop_name); obj_drop.transform.SetParent(obj_parent.transform); obj_drop.layer = LayerMask.NameToLayer("UI"); obj_drop.transform.localPosition = pos; RectTransform rect_drop = obj_drop.AddComponent<RectTransform>(); rect_drop.sizeDelta = new Vector2(160f, 30f); Image image_drop = obj_drop.AddComponent<Image>(); #if UNITY_EDITOR image_drop.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/UISprite.psd"); #else image_drop.sprite = sprite_UISprite; #endif image_drop.type = Image.Type.Sliced; Dropdown dropdown = obj_drop.AddComponent<Dropdown>(); //label GameObject obj_label = new GameObject("Label"); obj_label.transform.SetParent(obj_drop.transform); obj_label.layer = LayerMask.NameToLayer("UI"); RectTransform rect_label = obj_label.AddComponent<RectTransform>(); rect_label.anchorMin = Vector2.zero; rect_label.anchorMax = Vector2.one; rect_label.anchoredPosition = new Vector2(-7.5f, -0.5f); rect_label.sizeDelta = new Vector2(-35f, -13f); Text text_label = obj_label.AddComponent<Text>(); text_label.color = Color.black; text_label.font = Resources.GetBuiltinResource<Font>("Arial.ttf"); text_label.text = "Option A"; text_label.alignment = TextAnchor.MiddleLeft; //arrow GameObject obj_arrow = new GameObject("Arrow"); obj_arrow.transform.SetParent(obj_drop.transform); obj_arrow.layer = LayerMask.NameToLayer("UI"); RectTransform rect_arrow = obj_arrow.AddComponent<RectTransform>(); rect_arrow.anchorMin = new Vector2(1f, 0.5f); rect_arrow.anchorMax = new Vector2(1f, 0.5f); rect_arrow.anchoredPosition = new Vector2(-15f, 0f); rect_arrow.sizeDelta = new Vector2(20f, 20f); Image image_arrow = obj_arrow.AddComponent<Image>(); #if UNITY_EDITOR image_arrow.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/DropdownArrow.psd"); #else image_arrow.sprite = sprite_dropDownArrow; #endif //template GameObject obj_temp = new GameObject("Template"); obj_temp.transform.SetParent(obj_drop.transform); obj_temp.layer = LayerMask.NameToLayer("UI"); RectTransform rect_temp = obj_temp.AddComponent<RectTransform>(); rect_temp.anchorMin = Vector2.zero; rect_temp.anchorMax = new Vector2(1f, 0f); rect_temp.anchoredPosition = new Vector2(0f, 2f); rect_temp.pivot = new Vector2(0.5f, 1f); rect_temp.sizeDelta = new Vector2(0f, 150f); Image image_temp = obj_temp.AddComponent<Image>(); #if UNITY_EDITOR image_temp.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/UISprite.psd"); #else image_temp.sprite = sprite_UISprite; #endif image_temp.type = Image.Type.Sliced; ScrollRect srect_tmp = obj_temp.AddComponent<ScrollRect>(); srect_tmp.horizontal = false; srect_tmp.movementType = ScrollRect.MovementType.Clamped; //view port GameObject obj_vp = new GameObject("Viewport"); obj_vp.transform.SetParent(obj_temp.transform); obj_vp.layer = LayerMask.NameToLayer("UI"); RectTransform rect_vp = obj_vp.AddComponent<RectTransform>(); rect_vp.anchorMin = Vector2.zero; rect_vp.anchorMax = Vector2.one; rect_vp.anchoredPosition = Vector2.zero; rect_vp.pivot = new Vector2(0f, 1f); rect_vp.sizeDelta = new Vector2(-18f, 0f); Mask mask_vp = obj_vp.AddComponent<Mask>(); mask_vp.showMaskGraphic = false; Image image_vp = obj_vp.AddComponent<Image>(); #if UNITY_EDITOR image_vp.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/UIMask.psd"); #else image_vp.sprite = sprite_UIMask; #endif image_vp.type = Image.Type.Sliced; //content GameObject obj_cont = new GameObject("Content"); obj_cont.transform.SetParent(obj_vp.transform); obj_cont.layer = LayerMask.NameToLayer("UI"); RectTransform rect_cont = obj_cont.AddComponent<RectTransform>(); rect_cont.anchorMin = new Vector2(0f, 1f); rect_cont.anchorMax = Vector2.one; rect_cont.anchoredPosition = Vector2.zero; rect_cont.pivot = new Vector2(0.5f, 1f); rect_cont.sizeDelta = new Vector2(0f, 28f); //item GameObject obj_item = new GameObject("Item"); obj_item.transform.SetParent(obj_cont.transform); obj_item.layer = LayerMask.NameToLayer("UI"); RectTransform rect_item = obj_item.AddComponent<RectTransform>(); rect_item.anchorMin = new Vector2(0f, 0.5f); rect_item.anchorMax = new Vector2(1f, 0.5f); rect_item.anchoredPosition = Vector2.zero; rect_item.sizeDelta = new Vector2(0f, 20f); Toggle toggle_item = obj_item.AddComponent<Toggle>(); //item background GameObject obj_bg = new GameObject("Item Background"); obj_bg.transform.SetParent(obj_item.transform); obj_bg.layer = LayerMask.NameToLayer("UI"); RectTransform rect_bg = obj_bg.AddComponent<RectTransform>(); rect_bg.anchorMin = Vector2.zero; rect_bg.anchorMax = Vector2.one; rect_bg.anchoredPosition = Vector2.zero; rect_bg.sizeDelta = Vector2.zero; Image image_bg = obj_bg.AddComponent<Image>(); //item check mark GameObject obj_check = new GameObject("Item Checkmark"); obj_check.transform.SetParent(obj_item.transform); obj_check.layer = LayerMask.NameToLayer("UI"); RectTransform rect_check = obj_check.AddComponent<RectTransform>(); rect_check.anchorMin = new Vector2(0f, 0.5f); rect_check.anchorMax = new Vector2(0f, 0.5f); rect_check.anchoredPosition = new Vector2(10f, 0f); rect_check.sizeDelta = new Vector2(20f, 20f); Image image_check = obj_check.AddComponent<Image>(); #if UNITY_EDITOR image_check.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/Checkmark.psd"); #else image_check.sprite = sprite_checkmark; #endif //item label GameObject obj_ilabel = new GameObject("Item Label"); obj_ilabel.transform.SetParent(obj_item.transform); obj_ilabel.layer = LayerMask.NameToLayer("UI"); RectTransform rect_ilabel = obj_ilabel.AddComponent<RectTransform>(); rect_ilabel.anchorMin = Vector2.zero; rect_ilabel.anchorMax = Vector2.one; rect_ilabel.anchoredPosition = new Vector2(5f, -0.5f); rect_ilabel.sizeDelta = new Vector2(-30f, -3f); Text text_ilabel = obj_ilabel.AddComponent<Text>(); float c = 50f / 255f; text_ilabel.color = new Color(c, c, c, 1f); text_ilabel.font = Resources.GetBuiltinResource<Font>("Arial.ttf"); text_ilabel.text = "Option A"; text_ilabel.alignment = TextAnchor.MiddleLeft; //scrollbar GameObject obj_bar = AddScrollbar(obj_temp, Vector2.zero, "Scrollbar"); RectTransform rect_bar = obj_bar.GetComponent<RectTransform>(); rect_bar.anchorMin = new Vector2(1f, 0f); rect_bar.anchorMax = Vector2.one; rect_bar.pivot = Vector2.one; rect_bar.anchoredPosition = Vector2.zero; rect_bar.sizeDelta = new Vector3(20f, 0f); //set toggle toggle_item.targetGraphic = image_bg; toggle_item.graphic = image_check; //set scroll rect srect_tmp.content = rect_cont; srect_tmp.viewport = rect_vp; srect_tmp.verticalScrollbar = obj_bar.GetComponent<Scrollbar>(); srect_tmp.verticalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHideAndExpandViewport; srect_tmp.verticalScrollbarSpacing = -3; //set drop down dropdown.targetGraphic = image_drop; dropdown.template = rect_temp; dropdown.captionText = text_label; dropdown.itemText = text_ilabel; List<string> list = new List<string>(); list.Add("Option A"); list.Add("Option B"); list.Add("Option C"); dropdown.AddOptions(list); dropdown.RefreshShownValue(); obj_temp.SetActive(false); return obj_drop; } |
InputField
InputFieldを作成するScriptです。
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 |
GameObject AddInputField(GameObject obj_parent, Vector2 pos, string field_name) { GameObject obj_field = new GameObject(field_name); obj_field.transform.SetParent(obj_parent.transform); obj_field.layer = LayerMask.NameToLayer("UI"); RectTransform rect_field = obj_field.AddComponent<RectTransform>(); rect_field.anchoredPosition = pos; rect_field.sizeDelta = new Vector2(160f, 30f); Image image_field = obj_field.AddComponent<Image>(); #if UNITY_EDITOR image_field.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/InputFieldBackground.psd"); #else image_field.sprite = sprite_inputFieldBackground; #endif image_field.type = Image.Type.Sliced; InputField inputfield = obj_field.AddComponent<InputField>(); inputfield.targetGraphic = image_field; //place holder GameObject obj_ph = new GameObject("Placeholder"); obj_ph.transform.SetParent(obj_field.transform); obj_ph.layer = LayerMask.NameToLayer("UI"); RectTransform rect_ph = obj_ph.AddComponent<RectTransform>(); rect_ph.anchorMin = Vector2.zero; rect_ph.anchorMax = Vector2.one; rect_ph.anchoredPosition = new Vector2(0f, -0.5f); rect_ph.sizeDelta = new Vector2(-20f, -13f); Text text_ph = obj_ph.AddComponent<Text>(); text_ph.text = "Enter text..."; text_ph.font = Resources.GetBuiltinResource<Font>("Arial.ttf"); text_ph.fontStyle = FontStyle.Italic; float c = 50f / 255f; text_ph.color = new Color(c, c, c, 0.5f); //text GameObject obj_text = new GameObject("Text"); obj_text.transform.SetParent(obj_field.transform); obj_text.layer = LayerMask.NameToLayer("UI"); RectTransform rect_text = obj_text.AddComponent<RectTransform>(); rect_text.anchorMin = Vector2.zero; rect_text.anchorMax = Vector2.one; rect_text.anchoredPosition = new Vector2(0f, -0.5f); rect_text.sizeDelta = new Vector2(-20f, -13f); Text text_text = obj_text.AddComponent<Text>(); text_text.font = Resources.GetBuiltinResource<Font>("Arial.ttf"); text_text.supportRichText = false; text_text.color = new Color(c, c, c, 1f); inputfield.textComponent = text_text; inputfield.placeholder = text_ph; return obj_field; } |
Panel
Panelを作成するScriptです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
GameObject AddPanel(GameObject obj_parent, string panel_name) { GameObject obj_panel = new GameObject(panel_name); obj_panel.transform.SetParent(obj_parent.transform); obj_panel.layer = LayerMask.NameToLayer("UI"); RectTransform rect_panel = obj_panel.AddComponent<RectTransform>(); rect_panel.anchorMin = Vector2.zero; rect_panel.anchorMax = Vector2.one; rect_panel.anchoredPosition = Vector2.zero; rect_panel.sizeDelta = Vector2.zero; Image image_panel = obj_panel.AddComponent<Image>(); #if UNITY_EDITOR image_panel.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/Background.psd"); #else image_panel.sprite = sprite_background; #endif image_panel.type = Image.Type.Sliced; image_panel.color = new Color(1f, 1f, 1f, 0.5f); return obj_panel; } |
Scroll View
Scroll Viewを作成するScriptです 。Scrollbarを作成するScriptが必要となります。
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 |
GameObject AddScrollView(GameObject obj_parent, Vector2 pos, string view_name) { GameObject obj_sv = new GameObject(view_name); obj_sv.transform.SetParent(obj_parent.transform); obj_sv.layer = LayerMask.NameToLayer("UI"); RectTransform rect_sv = obj_sv.AddComponent<RectTransform>(); rect_sv.anchoredPosition = pos; rect_sv.sizeDelta = new Vector2(200f, 200f); ScrollRect scrollRect = obj_sv.AddComponent<ScrollRect>(); Image image_sv = obj_sv.AddComponent<Image>(); #if UNITY_EDITOR image_sv.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/Background.psd"); #else image_sv.sprite = sprite_background; #endif image_sv.type = Image.Type.Sliced; image_sv.color = new Color(1f, 1f, 1f, 0.5f); //view port GameObject obj_view = new GameObject("Viewport"); obj_view.transform.SetParent(obj_sv.transform); obj_view.layer = LayerMask.NameToLayer("UI"); RectTransform rect_view = obj_view.AddComponent<RectTransform>(); rect_view.anchorMin = Vector2.zero; rect_view.anchorMax = Vector2.one; rect_view.pivot = new Vector2(0f, 1f); rect_view.anchoredPosition = Vector2.zero; rect_view.sizeDelta = new Vector2(17f, 17f); Mask mask_view = obj_view.AddComponent<Mask>(); mask_view.showMaskGraphic = false; Image image_view = obj_view.AddComponent<Image>(); #if UNITY_EDITOR image_view.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/UIMask.psd"); #else image_view.sprite = sprite_UIMask; #endif image_view.type = Image.Type.Sliced; //content GameObject obj_content = new GameObject("Content"); obj_content.transform.SetParent(obj_view.transform); obj_content.layer = LayerMask.NameToLayer("UI"); RectTransform rect_content = obj_content.AddComponent<RectTransform>(); rect_content.anchorMin = new Vector2(0f, 1f); rect_content.anchorMax = Vector2.one; rect_content.pivot = new Vector2(0f, 1f); rect_content.anchoredPosition = new Vector2(0f, 117f); rect_content.sizeDelta = new Vector2(0f, 300f); //scroll bar horizontal GameObject obj_horizon = AddScrollbar(obj_sv, Vector2.zero, "Scrollbar Horizontal"); RectTransform rect_horizon = obj_horizon.GetComponent<RectTransform>(); rect_horizon.anchorMin = Vector2.zero; rect_horizon.anchorMax = new Vector2(1f, 0f); rect_horizon.pivot = Vector2.zero; rect_horizon.anchoredPosition = Vector2.zero; rect_horizon.sizeDelta = new Vector2(17f, 20f); //scroll bar vertical GameObject obj_vertical = AddScrollbar(obj_sv, Vector2.zero, "Scrollbar Vertical"); RectTransform rect_vertical = obj_vertical.GetComponent<RectTransform>(); rect_vertical.anchorMin = new Vector2(1f, 0f); rect_vertical.anchorMax = Vector2.one; rect_vertical.pivot = Vector2.one; rect_vertical.anchoredPosition = Vector2.zero; rect_vertical.sizeDelta = new Vector2(20f, 17f); scrollRect.content = rect_content; scrollRect.viewport = rect_view; scrollRect.horizontalScrollbar = obj_horizon.GetComponent<Scrollbar>(); scrollRect.horizontalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHideAndExpandViewport; scrollRect.horizontalScrollbarSpacing = -3; scrollRect.verticalScrollbar = obj_vertical.GetComponent<Scrollbar>(); scrollRect.verticalScrollbar.direction = Scrollbar.Direction.BottomToTop; scrollRect.verticalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHideAndExpandViewport; scrollRect.verticalScrollbarSpacing = -3; return obj_sv; } |
全てまとめたScript
以上のコードをまとめたScriptです。適当なゲームオブジェクトに以下のScriptをアタッチし、実行すればUI要素が作成されます。Inspectorでそれぞれの変数に画像を設定すれば、Build後も動作します。
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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 |
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class CreateUIElements : MonoBehaviour { public Sprite sprite_background, sprite_UISprite, sprite_checkmark, sprite_dropDownArrow, sprite_UIMask, sprite_inputFieldBackground, sprite_knob; // Use this for initialization void Start() { GameObject objCanvas = AddCanvas(); AddText(objCanvas, Vector2.zero, "Text"); AddImage(objCanvas, Vector2.zero, "Image"); AddRawImage(objCanvas, Vector2.zero, "RawImage"); AddButton(objCanvas, Vector2.zero, "Button"); AddToggle(objCanvas, Vector2.zero, "Toggle"); AddSlider(objCanvas, Vector2.zero, "Slider"); AddScrollbar(objCanvas, Vector2.zero, "Scrollbar"); AddDropdown(objCanvas, Vector2.zero, "Dropdown"); AddInputField(objCanvas, Vector2.zero, "InputField"); AddPanel(objCanvas, "Panel"); AddScrollView(objCanvas, Vector2.zero, "Scroll View"); } //Canvas GameObject AddCanvas() { GameObject obj = new GameObject("Canvas"); obj.layer = LayerMask.NameToLayer("UI"); Canvas canvas = obj.AddComponent<Canvas>(); canvas.renderMode = RenderMode.ScreenSpaceOverlay; canvas.sortingOrder = 0; canvas.targetDisplay = 0; canvas.additionalShaderChannels = AdditionalCanvasShaderChannels.None; canvas.pixelPerfect = false; CanvasScaler canvas_scalar = obj.AddComponent<CanvasScaler>(); canvas_scalar.uiScaleMode = CanvasScaler.ScaleMode.ConstantPixelSize; canvas_scalar.scaleFactor = 1; canvas_scalar.referencePixelsPerUnit = 100; obj.AddComponent<GraphicRaycaster>(); RectTransform rect = obj.GetComponent<RectTransform>(); rect.anchorMin = Vector2.zero; rect.anchorMax = Vector2.one; rect.anchoredPosition = Vector2.zero; rect.sizeDelta = Vector2.zero; return obj; } //Text GameObject AddText(GameObject obj_parent, Vector2 pos, string text_name) { GameObject obj_text = new GameObject(text_name); obj_text.transform.SetParent(obj_parent.transform); obj_text.layer = LayerMask.NameToLayer("UI"); RectTransform rect_text = obj_text.AddComponent<RectTransform>(); rect_text.anchorMin = new Vector2(0.5f, 0.5f); rect_text.anchorMax = new Vector2(0.5f, 0.5f); rect_text.anchoredPosition = pos; rect_text.sizeDelta = new Vector2(160f, 30f); Text text = obj_text.AddComponent<Text>(); text.font = Resources.GetBuiltinResource<Font>("Arial.ttf"); text.text = "New Text"; float c = 50f / 255f; text.color = new Color(c, c, c, 1f); return obj_text; } //Image GameObject AddImage(GameObject obj_parent, Vector2 pos, string image_name) { GameObject obj_image = new GameObject(image_name); obj_image.transform.SetParent(obj_parent.transform); obj_image.layer = LayerMask.NameToLayer("UI"); RectTransform rect_image = obj_image.AddComponent<RectTransform>(); rect_image.anchoredPosition = pos; rect_image.sizeDelta = new Vector2(100f, 100f); Image image = obj_image.AddComponent<Image>(); image.color = Color.white; return obj_image; } //RawImage GameObject AddRawImage(GameObject obj_parent, Vector2 pos, string image_name) { GameObject obj_image = new GameObject(image_name); obj_image.transform.SetParent(obj_parent.transform); obj_image.layer = LayerMask.NameToLayer("UI"); RectTransform rect_image = obj_image.AddComponent<RectTransform>(); rect_image.anchoredPosition = pos; rect_image.sizeDelta = new Vector2(100f, 100f); obj_image.AddComponent<RawImage>(); return obj_image; } //Button GameObject AddButton(GameObject obj_parent, Vector2 pos, string button_name) { GameObject obj_button = new GameObject(button_name); obj_button.transform.SetParent(obj_parent.transform); obj_button.layer = LayerMask.NameToLayer("UI"); RectTransform rect_button = obj_button.AddComponent<RectTransform>(); rect_button.anchoredPosition = pos; rect_button.sizeDelta = new Vector2(160f, 30f); Image image_button = obj_button.AddComponent<Image>(); image_button.color = Color.white; #if UNITY_EDITOR image_button.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/UISprite.psd"); #else image_button.sprite = sprite_UISprite; #endif image_button.type = Image.Type.Sliced; Button button = obj_button.AddComponent<Button>(); button.targetGraphic = image_button; //text GameObject obj_text = new GameObject("Text"); obj_text.transform.SetParent(obj_button.transform); RectTransform rect_text = obj_text.AddComponent<RectTransform>(); rect_text.anchorMin = Vector2.zero; rect_text.anchorMax = Vector2.one; rect_text.anchoredPosition = Vector3.zero; rect_text.sizeDelta = Vector2.zero; Text text_button = obj_text.AddComponent<Text>(); text_button.color = Color.black; text_button.font = Resources.GetBuiltinResource<Font>("Arial.ttf"); text_button.text = "Button"; text_button.alignment = TextAnchor.MiddleCenter; return obj_button; } //Toggle GameObject AddToggle(GameObject obj_parent, Vector2 pos, string toggle_name) { GameObject obj_toggle = new GameObject(toggle_name); obj_toggle.transform.SetParent(obj_parent.transform); obj_toggle.layer = LayerMask.NameToLayer("UI"); RectTransform rect_toggle = obj_toggle.AddComponent<RectTransform>(); rect_toggle.anchoredPosition = pos; rect_toggle.sizeDelta = new Vector3(160f, 20f); Toggle toggle = obj_toggle.AddComponent<Toggle>(); //background GameObject obj_bg = new GameObject("Background"); obj_bg.transform.SetParent(obj_toggle.transform); obj_bg.layer = LayerMask.NameToLayer("UI"); RectTransform rect_bg = obj_bg.AddComponent<RectTransform>(); rect_bg.anchorMin = new Vector2(0f, 1f); rect_bg.anchorMax = new Vector2(0f, 1f); rect_bg.anchoredPosition = new Vector2(10f, -10f); rect_bg.sizeDelta = new Vector2(20f, 20f); Image image_bg = obj_bg.AddComponent<Image>(); image_bg.type = Image.Type.Sliced; #if UNITY_EDITOR image_bg.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/UISprite.psd"); #else image_bg.sprite = sprite_UISprite; #endif //checkmark GameObject obj_mark = new GameObject("Checkmark"); obj_mark.transform.SetParent(obj_bg.transform); obj_mark.layer = LayerMask.NameToLayer("UI"); RectTransform rect_mark = obj_mark.AddComponent<RectTransform>(); rect_mark.anchorMin = new Vector2(0.5f, 0.5f); rect_mark.anchorMax = new Vector2(0.5f, 0.5f); rect_mark.anchoredPosition = Vector2.zero; rect_mark.sizeDelta = new Vector2(20f, 20f); Image image_mark = obj_mark.AddComponent<Image>(); #if UNITY_EDITOR image_mark.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/Checkmark.psd"); #else image_mark.sprite = sprite_checkmark; #endif //label GameObject obj_label = new GameObject("Label"); obj_label.transform.SetParent(obj_toggle.transform); RectTransform rect_label = obj_label.AddComponent<RectTransform>(); rect_label.anchorMin = Vector2.zero; rect_label.anchorMax = Vector2.one; rect_label.offsetMin = new Vector2(23f, 1f); rect_label.offsetMax = new Vector2(-5f, -2f); Text text_label = obj_label.AddComponent<Text>(); text_label.text = toggle_name; text_label.color = Color.black; text_label.font = Resources.GetBuiltinResource<Font>("Arial.ttf"); toggle.targetGraphic = image_bg; toggle.graphic = image_mark; return obj_toggle; } //Slider GameObject AddSlider(GameObject obj_parent, Vector2 pos, string slider_name) { GameObject obj_slider = new GameObject(slider_name); obj_slider.transform.SetParent(obj_parent.transform); obj_slider.layer = LayerMask.NameToLayer("UI"); RectTransform rect_slider = obj_slider.AddComponent<RectTransform>(); rect_slider.anchorMin = new Vector2(0.5f, 0.5f); rect_slider.anchorMax = new Vector2(0.5f, 0.5f); rect_slider.anchoredPosition = pos; rect_slider.sizeDelta = new Vector2(160f, 20f); Slider slider = obj_slider.AddComponent<Slider>(); //background GameObject obj_bg = new GameObject("Background"); obj_bg.transform.SetParent(obj_slider.transform); obj_bg.layer = LayerMask.NameToLayer("UI"); RectTransform rect_bg = obj_bg.AddComponent<RectTransform>(); rect_bg.anchorMin = new Vector2(0f, 0.25f); rect_bg.anchorMax = new Vector2(1f, 0.75f); rect_bg.anchoredPosition = Vector2.zero; rect_bg.sizeDelta = Vector2.zero; Image image_bg = obj_bg.AddComponent<Image>(); #if UNITY_EDITOR image_bg.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/Background.psd"); #else image_bg.sprite = sprite_background; #endif image_bg.type = Image.Type.Sliced; //fill area GameObject obj_fillarea = new GameObject("Fill Area"); obj_fillarea.transform.SetParent(obj_slider.transform); obj_fillarea.layer = LayerMask.NameToLayer("UI"); RectTransform rect_fillarea = obj_fillarea.AddComponent<RectTransform>(); rect_fillarea.anchorMin = new Vector2(0f, 0.25f); rect_fillarea.anchorMax = new Vector2(1f, 0.75f); rect_fillarea.anchoredPosition = new Vector3(-5f, 0f, 0f); rect_fillarea.sizeDelta = new Vector2(-20f, 0f); //fill GameObject obj_fill = new GameObject("Fill"); obj_fill.transform.SetParent(obj_fillarea.transform); obj_fill.layer = LayerMask.NameToLayer("UI"); RectTransform rect_fill = obj_fill.AddComponent<RectTransform>(); rect_fill.anchorMin = new Vector2(0f, 0f); rect_fill.anchorMax = new Vector2(0f, 1f); rect_fill.anchoredPosition = Vector2.zero; rect_fill.sizeDelta = new Vector2(10f, 0f); Image image_fill = obj_fill.AddComponent<Image>(); #if UNITY_EDITOR image_fill.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/UISprite.psd"); #else image_bg.sprite = sprite_UISprite; #endif image_fill.type = Image.Type.Sliced; //handle side area GameObject obj_hsarea = new GameObject("Handle Slide Area"); obj_hsarea.transform.SetParent(obj_slider.transform); obj_hsarea.layer = LayerMask.NameToLayer("UI"); RectTransform rect_hsarea = obj_hsarea.AddComponent<RectTransform>(); rect_hsarea.anchorMin = Vector2.zero; rect_hsarea.anchorMax = new Vector2(1f, 1f); rect_hsarea.anchoredPosition = Vector2.zero; rect_hsarea.sizeDelta = new Vector2(-20f, 0f); //handle GameObject obj_handle = new GameObject("Handle"); obj_handle.transform.SetParent(obj_hsarea.transform); obj_handle.layer = LayerMask.NameToLayer("UI"); RectTransform rect_handle = obj_handle.AddComponent<RectTransform>(); rect_handle.anchorMin = Vector2.zero; rect_handle.anchorMax = new Vector2(0f, 1f); rect_handle.anchoredPosition = Vector2.zero; rect_handle.sizeDelta = new Vector2(20f, 0f); Image image_handle = obj_handle.AddComponent<Image>(); #if UNITY_EDITOR image_handle.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/Knob.psd"); #else image_handle.sprite = sprite_knob; #endif slider.targetGraphic = image_handle; slider.fillRect = rect_fill; slider.handleRect = rect_handle; return obj_slider; } //Scrollbar GameObject AddScrollbar(GameObject obj_parent, Vector2 pos, string scrollbar_name) { //scrollbar GameObject obj_bar = new GameObject(scrollbar_name); obj_bar.transform.SetParent(obj_parent.transform); obj_bar.layer = LayerMask.NameToLayer("UI"); RectTransform rect_bar = obj_bar.AddComponent<RectTransform>(); rect_bar.anchoredPosition = pos; rect_bar.sizeDelta = new Vector3(160f, 20f); Image image_bar = obj_bar.AddComponent<Image>(); #if UNITY_EDITOR image_bar.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/Background.psd"); #else image_bar.sprite = sprite_background; #endif image_bar.type = Image.Type.Sliced; Scrollbar scrollbar = obj_bar.AddComponent<Scrollbar>(); //sliding area GameObject obj_sa = new GameObject("Sliding Area"); obj_sa.transform.SetParent(obj_bar.transform); obj_sa.layer = LayerMask.NameToLayer("UI"); RectTransform rect_sa = obj_sa.AddComponent<RectTransform>(); rect_sa.anchoredPosition = Vector2.zero; rect_sa.anchorMin = Vector2.zero; rect_sa.anchorMax = Vector2.one; rect_sa.sizeDelta = new Vector2(-20f, -20f); //handle GameObject obj_handle = new GameObject("Handle"); obj_handle.transform.SetParent(obj_sa.transform); obj_handle.layer = LayerMask.NameToLayer("UI"); RectTransform rect_handle = obj_handle.AddComponent<RectTransform>(); rect_handle.anchorMin = Vector2.zero; rect_handle.anchorMax = new Vector2(0.2f, 1f); rect_handle.anchoredPosition = Vector2.zero; rect_handle.sizeDelta = new Vector2(20f, 20f); Image image_handle = obj_handle.AddComponent<Image>(); #if UNITY_EDITOR image_handle.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/UISprite.psd"); #else image_handle.sprite = sprite_UISprite; #endif image_handle.type = Image.Type.Sliced; scrollbar.targetGraphic = image_handle; scrollbar.handleRect = rect_handle; return obj_bar; } //Dropdown GameObject AddDropdown(GameObject obj_parent, Vector2 pos, string drop_name) { GameObject obj_drop = new GameObject(drop_name); obj_drop.transform.SetParent(obj_parent.transform); obj_drop.layer = LayerMask.NameToLayer("UI"); obj_drop.transform.localPosition = pos; RectTransform rect_drop = obj_drop.AddComponent<RectTransform>(); rect_drop.sizeDelta = new Vector2(160f, 30f); Image image_drop = obj_drop.AddComponent<Image>(); #if UNITY_EDITOR image_drop.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/UISprite.psd"); #else image_drop.sprite = sprite_UISprite; #endif image_drop.type = Image.Type.Sliced; Dropdown dropdown = obj_drop.AddComponent<Dropdown>(); //label GameObject obj_label = new GameObject("Label"); obj_label.transform.SetParent(obj_drop.transform); obj_label.layer = LayerMask.NameToLayer("UI"); RectTransform rect_label = obj_label.AddComponent<RectTransform>(); rect_label.anchorMin = Vector2.zero; rect_label.anchorMax = Vector2.one; rect_label.anchoredPosition = new Vector2(-7.5f, -0.5f); rect_label.sizeDelta = new Vector2(-35f, -13f); Text text_label = obj_label.AddComponent<Text>(); text_label.color = Color.black; text_label.font = Resources.GetBuiltinResource<Font>("Arial.ttf"); text_label.text = "Option A"; text_label.alignment = TextAnchor.MiddleLeft; //arrow GameObject obj_arrow = new GameObject("Arrow"); obj_arrow.transform.SetParent(obj_drop.transform); obj_arrow.layer = LayerMask.NameToLayer("UI"); RectTransform rect_arrow = obj_arrow.AddComponent<RectTransform>(); rect_arrow.anchorMin = new Vector2(1f, 0.5f); rect_arrow.anchorMax = new Vector2(1f, 0.5f); rect_arrow.anchoredPosition = new Vector2(-15f, 0f); rect_arrow.sizeDelta = new Vector2(20f, 20f); Image image_arrow = obj_arrow.AddComponent<Image>(); #if UNITY_EDITOR image_arrow.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/DropdownArrow.psd"); #else image_arrow.sprite = sprite_dropDownArrow; #endif //template GameObject obj_temp = new GameObject("Template"); obj_temp.transform.SetParent(obj_drop.transform); obj_temp.layer = LayerMask.NameToLayer("UI"); RectTransform rect_temp = obj_temp.AddComponent<RectTransform>(); rect_temp.anchorMin = Vector2.zero; rect_temp.anchorMax = new Vector2(1f, 0f); rect_temp.anchoredPosition = new Vector2(0f, 2f); rect_temp.pivot = new Vector2(0.5f, 1f); rect_temp.sizeDelta = new Vector2(0f, 150f); Image image_temp = obj_temp.AddComponent<Image>(); #if UNITY_EDITOR image_temp.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/UISprite.psd"); #else image_temp.sprite = sprite_UISprite; #endif image_temp.type = Image.Type.Sliced; ScrollRect srect_tmp = obj_temp.AddComponent<ScrollRect>(); srect_tmp.horizontal = false; srect_tmp.movementType = ScrollRect.MovementType.Clamped; //view port GameObject obj_vp = new GameObject("Viewport"); obj_vp.transform.SetParent(obj_temp.transform); obj_vp.layer = LayerMask.NameToLayer("UI"); RectTransform rect_vp = obj_vp.AddComponent<RectTransform>(); rect_vp.anchorMin = Vector2.zero; rect_vp.anchorMax = Vector2.one; rect_vp.anchoredPosition = Vector2.zero; rect_vp.pivot = new Vector2(0f, 1f); rect_vp.sizeDelta = new Vector2(-18f, 0f); Mask mask_vp = obj_vp.AddComponent<Mask>(); mask_vp.showMaskGraphic = false; Image image_vp = obj_vp.AddComponent<Image>(); #if UNITY_EDITOR image_vp.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/UIMask.psd"); #else image_vp.sprite = sprite_UIMask; #endif image_vp.type = Image.Type.Sliced; //content GameObject obj_cont = new GameObject("Content"); obj_cont.transform.SetParent(obj_vp.transform); obj_cont.layer = LayerMask.NameToLayer("UI"); RectTransform rect_cont = obj_cont.AddComponent<RectTransform>(); rect_cont.anchorMin = new Vector2(0f, 1f); rect_cont.anchorMax = Vector2.one; rect_cont.anchoredPosition = Vector2.zero; rect_cont.pivot = new Vector2(0.5f, 1f); rect_cont.sizeDelta = new Vector2(0f, 28f); //item GameObject obj_item = new GameObject("Item"); obj_item.transform.SetParent(obj_cont.transform); obj_item.layer = LayerMask.NameToLayer("UI"); RectTransform rect_item = obj_item.AddComponent<RectTransform>(); rect_item.anchorMin = new Vector2(0f, 0.5f); rect_item.anchorMax = new Vector2(1f, 0.5f); rect_item.anchoredPosition = Vector2.zero; rect_item.sizeDelta = new Vector2(0f, 20f); Toggle toggle_item = obj_item.AddComponent<Toggle>(); //item background GameObject obj_bg = new GameObject("Item Background"); obj_bg.transform.SetParent(obj_item.transform); obj_bg.layer = LayerMask.NameToLayer("UI"); RectTransform rect_bg = obj_bg.AddComponent<RectTransform>(); rect_bg.anchorMin = Vector2.zero; rect_bg.anchorMax = Vector2.one; rect_bg.anchoredPosition = Vector2.zero; rect_bg.sizeDelta = Vector2.zero; Image image_bg = obj_bg.AddComponent<Image>(); //item check mark GameObject obj_check = new GameObject("Item Checkmark"); obj_check.transform.SetParent(obj_item.transform); obj_check.layer = LayerMask.NameToLayer("UI"); RectTransform rect_check = obj_check.AddComponent<RectTransform>(); rect_check.anchorMin = new Vector2(0f, 0.5f); rect_check.anchorMax = new Vector2(0f, 0.5f); rect_check.anchoredPosition = new Vector2(10f, 0f); rect_check.sizeDelta = new Vector2(20f, 20f); Image image_check = obj_check.AddComponent<Image>(); #if UNITY_EDITOR image_check.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/Checkmark.psd"); #else image_check.sprite = sprite_checkmark; #endif //item label GameObject obj_ilabel = new GameObject("Item Label"); obj_ilabel.transform.SetParent(obj_item.transform); obj_ilabel.layer = LayerMask.NameToLayer("UI"); RectTransform rect_ilabel = obj_ilabel.AddComponent<RectTransform>(); rect_ilabel.anchorMin = Vector2.zero; rect_ilabel.anchorMax = Vector2.one; rect_ilabel.anchoredPosition = new Vector2(5f, -0.5f); rect_ilabel.sizeDelta = new Vector2(-30f, -3f); Text text_ilabel = obj_ilabel.AddComponent<Text>(); float c = 50f / 255f; text_ilabel.color = new Color(c, c, c, 1f); text_ilabel.font = Resources.GetBuiltinResource<Font>("Arial.ttf"); text_ilabel.text = "Option A"; text_ilabel.alignment = TextAnchor.MiddleLeft; //scrollbar GameObject obj_bar = AddScrollbar(obj_temp, Vector2.zero, "Scrollbar"); RectTransform rect_bar = obj_bar.GetComponent<RectTransform>(); rect_bar.anchorMin = new Vector2(1f, 0f); rect_bar.anchorMax = Vector2.one; rect_bar.pivot = Vector2.one; rect_bar.anchoredPosition = Vector2.zero; rect_bar.sizeDelta = new Vector3(20f, 0f); //set toggle toggle_item.targetGraphic = image_bg; toggle_item.graphic = image_check; //set scroll rect srect_tmp.content = rect_cont; srect_tmp.viewport = rect_vp; srect_tmp.verticalScrollbar = obj_bar.GetComponent<Scrollbar>(); srect_tmp.verticalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHideAndExpandViewport; srect_tmp.verticalScrollbarSpacing = -3; //set drop down dropdown.targetGraphic = image_drop; dropdown.template = rect_temp; dropdown.captionText = text_label; dropdown.itemText = text_ilabel; List<string> list = new List<string>(); list.Add("Option A"); list.Add("Option B"); list.Add("Option C"); dropdown.AddOptions(list); dropdown.RefreshShownValue(); obj_temp.SetActive(false); return obj_drop; } //InputField GameObject AddInputField(GameObject obj_parent, Vector2 pos, string field_name) { GameObject obj_field = new GameObject(field_name); obj_field.transform.SetParent(obj_parent.transform); obj_field.layer = LayerMask.NameToLayer("UI"); RectTransform rect_field = obj_field.AddComponent<RectTransform>(); rect_field.anchoredPosition = pos; rect_field.sizeDelta = new Vector2(160f, 30f); Image image_field = obj_field.AddComponent<Image>(); #if UNITY_EDITOR image_field.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/InputFieldBackground.psd"); #else image_field.sprite = sprite_inputFieldBackground; #endif image_field.type = Image.Type.Sliced; InputField inputfield = obj_field.AddComponent<InputField>(); inputfield.targetGraphic = image_field; //place holder GameObject obj_ph = new GameObject("Placeholder"); obj_ph.transform.SetParent(obj_field.transform); obj_ph.layer = LayerMask.NameToLayer("UI"); RectTransform rect_ph = obj_ph.AddComponent<RectTransform>(); rect_ph.anchorMin = Vector2.zero; rect_ph.anchorMax = Vector2.one; rect_ph.anchoredPosition = new Vector2(0f, -0.5f); rect_ph.sizeDelta = new Vector2(-20f, -13f); Text text_ph = obj_ph.AddComponent<Text>(); text_ph.text = "Enter text..."; text_ph.font = Resources.GetBuiltinResource<Font>("Arial.ttf"); text_ph.fontStyle = FontStyle.Italic; float c = 50f / 255f; text_ph.color = new Color(c, c, c, 0.5f); //text GameObject obj_text = new GameObject("Text"); obj_text.transform.SetParent(obj_field.transform); obj_text.layer = LayerMask.NameToLayer("UI"); RectTransform rect_text = obj_text.AddComponent<RectTransform>(); rect_text.anchorMin = Vector2.zero; rect_text.anchorMax = Vector2.one; rect_text.anchoredPosition = new Vector2(0f, -0.5f); rect_text.sizeDelta = new Vector2(-20f, -13f); Text text_text = obj_text.AddComponent<Text>(); text_text.font = Resources.GetBuiltinResource<Font>("Arial.ttf"); text_text.supportRichText = false; text_text.color = new Color(c, c, c, 1f); inputfield.textComponent = text_text; inputfield.placeholder = text_ph; return obj_field; } //Panel GameObject AddPanel(GameObject obj_parent, string panel_name) { GameObject obj_panel = new GameObject(panel_name); obj_panel.transform.SetParent(obj_parent.transform); obj_panel.layer = LayerMask.NameToLayer("UI"); RectTransform rect_panel = obj_panel.AddComponent<RectTransform>(); rect_panel.anchorMin = Vector2.zero; rect_panel.anchorMax = Vector2.one; rect_panel.anchoredPosition = Vector2.zero; rect_panel.sizeDelta = Vector2.zero; Image image_panel = obj_panel.AddComponent<Image>(); #if UNITY_EDITOR image_panel.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/Background.psd"); #else image_panel.sprite = sprite_background; #endif image_panel.type = Image.Type.Sliced; image_panel.color = new Color(1f, 1f, 1f, 0.5f); return obj_panel; } //ScrollView GameObject AddScrollView(GameObject obj_parent, Vector2 pos, string view_name) { GameObject obj_sv = new GameObject(view_name); obj_sv.transform.SetParent(obj_parent.transform); obj_sv.layer = LayerMask.NameToLayer("UI"); RectTransform rect_sv = obj_sv.AddComponent<RectTransform>(); rect_sv.anchoredPosition = pos; rect_sv.sizeDelta = new Vector2(200f, 200f); ScrollRect scrollRect = obj_sv.AddComponent<ScrollRect>(); Image image_sv = obj_sv.AddComponent<Image>(); #if UNITY_EDITOR image_sv.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/Background.psd"); #else image_sv.sprite = sprite_background; #endif image_sv.type = Image.Type.Sliced; image_sv.color = new Color(1f, 1f, 1f, 0.5f); //view port GameObject obj_view = new GameObject("Viewport"); obj_view.transform.SetParent(obj_sv.transform); obj_view.layer = LayerMask.NameToLayer("UI"); RectTransform rect_view = obj_view.AddComponent<RectTransform>(); rect_view.anchorMin = Vector2.zero; rect_view.anchorMax = Vector2.one; rect_view.pivot = new Vector2(0f, 1f); rect_view.anchoredPosition = Vector2.zero; rect_view.sizeDelta = new Vector2(17f, 17f); Mask mask_view = obj_view.AddComponent<Mask>(); mask_view.showMaskGraphic = false; Image image_view = obj_view.AddComponent<Image>(); #if UNITY_EDITOR image_view.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/UIMask.psd"); #else image_view.sprite = sprite_UIMask; #endif image_view.type = Image.Type.Sliced; //content GameObject obj_content = new GameObject("Content"); obj_content.transform.SetParent(obj_view.transform); obj_content.layer = LayerMask.NameToLayer("UI"); RectTransform rect_content = obj_content.AddComponent<RectTransform>(); rect_content.anchorMin = new Vector2(0f, 1f); rect_content.anchorMax = Vector2.one; rect_content.pivot = new Vector2(0f, 1f); rect_content.anchoredPosition = new Vector2(0f, 117f); rect_content.sizeDelta = new Vector2(0f, 300f); //scroll bar horizontal GameObject obj_horizon = AddScrollbar(obj_sv, Vector2.zero, "Scrollbar Horizontal"); RectTransform rect_horizon = obj_horizon.GetComponent<RectTransform>(); rect_horizon.anchorMin = Vector2.zero; rect_horizon.anchorMax = new Vector2(1f, 0f); rect_horizon.pivot = Vector2.zero; rect_horizon.anchoredPosition = Vector2.zero; rect_horizon.sizeDelta = new Vector2(17f, 20f); //scroll bar vertical GameObject obj_vertical = AddScrollbar(obj_sv, Vector2.zero, "Scrollbar Vertical"); RectTransform rect_vertical = obj_vertical.GetComponent<RectTransform>(); rect_vertical.anchorMin = new Vector2(1f, 0f); rect_vertical.anchorMax = Vector2.one; rect_vertical.pivot = Vector2.one; rect_vertical.anchoredPosition = Vector2.zero; rect_vertical.sizeDelta = new Vector2(20f, 17f); scrollRect.content = rect_content; scrollRect.viewport = rect_view; scrollRect.horizontalScrollbar = obj_horizon.GetComponent<Scrollbar>(); scrollRect.horizontalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHideAndExpandViewport; scrollRect.horizontalScrollbarSpacing = -3; scrollRect.verticalScrollbar = obj_vertical.GetComponent<Scrollbar>(); scrollRect.verticalScrollbar.direction = Scrollbar.Direction.BottomToTop; scrollRect.verticalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHideAndExpandViewport; scrollRect.verticalScrollbarSpacing = -3; return obj_sv; } } |
-
前の記事
シェーダでテクスチャフィルタリング 2018.12.15
-
次の記事
シェーダでノイズ1 2019.01.29
コメントを書く