35. 効果音を流してみよう
• PlayerController を開く
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
36. 効果音を流してみよう
• 変数 audioSource を追加(これに Component を代入する)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public AudioSource audioSource;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
Inspector から設定
できるように public
型は
AudioSource型
40. 効果音を流してみよう
• スペースを押した時に再生するようにしてみる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public AudioSource audioSource;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
41. 効果音を流してみよう
• スペースを押した時に再生するようにしてみる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public AudioSource audioSource;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
}
}
}
Input.GetKeyDown(対応するKeyCode)
押した瞬間にtrueになる
42. 効果音を流してみよう
• スペースを押した時に再生するようにしてみる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public AudioSource audioSource;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
audioSource.Play();
}
}
}
AudioSourceが代入された変数.Play();
43. 効果音を流してみよう
• 完成
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public AudioSource audioSource;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
audioSource.Play();
}
}
}
変数定義
Spaceを押すと
再生
48. マウスクリックを取得する
• Input.GetMouseButtonDown(ボタンの種類)
• ボタンの種類
• 左クリック: 0
• 右クリック: 1
• 中クリック: 2
• 確認してみる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public AudioSource audioSource;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
audioSource.Play();
}
}
}
マウスの左クリックで
再生
53. マウスの座標を取得する
• ObjectGeneratorを開く
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectGenerator : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
54. マウスの座標を取得する
• 生成する GameObject を代入する変数 prefab を追加
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectGenerator : MonoBehaviour
{
public GameObject prefab;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
56. マウスの座標を取得する
• マウスの左クリックで生成できるようにしてみる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectGenerator : MonoBehaviour
{
public GameObject prefab;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector2 position = Input.mousePosition;
Instantiate(prefab, position, Quaternion.identity);
}
}
}
マウスを左クリックした時
マウスの座標を変数に代入
GameObjectを生成
63. マウスの座標を取得する
• やってみる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectGenerator : MonoBehaviour
{
public GameObject prefab;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector2 position = Input.mousePosition;
Instantiate(prefab, position, Quaternion.identity);
}
}
}
64. マウスの座標を取得する
• やってみる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectGenerator : MonoBehaviour
{
public GameObject prefab;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector2 screenPosition = Input.mousePosition;
Vector2 worldPosition = Camera.main.ScreenToWorldPoint(screenPosition);
Instantiate(prefab, worldPosition, Quaternion.identity);
}
}
}
68. マウスの先にあるGameObjectを取得する
• 毎フレームマウスの先のGameObjectを取得し、Consoleに出力
(重なってる場合は全部取得)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class (クラス名) : MonoBehaviour
{
// Update is called once per frame
void Update()
{
Vector2 position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Collider2D[] collisions = Physics2D.OverlapPointAll(position);
foreach (Collider2D collision in collisions)
{
Debug.Log(collision.gameObject);
}
}
}
69. マウスの先にあるGameObjectを取得する
• 使い途の例
• 右クリックした時にマウスの先にあるGameObjectを全て削除
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectDestroyer : MonoBehaviour
{
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(1))
{
Vector2 position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Collider2D[] collisions = Physics2D.OverlapPointAll(position);
foreach (Collider2D collision in collisions)
{
Destroy(collision.gameObject);
}
}
}
}
右クリックした時に マウスのワールド
座標を取得
マウスに重なっている
Collider2Dを全取得ひとつひとつ
に対して…
GameObjectを削除
79. ボタンを作る
• 押したときの処理の設定の仕方
1. スクリプトに public 関数として書く
2. ボタンの OnClick() に設定
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ClickHandler : MonoBehaviour
{
public void SayHello()
{
Debug.Log("Hello, Unity!");
}
}
public関数
「SayHello()」を記述
102. 補足 : マウスの座標取得について
• これは動くが
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectGenerator : MonoBehaviour
{
public GameObject prefab;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector2 screenPosition = Input.mousePosition;
Vector2 worldPosition = Camera.main.ScreenToWorldPoint(screenPosition);
Instantiate(prefab, worldPosition, Quaternion.identity);
}
}
}
103. 補足 : マウスの座標取得について
• これは動かない
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectGenerator : MonoBehaviour
{
public GameObject prefab;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector2 screenPosition = Input.mousePosition;
Vector3 worldPosition = Camera.main.ScreenToWorldPoint(screenPosition);
Instantiate(prefab, worldPosition, Quaternion.identity);
}
}
}