ライブラリURL
使い方
ParticleManager.cs
/// <summary>
/// 使い方
/// ①Resourcesディレクトリ配下に生成したいパーティクルを配置
/// ②Instantiate(ParticleManager.Instance.Create("パーティクル名"));
/// </summary>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ParticleManager : SingletonMonoBehaviour<ParticleManager> {
public GameObject Create(string name){
return (GameObject)Resources.Load (string.Format("Particles/{0}", name));
}
}
SingletonMonoBehaviour.cs
using UnityEngine;
using System;
public abstract class SingletonMonoBehaviour<T> : MonoBehaviour where T : MonoBehaviour{
private static T instance;
public static T Instance
{
get{
if (instance == null) {
Type t = typeof(T);
instance = (T)FindObjectOfType (t);
if (instance == null) {
Debug.LogError (t + " をアタッチしているGameObjectはありません");
}
}
return instance;
}
}
virtual protected void Awake(){
// 他のゲームオブジェクトにアタッチされているか調べる
// アタッチされている場合は破棄する。
CheckInstance();
}
protected bool CheckInstance(){
if (instance == null) {
instance = this as T;
return true;
} else if (Instance == this) {
return true;
}
Destroy (this);
return false;
}
}