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

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

新規登録して質問してみよう
ただいま回答率
85.47%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Unity

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

Q&A

0回答

635閲覧

【Unity】生成したオブジェクトがiOSデバイスで表示されない

takk_014

総合スコア53

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Unity

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

0グッド

0クリップ

投稿2021/08/03 08:03

Instantiateで生成するオブジェクトがiOSデバイスで表示されないです。
Unityのシミュレーター上でそのiOSデバイスと同じ画面サイズに設定した状態では表示することができるのですが・・・
Instantiateで生成したのではなく元から配置してあるオブジェクトはiOSデバイスでも表示されます。
下記がコードになります。

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; //追加 5 6[RequireComponent(typeof(MoveSceneManager))] 7[RequireComponent(typeof(SaveManager))] 8[RequireComponent(typeof(SoundManager))] 9[DefaultExecutionOrder(-5)] 10 11public class GameManager : SingletonMonoBehaviour<GameManager> 12{ 13 [Header("シーンロード時に自動生成するプレハブを登録")] 14 [SerializeField] 15 GameObject[] prefabs = null; 16 17 //--ここから追加-- 18 [Header("ゲーム設定")] 19 [SerializeField] 20 GameObject playerPrefab = null; //追加その3 21 [SerializeField] 22 int maxLevel = 10; 23 [SerializeField, Min(1), Tooltip("LvUp毎に障害物の生成間隔を小さくするための除数")] 24 float divisor = 1.1f; 25 [SerializeField, Tooltip("スコアの桁数")] 26 int scoreDigits = 8; 27 [SerializeField] 28 Vector2 playerSpawnPosition = Vector2.zero; 29 [SerializeField, Tooltip("1メートルを何秒で走るか")] 30 float secondsPerMeter = 0.05f; 31 [SerializeField, Tooltip("1メートル走った時に加算される基本スコア")] 32 int scorePerMeter = 10; 33 [SerializeField, Tooltip("レベルアップに必要な走行距離")] 34 int meterPerLevel = 100; 35 [Header("UIの設定")] 36 [SerializeField] 37 string mileageTextName = "MileageText"; 38 [SerializeField] 39 string scoreTextName = "ScoreText"; 40 [SerializeField] 41 string highScoreTextName = "HighScoreText"; 42 [SerializeField] 43 string levelTextName = "LvText"; 44 [SerializeField] 45 string gameOverCanvasName = "GameOverCanvas(Clone)"; //追加その2 46 [SerializeField] 47 string retryButtonName = "RetryButton"; //追加その2 48 [SerializeField] 49 string countDownTextName = "CountdownText"; //追加その3 50 //--追加ここまで-- 51 52 MoveSceneManager moveSceneManager; 53 SaveManager saveManager; 54 SoundManager soundManager; 55 56 //--ここから追加-- 57 bool gameStarted = false; //追加その3 58 bool timerIsActive = false; 59 int level = 1; 60 int mileage = 0; //走行距離 61 int maxScore = 0; 62 int score = 0; 63 int highScore = 0; 64 Text mileageText; 65 Text scoreText; 66 public Text addScoreText; 67 Text highScoreText; 68 Text levelText; 69 Text countdownText; //追加その3 70 WallSpawner wallSpawner; 71 Coroutine timer; 72 Canvas gameOverCanvas; //追加その2 73 Button retryButton; //追加その2 74 Button jumpButton; //追加その2 75 public static GameObject playerObj; //追加その2 76 CoinSpawner coinSpawner; //追加その4 77 78 79 public int Score 80 { 81 set{ 82 score = Mathf.Clamp(value, 0, maxScore); 83 if(score > highScore) 84 { 85 highScore = score; 86 } 87 UpdateScoreUi(); 88 } 89 get{ 90 return score; 91 } 92 } 93 public int HighScore 94 { 95 get{ 96 return highScore; 97 } 98 } 99 //--追加ここまで-- 100 101 protected override void Awake() 102 { 103 base.Awake(); 104 105 if (Debug.isDebugBuild){} 106 107 moveSceneManager = GetComponent<MoveSceneManager>(); 108 saveManager = GetComponent<SaveManager>(); 109 soundManager = GetComponent<SoundManager>(); 110 } 111 112 void Start() 113 { 114 if (Debug.isDebugBuild) 115 { 116 InstantiateWhenLoadScene(); 117 LoadComponents(); //追加 118 GameStart(); //追加 119 } 120 } 121 122 void Update() 123 { 124 if (!gameStarted || moveSceneManager.SceneName == "Title") 125 { 126 if(timer != null) 127 { 128 StopCoroutine(timer); 129 } 130 return; 131 } 132 if (!timerIsActive) 133 { 134 timer = StartCoroutine("MileageTimer"); 135 } 136 } 137 138 public void InstantiateWhenLoadScene() 139 { 140 if(moveSceneManager.SceneName == "Title") 141 { 142 return; 143 } 144 145 foreach (GameObject prefab in prefabs) 146 { 147 Instantiate(prefab, transform.position, Quaternion.identity); 148 } 149 } 150 151 //--ここから追加-- 152 public void InitGame() 153 { 154 gameStarted = false; //追加その3 155 level = 1; 156 mileage = 0; 157 maxScore = (int)Mathf.Pow(10, scoreDigits) - 1; //スコアの最大値を作成。例えば、8桁なら99999999 158 score = 0; 159 timerIsActive = false; 160 wallSpawner.isActive = false; //追加その3 161 wallSpawner.InitSpawner(); //追加その3 162 coinSpawner.isActive = false; //追加その4 163 coinSpawner.InitSpawner(); //追加その4 164 highScore = saveManager.HighScore; //追加その5 165 166 //追加その3。UI表示の初期化 167 UpdateMileageUi(); 168 UpdateLevelUi(); 169 UpdateScoreUi(); 170 } 171 public void GameStart() 172 { 173 InitGame(); 174 StartCoroutine("Countdown"); 175 } 176 public void GameOver(){ 177 if(playerObj != null) 178 { 179 Destroy(playerObj); 180 } 181 gameStarted = false; //追加その3 182 timerIsActive = false; 183 wallSpawner.isActive = false; 184 gameOverCanvas.enabled = true; 185 retryButton.enabled = true; //追加その3 186 coinSpawner.isActive = false; //追加その4 187 jumpButton.enabled = false; 188 189 //追加その5 ゲームオーバーした時にハイスコアを更新する処理 190 if (highScore > saveManager.HighScore) 191 { 192 saveManager.HighScore = highScore; 193 saveManager.Save(); //jsonを 取得&出力 する処理 194 } 195 } 196 public void Retry(){ 197 //シーンにある障害物を全部消去する 198 GameObject[] walls = GameObject.FindGameObjectsWithTag("Wall"); 199 foreach(GameObject wall in walls) 200 { 201 Destroy(wall); 202 } 203 //追加4.コインを消去 204 GameObject[] coins = GameObject.FindGameObjectsWithTag("Item"); 205 foreach (GameObject coin in coins) 206 { 207 Destroy(coin); 208 } 209 gameOverCanvas.enabled = false; 210 retryButton.enabled = false; //追加その3 211 GameStart(); 212 } 213 // ここでカウントダウンの反復処理をしている 214 IEnumerator Countdown() 215 { 216 //最初はスコアを非表示にする 217 mileageText.enabled = false; 218 scoreText.enabled = false; 219 highScoreText.enabled = false; 220 levelText.enabled = false; 221 jumpButton.enabled = false; 222 223 countdownText.enabled = false; 224 yield return new WaitForSeconds(1); 225 countdownText.enabled = true; 226 for(int i = 2; i >= 0; i--) 227 { 228 countdownText.text = (i + 1).ToString(); 229 yield return new WaitForSeconds(1); 230 } 231 countdownText.enabled = false; 232 233 //スコアを表示にする 234 mileageText.enabled = true; 235 scoreText.enabled = true; 236 highScoreText.enabled = true; 237 levelText.enabled = true; 238 jumpButton.enabled = true; 239 // ここでPlayerを生成している 240 playerObj = Instantiate(playerPrefab, playerSpawnPosition, Quaternion.identity); 241 gameStarted = true; 242 wallSpawner.isActive = true; 243 coinSpawner.isActive = true; //追加その4 244 } 245 246 //シーン読み込み時に各種コンポーネントを取得するメソッド 247 public void LoadComponents() 248 { 249 if (moveSceneManager.SceneName == "Title") 250 { 251 return; 252 } 253 wallSpawner = GameObject.FindGameObjectWithTag("WallSpawner").GetComponent<WallSpawner>(); 254 mileageText = GameObject.Find(mileageTextName).GetComponent<Text>(); 255 scoreText = GameObject.Find(scoreTextName).GetComponent<Text>(); 256 addScoreText = GameObject.Find("AddScore").GetComponent<Text>(); 257 highScoreText = GameObject.Find(highScoreTextName).GetComponent<Text>(); 258 levelText = GameObject.Find(levelTextName).GetComponent<Text>(); 259 260 //追加その2 261 gameOverCanvas = GameObject.Find(gameOverCanvasName).GetComponent<Canvas>(); 262 retryButton = GameObject.Find(retryButtonName).GetComponent<Button>(); 263 jumpButton = GameObject.Find("JumpButton").GetComponent<Button>(); 264 265 //追加その3 266 countdownText = GameObject.Find(countDownTextName).GetComponent<Text>(); 267 268 269 270 //ボタンにクリック時の処理を登録 271 retryButton.onClick.AddListener(() => Retry()); 272 //追加その4 273 coinSpawner = GameObject.FindGameObjectWithTag("CoinSpawner").GetComponent<CoinSpawner>(); 274 } 275 IEnumerator MileageTimer() 276 { 277 timerIsActive = true; 278 mileage++; 279 LevelUp(); 280 UpdateMileageUi(); 281 Score += scorePerMeter * level; 282 yield return new WaitForSeconds(secondsPerMeter); 283 timerIsActive = false; 284 } 285 void LevelUp() 286 { 287 if(level < maxLevel && mileage % meterPerLevel == 0) 288 { 289 level++; 290 UpdateLevelUi(); 291 //障害物の生成間隔を小さくする 292 wallSpawner.MinWaitTime /= divisor; 293 wallSpawner.MaxWaitTime /= divisor; 294 295 coinSpawner.MinWaitTime /= divisor; //追加その4 296 coinSpawner.MaxWaitTime /= divisor; //追加その4 297 } 298 } 299 void UpdateMileageUi() 300 { 301 mileageText.text = mileage.ToString() + "m"; 302 } 303 void UpdateScoreUi() 304 { 305 scoreText.text = "Score: " + score.ToString("D" + scoreDigits.ToString()); //ToStringに特定の文字列を渡すと、桁数などを指定できる 306 highScoreText.text = "High: " + highScore.ToString("D" + scoreDigits.ToString()); 307 } 308 void UpdateLevelUi() 309 { 310 levelText.text = "Lv." + level.ToString(); 311 } 312 313 // コイン取得時のスコア表示を消す処理 314 public void hiddenAddScore() 315 { 316 Invoke("testInvoke", 1); 317 return; 318 } 319 void testInvoke() 320 { 321 addScoreText.enabled = false; 322 } 323 324}

iOSデバイスでもInstantiateで生成したオブジェクトを表示するにはどうすれば良いでしょうか?

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問