質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

1506閲覧

GUIの画面が点滅する問題

mofumofupafu

総合スコア9

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2018/05/23 08:17

前提・実現したいこと

「Unityにおいて、uGUIのボタンを押すとインベントリがGUIで表示される」というのを実現したいと思っています。現在インベントリ画面自体はすでにできていて、表示も実際に行えています。以前まではボタンでインベントリ表示を行うのは開発中ではいちいちめんどくさいなと思い、InputGetbuttonを使用し、キー操作でインベントリを開いていました。そこでは問題はありませんでした。しかしいざボタンからインベントリを開くというのを実装してみようと思い、InputGetbuttonではなく、uGUIのボタンからインベントリを開くと、GUIのインベントリ画面がめちゃくちゃ点滅しとても見られたものではありません。uGUIのボタンが押されたかどうかはbool変数(charaSetbtn)がtrueになったかどうかで判断しています。

InputGetbuttonではうまく表示できていたのに、uGUIのボタンの方ではうまくいかない理由がわかりません。

発生している問題・エラーメッセージ

uGUIのボタンからGUIのインベントリを開くとインベントリが点滅する(ちらつく)現象が起きる

該当のソースコード

C#

1using UnityEngine; 2using System.Collections; 3using System.Collections.Generic; 4//using UnityEngine.UI; 5 6public class charaInventry : MonoBehaviour { 7 public int Slotx, Sloty; 8 public GUISkin skin; 9 public List<Charas>Inventory = new List<Charas>(); 10 public List<Charas>slots = new List<Charas>(); 11 public List<Charas>Nowslots = new List<Charas>(); 12 private CharaDataBase datadase; 13 public bool showInventory; 14 public Rect slotRect; 15 public Rect NowslotRect; 16 private bool showCharatip; 17 private string charatip; 18 private bool dragingChara; 19 private bool daburiCheck; 20 private Charas draggedChara; 21 private int prevIndex; 22 private int nowIndex; 23 private string At = "Attacker"; 24 private string Ta = "Tank"; 25 private string He = "Healer"; 26 public bool charaSetbtn; //こちらがボタンが押されたかどうかを判断するbool変数 27 28// private Status status; 29 // Use this for initialization 30 void Awake(){ 31 32 DontDestroyOnLoad (this); 33 34 } 35 36 void Start () { 37 daburiCheck = false; 38 charaSetbtn = false;//スタート時はfalseになっています 39 for (int i = 0; i < (Slotx * Sloty); i++) { 40 41 slots.Add (new Charas ()); 42 Inventory.Add (new Charas ()); 43 44 } 45 46 for (int i = 0; i < 3; i++) { 47 48 Nowslots.Add (new Charas ()); 49 } 50 datadase = GameObject.FindGameObjectWithTag ("CharaDatabase").GetComponent<CharaDataBase>(); 51// Debug.Log (datadase.charas[0].charaName); 52 AddChara(0); 53 AddChara(1); 54 AddChara(2); 55// AddChara(3); 56 Inventory.AddRange (Nowslots); 57// Inventory.Add (datadase.charas [0]);//AddCharaと意味一緒 58 59// print(InventoryContains(3)); 60 } 61 62 // Update is called once per frame 63 void Update () { 64 if (charaSetbtn/*Input.GetButtonDown("inventory")*/) {//ここで実際にボタンが押されるとtrueになり関数が呼ばれる 65 showInventory = !showInventory; 66 } 67 68 69 } 70 71 void OnGUI(){ 72 charatip = ""; 73 GUI.skin = skin; 74 if (showInventory){ 75 DrawInventory (); 76 if (showCharatip) 77 GUI.Box (new Rect (Event.current.mousePosition.x+10f,Event.current.mousePosition.y,200,200), charatip,skin.GetStyle("pictip"));//Slot以外の箱スタイル作るべき 78 79 80 } 81 if (dragingChara) { 82 83 GUI.DrawTexture (new Rect (Event.current.mousePosition.x, Event.current.mousePosition.y, 50, 50), draggedChara.CharaImage); 84 85 } 86 87 88 } 89 90 void DrawInventory(){ 91 Event e = Event.current; 92 int i = 0; 93 string NowString; 94 for (int z = 0; z < 3; z++) { 95 NowslotRect = new Rect (z*90,Sloty*60+30,80,40); 96 if (z == 0) 97 NowString = "<color=#fc2020>"+At+ "</color>\n"; 98 else if (z == 1) 99 NowString = "<color=#205efc>"+Ta+ "</color>\n"; 100 else 101 NowString = "<color=#20fc65>"+He+ "</color>\n"; 102 GUI.Box(NowslotRect,NowString,skin.GetStyle("pictip")); 103 104 105 } 106 for (int y = 0; y < Sloty; y++) { 107 for (int x = 0; x < Slotx; x++) { 108 109 if (y == Sloty - 1) { 110 slotRect = new Rect (x * 90, y * 60, 80, 80); 111 112 } else { 113 slotRect = new Rect (x * 60, y * 60, 50, 50); 114 115 } 116 GUI.Box (slotRect,"",skin.GetStyle("Slot")); 117 118 slots [i] = Inventory [i]; 119 120 if (slots [i].charaName != null) { 121 122 GUI.DrawTexture (slotRect, slots [i].CharaImage); 123 124 if (slotRect.Contains (e.mousePosition)) { 125 charatip = CreateCharatip (slots [i]); 126 showCharatip = true; 127 if(e.isMouse && e.type == EventType.mouseDown && e.button==1){ 128 Debug.Log ("c" + i); 129 } 130 131 if (e.button == 0 && e.type == EventType.MouseDrag && !dragingChara) { 132 133 dragingChara = true; 134 prevIndex = i; 135 draggedChara = slots [i]; 136 Inventory [i] = new Charas (); 137 138 } 139 if (e.type == EventType.mouseUp && dragingChara) { 140 nowIndex = i; 141 if (prevIndex <= ((Sloty * Slotx) - 4) && nowIndex <= ((Sloty * Slotx) - 4)) { 142 Inventory [prevIndex] = Inventory [i]; 143 Inventory [i] = draggedChara; 144 dragingChara = false; 145 draggedChara = null; 146 } else if (prevIndex <= ((Sloty * Slotx) - 4) && nowIndex > ((Sloty * Slotx) - 4)) { 147// print (Inventory[(Sloty * Slotx - 3)].charaName); 148 for (int j = Sloty * Slotx - 3; j < Sloty * Slotx; j++) { 149 print (Inventory [j].charaName); 150 if (draggedChara.charaName == Inventory [j].charaName) { 151 daburiCheck = false; 152 break; 153 154 } else { 155 daburiCheck = true; 156 } 157 } 158 159 if (daburiCheck) { 160 if (draggedChara.charaType == "Attacker" && i == (Slotx * Sloty - 3) || (draggedChara.charaType == "Tank" && i == Slotx * Sloty - 2) || (draggedChara.charaType == "Healer" && i == Slotx * Sloty - 1)) { 161 162 163 Inventory [prevIndex] = draggedChara; 164 Inventory [i] = draggedChara; 165 dragingChara = false; 166 draggedChara = null; 167 } 168 } 169 170 else { 171 Inventory [prevIndex] = draggedChara; 172 dragingChara = false; 173 draggedChara = null; 174 } 175 176 177 daburiCheck = false; 178 179 }else if (prevIndex > ((Sloty * Slotx) - 4)) { 180 Inventory [prevIndex] = draggedChara; 181 dragingChara = false; 182 draggedChara = null; 183 } 184 185 } 186 } 187 } else { 188 if(slotRect.Contains (e.mousePosition)){ 189 if (e.button == 0 && e.type == EventType.MouseDrag && !dragingChara) { 190 if(slots [i].charaName != null){ 191 dragingChara = true; 192 prevIndex = i; 193 draggedChara = slots [i]; 194 Inventory [i] = new Charas (); 195 } 196 } 197 if (e.type == EventType.mouseUp && dragingChara){ 198 nowIndex = i; 199 if (prevIndex <= ((Sloty * Slotx) - 4) && nowIndex <= ((Sloty * Slotx) - 4)) { 200 Inventory [i] = draggedChara; 201 dragingChara = false; 202 draggedChara = null; 203 } else if (prevIndex <= ((Sloty * Slotx) - 4) && nowIndex > ((Sloty * Slotx) - 4)) { 204 205 for (int j = Sloty * Slotx - 3; j < Sloty * Slotx; j++) { 206 if (draggedChara.charaName == Inventory [j].charaName) { 207 daburiCheck = false; 208 break; 209 210 } else { 211 daburiCheck = true; 212 } 213 } 214 215 216 if (daburiCheck) { 217 Debug.Log (draggedChara.VIT); 218 if (draggedChara.charaType == "Attacker" && i == (Slotx * Sloty - 3) || (draggedChara.charaType == "Tank" && i == Slotx * Sloty - 2) || (draggedChara.charaType == "Healer" && i == Slotx * Sloty - 1)) { 219 220 Inventory [prevIndex] = draggedChara; 221 Inventory [i] = draggedChara; 222 dragingChara = false; 223 draggedChara = null; 224 }else{ 225 226 Inventory [prevIndex] = draggedChara; 227 dragingChara = false; 228 draggedChara = null; 229 230 } 231 } else{ 232 233 Inventory [prevIndex] = draggedChara; 234 dragingChara = false; 235 draggedChara = null; 236 237 } 238 239 240 daburiCheck = false; 241 242 }else if(prevIndex > ((Sloty * Slotx) - 4) && nowIndex <= ((Sloty * Slotx) - 4)){ 243 Inventory [prevIndex] = draggedChara; 244 // Inventory [i] = Inventory [i]; 245 dragingChara = false; 246 draggedChara = null; 247 }else if(prevIndex > ((Sloty * Slotx) - 4) && nowIndex > ((Sloty * Slotx) - 4)){ 248 Inventory [prevIndex] = Inventory [i]; 249 Inventory [i] = draggedChara; 250 dragingChara = false; 251 draggedChara = null; 252 } 253 } 254 255 } 256 257 } 258 if (charatip == "") { 259 showCharatip = false; 260 } 261 262 i++; 263 } 264 265 } 266 267} 268 269 270 string CreateCharatip(Charas charas){ 271 charatip = "<color=#ffffff>" + charas.charaName + "</color>\n" + "\nATK "+charas.ATK 272 + "\nDEF "+charas.DEF + "\nINT "+charas.INT + "\nAGL "+charas.AGL + "\nDEX "+charas.DEX 273 + "\nVIT "+charas.VIT/100 + "\nMPR "+charas.MPR + "<color=#327cf2>" + "\nLEVEL "+ + charas.LEVEL + "\nEXP "+ + charas.EXP + "</color>\n"; 274 return charatip; 275 276 } 277 void RemoveChara(int id){ 278 for (int i = 0; i < Inventory.Count; i++) { 279 280 281 if (Inventory[i].charaID == id) { 282 Inventory [i] = new Charas(); 283 break; 284 } 285 286 287 288 } 289 290 } 291 292 void AddChara (int id){//選択画面にキャラ追加する 293 for (int i = 0; i < Inventory.Count; i++) { 294 if (Inventory [i].charaName == null) { 295 for (int j = 0; j < datadase.charas.Count; j++) { 296 297 if (datadase.charas [j].charaID == id) { 298 299 Inventory [i] = datadase.charas [j]; 300 } 301 } 302 break; 303 } 304 } 305 } 306 307 bool InventoryContains(int id){ 308 bool result = false; 309 for (int i = 0; i < Inventory.Count; i++) { 310 result = Inventory [i].charaID == id; 311 if (result) { 312 313 break; 314 } 315 } 316 return result; 317 } 318 319 public void InventoryMonitor(){ 320 charaSetbtn = !charaSetbtn; 321 Debug.Log (charaSetbtn); 322 323 } 324} 325 326

