44. ObjInstatiation
MainCameraに追加
44
using UnityEngine;
public class ObjInstatiation : MonoBehaviour
{
public GameObject obj;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
for (int y = 0; y < 10; y++)
{
for (int x = 0; x < 10; x++)
{
GameObject instantiatedObj = Instantiate(obj) as GameObject;
instantiatedObj.AddComponent<Rigidbody>();
instantiatedObj.transform.position = new Vector3(x, y, 0);
}
}
}
}
}
66. コルーチンを使った例using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoroutineBase : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
StartCoroutine("CoroutineTest");
}
//コルーチン関数を定義
private IEnumerator CoroutineTest() //コルーチン関数の名前
{
Debug.Log("スタート");
yield return new WaitForSeconds(3.0f);
Debug.Log("スタートから3秒後経過した");
}
}
66
67. コルーチンを使った例
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoroutineBase : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
StartCoroutine("CoroutineTest");
}
//コルーチン関数を定義
private IEnumerator CoroutineTest() //コルーチン関数の名前
{
//コルーチンの内容
Debug.Log("スタート");
yield return new WaitForSeconds(3.0f);
Debug.Log("スタートから3秒後経過した");
yield return new WaitForSeconds(5.0f);
Debug.Log("スタートから8秒後経過した");
}
}
67
68. コルーチンを使った例
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoroutineBase : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
StartCoroutine("CoroutineTest");
}
//コルーチン関数を定義
private IEnumerator CoroutineTest() //コルーチン関数の名前
{
//コルーチンの内容
Debug.Log("スタート");
yield return new WaitForSeconds(3.0f);
Debug.Log("スタートから3秒後経過した");
yield return new WaitForSeconds(5.0f);
Debug.Log("スタートから8秒後経過した");
StartCoroutine("CoroutineCallTest");
}
IEnumerator CoroutineCallTest()
{
yield return new WaitForSeconds(2.0f);
Debug.Log("違うコールチンが呼ばれます");
}
}
68
69. Tips:Script Execution Order Setting
CallTest.cs
***********************
using UnityEngine;
public class CallTest : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log("ユニティの本社はアメリカにあります ");
}
}
***********************
SecondCall.cs
***********************
using UnityEngine;
public class SecondCall : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log("今日の夕食はカレーです ");
}
}
*********************** 69
異なるクラス間で、どのスクリプトの Start()が先に呼ばれるかを明示的に指定する方法を説明します。
78. EnemyController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI; //名前空間追加
public class EnemyController : MonoBehaviour
{
public int enemyHealth = 10;
public int damageValue = 5;
public Slider HpSlider;
NavMeshAgent nav; //NavimeshAgent型の変数
GameObject target; //ターゲット追加
// Use this for initialization
void Start()
{
nav = GetComponent<NavMeshAgent>(); //自分自身のNavMeshAgentを参照
target = GameObject.FindGameObjectWithTag("Player"); //targetにPlayerを参照
}
void Update()
{
nav.SetDestination(target.transform.position);
}
//*********************************************************************************
78
79. コルーチンを使った例:
using UnityEngine;
using UnityEngine.AI;
using System.Collections;
public class EnemyController : MonoBehaviour
{
public int enemyHealth = 10;
public int damageValue = 5;
public Slider HpSlider;
NavMeshAgent nav;
Transform target;
// Use this for initialization
void Start()
{
nav = GetComponent<NavMeshAgent>();
target = GameObject.FindGameObjectWithTag("Player").transform;
StartCoroutine(UpdateTargetPath());
}
IEnumerator UpdateTargetPath()
{
float refreshRate = 0.5f;
while (target != null)
{
Vector3 targetPosition = new Vector3(target.position.x, 0, target.position.z);
nav.SetDestination(targetPosition);
yield return new WaitForSeconds(refreshRate);
}
}
//*********************************************************************************
79
91. Scriptable Objectを読み込む
ScriptableObjectTest
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScriptableObjectTest : MonoBehaviour
{
public ScriptableSample sample;
// Start is called before the first frame update
void Start()
{
Debug.Log(sample.name);
Debug.Log(sample.age);
Debug.Log(sample.occupation);
}
}
91
94. 動的にScriptableObjectを読み込み
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScriptableObjectTest : MonoBehaviour
{
// public ScriptableSample sample;
// Start is called before the first frame update
void Start()
{
ScriptableSample sample = Resources.Load<ScriptableSample>("ParameterTable");
Debug.Log(sample.name);
Debug.Log(sample.age);
Debug.Log(sample.occupation);
}
}
94
106. どちらかのカラスが生成されるにする
*実際にはObjectPoolが適用されたものに以下の変更部分を変更して書き直しましょう。
using UnityEngine;
using System.Collections;
public class EnemyManager : MonoBehaviour
{
public GameObject[] enemyPrefab;
public Transform[] spawnPoints;
public float interval = 3.0f;
float time = 0.0f;
void Update()
{
time += Time.deltaTime;
if(time > interval)
{
int enemyIndex = Random.Range(0, enemyPrefab.Length);
int spawnPointIndex = Random.Range(0, spawnPoints.Length);
GameObject enemy = Instantiate(enemyPrefab[enemyIndex],spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
time = 0f;
}
}
}
106
107. 書き換える部分
(人によって書き方が違うと思いますので参考まで)
107
using UnityEngine;
using System.Collections;
using System.Collections.Generic; //追記
public class EnemyManager : MonoBehaviour
{
public GameObject[] enemyPrefab;
public Transform[] spawnPoints;
public float interval = 3.0f;
float time = 0.0f;
List<GameObject> enemyStocks;
private void Start()
{
enemyStocks = new List<GameObject>();
for (int i = 0; i < 10; i++) //10体生成
{
int enemyIndex = Random.Range(0, enemyPrefab.Length);
GameObject obj = (GameObject)Instantiate(enemyPrefab[enemyIndex]); //enemyPrefabを生成
obj.SetActive(false);
enemyStocks.Add(obj);
}
}
126. LoadAssetBundleを追加する
126
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LoadAssetBundle : MonoBehaviour
{
IEnumerator Start()
{
//itemアセットバンドルをロードします
var ab = AssetBundle.LoadFromFile("AssetBundleItem/item");
if (ab == null) {
yield break;
}
// ロードしたファイルの中から 4 Side Diamond(プレハブ) をロードして生成する
var prefab = ab.LoadAsset<GameObject>("4 Side Diamond");
Instantiate(prefab);
}
}
128. 今度はアセットバンドルの中身を全てロードする
128
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LoadAssetBundle: MonoBehaviour
{
IEnumerator Start()
{
var ab = AssetBundle.LoadFromFile("AssetBundleItem/item");
if (ab == null) {
yield break;
}
//アセットバンドル化されていたプレハブを全て読み込んで生成する
var prefabs = ab.LoadAllAssets<GameObject>();
foreach (var prefab in prefabs)
{
Instantiate(prefab);
}
}
}
132. シーンを読み込みます
132
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LoadNewSceneFromAssetBundle: MonoBehaviour
{
IEnumerator Start()
{
var ab = AssetBundle.LoadFromFile("AssetBundleItem/newscene");
if (ab == null) {
yield break;
}
// Secondシーンをロードする
yield return SceneManager.LoadSceneAsync("Second", LoadSceneMode.Additive);
}
}
LoadNewSceneFromAssetBundle
140. ExportAssetBundleToStreamingAssets
140
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
public class ExportAssetBundleToStreamingAssets
{
[MenuItem("Assets/Build AssetBundles To StreamingAssets")]
public static void Build()
{
BuildPipeline.BuildAssetBundles("Assets/StreamingAssets",BuildAssetBundleOptions.None,BuildTarget.StandaloneWindows);
AssetDatabase.Refresh();
// ビルド終了を表示
EditorUtility.DisplayDialog("アセットバンドルの状態","アセットバンドルビルドが終わりました","OK");
}
}
147. Range 値の範囲を指定する
using UnityEngine;
using System.Collections;
public class HelloExtension : MonoBehaviour {
[Range(1,100)]
public int level = 1;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Debug.Log (level);
}
}
147
148. Multilineusing UnityEngine;
using System.Collections;
public class HelloExtension : MonoBehaviour {
[Range(1,100)]
public int level = 1;
[Multiline(5)]
public string message;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Debug.Log (level);
Debug.Log (message);
}
}
148
149. [HideInInspector]
using UnityEngine;
using System.Collections;
public class HelloExtension : MonoBehaviour {
[Range(1,100)]public int level = 1;
[Multiline(5)]public string message;
[HideInInspector] public int x = 10;
// Use this for initialization
void Start () {
Debug.Log (x);
}
// Update is called once per frame
void Update () {
Debug.Log (level);
Debug.Log (message);
}
}
149
150. [SerializeField]
using UnityEngine;
using System.Collections;
public class HelloExtension : MonoBehaviour {
[Range(1,100)]public int level = 1;
[Multiline(5)]public string message;
[HideInInspector] public int x = 10;
[SerializeField] private int y = 5;
// Use this for initialization
void Start () {
Debug.Log (x);
Debug.Log (y);
}
}
150
151. Spaceusing UnityEngine;
using System.Collections;
public class HelloExtension : MonoBehaviour {
[Range(1,100)]public int level = 1;
[Multiline(5)]public string message;
[HideInInspector] public int x = 10;
[SerializeField] private int y = 5;
[Space(10.0f)]public float figure = 30;
// Use this for initialization
void Start () {
Debug.Log (x);
Debug.Log (y);
}
// Update is called once per frame
void Update () {
Debug.Log (level);
Debug.Log (message);
}
} 151
152. Header
using UnityEngine;
using System.Collections;
public class HelloExtension : MonoBehaviour {
[Range(1,100)]public int level = 1;
[Multiline(5)]public string message;
[HideInInspector] public int x = 10;
[SerializeField] private int y = 5;
[Header("プレイヤーのスピード ")]
[Space(10.0f)]public float speed = 30;
// Use this for initialization
void Start () {
Debug.Log (x);
Debug.Log (y);
}
// Update is called once per frame
void Update () {
Debug.Log (level);
Debug.Log (message);
}
}
152