サンタゲームのメインスクリプト.単眼VRで照準に入ったら色を変えたり,スコアを表示したり,残時間を表示するなど.
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 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 | using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; public class scope: MonoBehaviour { [Header( "クロスヘア(照準)の画像をつけたUIを入れる" )] public Image aimPointImage; [Header( "スライダー(補足時間目安)のUIを入れる" )] public Slider myslider; //UIのスライダー float slideValue = 0f; //スライダーの値用の変数 bool soundBool = false ; //音を連続再生されるのを防止するBool [Header( "何点ゲットしたかを表示する画像用のUI" )] public Image pointImage; //何点ゲットしたかを表示する画像用のUI [Header( "上のImageが入っているGameObject" )] public GameObject imageObj; //上のImageが入っているGameObject [Header( "10点用画像" )] public Sprite point10; //Imageに割り当てる画像(スプライト10点用以下同じ) [Header( "20点用画像" )] public Sprite point20; [Header( "50点用画像" )] public Sprite point50; [Header( "クロスヘアにロックした時の音" )] public AudioClip scopesound; //クロスヘアにロックした時の音 [Header( "プレゼントをゲットした音" )] public AudioClip getsound; //プレゼントをゲットした音 AudioSource audio; //オーディオソース用 AudioSource audio2; [Header( "ポイント表示用UI" )] public Text pointTxt; //文字変数ポイント用 [Header( "時間表示用UI" )] public Text timeText; //時間表示用文字 int mypoint; //ポイント用Int [Header( "初期残時間" )] public int mytime; //時間用 private void Start() { AudioSource[] audioSources = gameObject.GetComponents<AudioSource>(); //Maincameraにアサインされている複数のAudioSourceを取得し配列に入れる audio = audioSources[0]; //一つ目のオーディオソースの名前をaudioに audio2 = audioSources[1]; imageObj.SetActive( false ); //ポイント取得画像パネルを隠す mypoint = 0; pointTxt.text = mypoint.ToString(); //UIの文字を初期化する timeText.text = mytime.ToString(); //上と同じ Invoke( "startTimer" , 3f); //3秒後にタイマースタート } void FixedUpdate() { // Rayを飛ばす Ray ray = new Ray(transform.position, transform.forward); // outパラメータ用に、Rayのヒット情報を取得するための変数を用意 RaycastHit hit; // Rayのhit情報を取得する if (Physics.SphereCast(ray, 0.5f, out hit, 80.0f)){ //Rayで球を飛ばす // Rayがhitしたオブジェクトのタグ名を取得 string hitTag = hit.collider.tag; // タグの名前がpresentだったら、照準の色が変わる if ((hitTag.Equals( "present" ))){ //照準を赤に変える aimPointImage.color = new Color(1.0f, 0.0f, 0.0f, 1.0f); slideValue += 1.5f; //スライドバー用の値をアップさせる myslider.value = slideValue; //スライドバーの値をセットする if (!soundBool){ //連打再生されないようにBoolで飛ばす soundBool = true ; audio.PlayOneShot(scopesound, 0.2f); //スコープ用の音を再生する } //99越えたら消す if (slideValue > 99){ pointget(hit.collider.gameObject); Destroy(hit.collider.gameObject); soundBool = false ; //再び再生できるようにする } } else { // present以外では水色に aimPointImage.color = new Color(0.0f, 1.0f, 1.0f, 1.0f); slideValue = 0f; //スライド値を0にする myslider.value = slideValue; //その値をスライドにセットする audio.Stop(); //音再生停止 soundBool = false ; } } else { // Rayがヒットしていない場合は水色に aimPointImage.color = new Color(0.0f, 1.0f, 1.0f, 1.0f); slideValue = 0f; myslider.value = slideValue; audio.Stop(); soundBool = false ; } } void pointget(GameObject go){ //音再生 audio2.PlayOneShot(getsound); //ポイント表示 //InstatiateしたPrefab名で足す点数を変えている //と同時にポイント用画像を選定してセットする if (go.name.Contains( "PresentYel" )) { pointImage.sprite = point10; mypoint += 10; } else if (go.name.Contains( "PresentBlue" )) { pointImage.sprite = point20; mypoint += 20; } else if (go.name.Contains( "PresentRed" )) { pointImage.sprite = point50; mypoint += 50; } PlayerPrefs.SetInt( "score" , mypoint); //End画面用にPlayerPrefsに得点をセットする imageObj.SetActive( true ); //得点画面を出す Invoke( "hidePoint" , 1f); //1秒後にポイント画面を隠す pointTxt.text = mypoint.ToString(); //ポイントを文字列に変更して表示 } void hidePoint(){ imageObj.SetActive( false ); //ポイント画面を隠す } void startTimer(){ StartCoroutine ( "myTimer" ); //コルーチンをスタートする } IEnumerator myTimer() { while ( true ) { mytime--; //初期設定の120秒を減らす if (mytime < 10){ //10秒未満になったら残り時間を変える timeText.color = new Color(1.0f, 0.0f, 0.0f, 1.0f); //文字を赤色に } else { timeText.color = new Color(1.0f, 1.0f, 1.0f, 1.0f); //文字を白色に } timeText.text = mytime.ToString(); yield return new WaitForSeconds(1.0f); //1秒間待つ //終了処理 if (mytime < 1){ Invoke( "showScore" , 2f); yield break ; //終了処理へ } } } //やめるボタン用 public void goHomeBtn(){ SceneManager.LoadScene( "op" ); } //タイムアップ処理 void showScore(){ //スコア表示画面へ SceneManager.LoadScene( "end" ); } } |
スクリプトの使い方です.説明文を入れていますので参考に.