試したこと

インベントリスクリプトではなく、ゲームを管理しているスクリプトの方でstaticな変数を作り同じようにやって見ましたが、そちらも結果は変わりませんでした。InputGetbuttonではうまく表示できていたのに、uGUIのボタンの方ではうまくいかない理由がわかりません。

補足情報(FW/ツールのバージョンなど)

unity 5.3.5f1

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

uGUI(に連動させているスクリプト)とInputGetbuttonで挙動が違うようにしてしまっているからです。

InventoryMonitor()で実装されているように、現在は「uGUIのボタンを押す度にcharaSetbtnが切り替わる」処理になっています。
つまり一度charaSetbtnがtrueになった後、再度ボタンを押すまではtrueになりっぱなしです。
一方Input.GetButtonDownは「押された1フレーム分だけtrueになる」のでここが違いです。

影響が出るのはUpdate()の内部、「charaSetbtnがtrueだったらshowInventoryを反転」という処理です。
Updateなのでこの処理は毎フレーム実行されます。
この結果「一度charaSetbtnがtrueになった後、再度ボタンを押すまでshowInventoryを毎フレーム反転」というコードになり、GUIが高速点滅します。

一度charaSetbtnを挟む意図が分からないので具体的な修正案は出しませんが、上記を踏まえて再確認してみてください。

投稿2018/05/23 09:34

sakura_hana

総合スコア11427

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

mofumofupafu

2018/05/23 13:58

GetButtonDownは1フレームだけtrueになるのですね!!助かりました!ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問