LoginSignup
1
2

More than 5 years have passed since last update.

Unityのパーティクル生成を一元化するライブラリ作ってみた

Last updated at Posted at 2017-07-02

ライブラリ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;
    }
}
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2