using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class PrefabDestroyer : MonoBehaviour, IPointerClickHandler
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void OnPointerClick(PointerEventData pointerData)
{
Debug.Log(gameObject.name + " がクリックされました!");
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PrefabCreater : MonoBehaviour
{
public GameObject prefab;
void Start()
{
this.CreatePrefab();
}
void Update()
{
}
void CreatePrefab()
{
float x = Random.Range(-5.0f, 5.0f);
float y = Random.Range(0.0f, 5.0f);
float z = Random.Range(-5.0f, 5.0f);
GameObject new_prefab = Instantiate(
prefab,
new Vector3(x, y, z),
Quaternion.identity
);
}
}
解説
最初のこの部分で、まずUnityのUIから動的に生成したいPrefabを指定してあげます。
public GameObject prefab;
こう記述することで、Inspectorビューから事前に生成するプレハブを指定できます。
そして、適当な名前の動的生成用の関数(CreatePrefab)を作ります.
void CreatePrefab()
{
float x = Random.Range(-5.0f, 5.0f);
float y = Random.Range(0.0f, 5.0f);
float z = Random.Range(-5.0f, 5.0f);
GameObject new_prefab = Instantiate(
prefab,
new Vector3(x, y, z),
Quaternion.identity
);
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PrefabCreater : MonoBehaviour
{
public GameObject prefab;
private GameObject new_prefab;
// Start is called before the first frame update
void Start()
{
this.CreatePrefab();
}
// Update is called once per frame
void Update()
{
}
void CreatePrefab()
{
float x = Random.Range(-5.0f, 5.0f);
float y = Random.Range(0.0f, 5.0f);
float z = Random.Range(-5.0f, 5.0f);
new_prefab = Instantiate(
prefab,
new Vector3(x, y, z),
Quaternion.identity
);
Invoke("DestroyPrefab", 3.0f);
}
void DestroyPrefab()
{
Destroy(new_prefab);
}
}
using UnityEngine;
public class Test : MonoBehaviour
{
void Start()
{
this.gameObject.SetActive(false);
}
// Update is called once per frame
void Update()
{
}
}
using UnityEngine;
public class Test : MonoBehaviour
{
Transform t;
void Start()
{
t = this.gameObject.GetComponent<Transform>();
}
// Update is called once per frame
void Update()
{
Vector3 pos = t.position;
pos.x += 0.1f;
t.position = pos;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test2 : MonoBehaviour
{
Transform v;
void Start()
{
v = this.gameObject.GetComponent<Transform>();
}
// Update is called once per frame
void Update()
{
Quaternion rot = v.rotation;
rot.x += 0.01f;
v.rotation = rot;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test3 : MonoBehaviour
{
Transform v;
void Start()
{
v = this.gameObject.GetComponent<Transform>();
Debug.Log(v);
}
// Update is called once per frame
void Update()
{
Vector3 s = v.localScale;
s.x += 0.01f;
v.localScale = s;
}
}
using System;
public sealed class ExClass
{
private static ExClass c = new ExClass();
private ExClass() // privateのコンストラクタ
{
}
public static ExClass GetInstance()
{
return c;
}
}