unity游戏开发(一): 工厂模式与资源池

定义游戏物体工厂的接口:

​/// 
/// 游戏物体工厂的接口
/// 
public interface IBaseFacotry  {

    GameObject GetItem(string itemName);

    void PushItem(string itemName,GameObject item);
}​

定义游戏物体类型的工厂基类(存储的是GameObject类型)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// 
/// 游戏物体类型的工厂基类
/// 
public class BaseFactory : IBaseFactory
{
    //当前拥有的gameObject类型的资源(UI,UIPanel,Game) 切记:它存放的是游戏物体资源
    protected Dictionary factoryDict = new Dictionary();
    //对象池字典,每一个游戏物品类型对应一个栈
    //(具体存贮的游戏物体类型的对象)注:对应的是一个具体的游戏物体对象
    protected Dictionary> objectPoolDict = new Dictionary>();

    protected string loadPath;

    public BaseFactory()
    {
        loadPath = "Prefabs/";
    }

    //取实例
    public GameObject GetItem(string itemName)
    {
        GameObject itemGO = null;
        if (objectPoolDict.ContainsKey(itemName))
        {
            if (objectPoolDict[itemName].Count == 0)
            {
                GameObject go = GetResources(itemName);
                itemGO = CreateGameObject(go);
            }
            else
            {
                itemGO = objectPoolDict[itemName].Pop();
                itemGO.SetActive(true);
            }
        }
        else
        {
            objectPoolDict.Add(itemName,new Stack());
            GameObject go = GetResources(itemName);
            itemGO = CreateGameObject(go);
        }
        //安全校验
        if (itemGO == null)
        {
            Debug.Log(itemName + "的实例获取失败");
        }
        return itemGO;
    }

    //放入池子
    public void PushItem(string itemName, GameObject item)
    {
        item.SetActive(false);
        item.transform.SetParent(GameManager.instance.transform);
        if (objectPoolDict.ContainsKey(itemName))
        {
            objectPoolDict[itemName].Push(item);
        }
        else
        {
            Debug.Log("当前字典没有" + itemName + "的栈");
        }
    }

    //取资源
    public GameObject GetResources(string itemName)
    {
        GameObject itemGO = null;
        string itemLoadPath = loadPath + itemName;
        if (factoryDict.ContainsKey(itemName))
        {
            itemGO = factoryDict[itemName];
        }
        else
        {
            itemGO = Resources.Load(itemLoadPath);
            factoryDict.Add(itemName,itemGO);
        }
        //安全校检
        if (itemGO == null)
        {
            Debug.Log(itemName + "的资源获取失败" + ",失败路径:" + itemLoadPath);
        }
        return itemGO;
    }

    //实例化
    private GameObject CreateGameObject(GameObject gameObject)
    {
        GameObject go = GameObject.Instantiate(gameObject);
        return go;
    }
}

定义不同种类的游戏物体工厂的枚举类

public enum FactoryType  {
    UIFactory,
    UIPanelFactory,
    GameFactory
}

子类工厂

public class UIPanelFactory : BaseFactory
{
    public UIPanelFactory()
    {
        loadPath += "UIPanel/";
    }
}

public class UIFactory : BaseFactory
{
    public UIFactory()
    {
        loadPath += "UI/";
    }
}

public class GameFactory : BaseFactory
{
    public GameFactory()
    {
        loadPath += "Game/";
    }
}

其他种类资源工厂的接口,如 AudioClip 、Sprite等,

区别:上面的UIPanel、Game等都是GameObject类型,而这个是其他类型

/// 
/// 其他种类资源工厂的接口,每种工厂获取的资源都不同,所以我们用泛型接口
/// 
/// 
public interface IBaseRescrousFactory
{
    T GetSingleResources(string resourcesName);
}

Sprite工厂

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class SpriteFactory : IBaseRescrousFactory
{
    private Dictionary spriteFactoryDict = new Dictionary();
    protected string loadPath;

    public SpriteFactory()
    {
        loadPath = "Pictures/";
    }

    public Sprite GetSingleResources(string resourcesName)
    {
        Sprite itemGO = null;
        string itemLoadPath = loadPath + resourcesName;
        if (spriteFactoryDict.ContainsKey(resourcesName))
        {
            itemGO = spriteFactoryDict[resourcesName];
        }
        else
        {
            itemGO = Resources.Load(itemLoadPath);
            spriteFactoryDict.Add(resourcesName,itemGO);
        }
        if (itemGO == null)
        {
            Debug.Log(resourcesName + "的资源获取失败,失败路径为:" + itemLoadPath);
        }
        return itemGO;
    }
}

AudioClip工厂

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AudioClipFactory : IBaseRescrousFactory
{
    private Dictionary audioClipFactoryDict = new Dictionary();

    protected string loadPath;

    public AudioClipFactory()
    {
        loadPath = "AudioClips/";
    }

    public AudioClip GetSingleResources(string resourcesName)
    {
        AudioClip itemGO = null;
        string itemLoadPath = loadPath + resourcesName;
        if (audioClipFactoryDict.ContainsKey(resourcesName))
        {
            itemGO = audioClipFactoryDict[resourcesName];
        }
        else
        {
            itemGO = Resources.Load(itemLoadPath);
            audioClipFactoryDict.Add(resourcesName,itemGO);
        }
        if (itemGO == null)
        {
            Debug.Log(resourcesName + "的资源获取失败,失败路径为:" + itemLoadPath);
        }
        return itemGO;
    }
}

工厂管理者:FactoryManager

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FactoryManager {

    public Dictionary factoryDict = new Dictionary();
    public SpriteFactory spriteFactory;
    public AudioClipFactory audioClipFactory;

    public FactoryManager()
    {
        factoryDict.Add(FactoryType.GameFactory,new GameFactory());
        factoryDict.Add(FactoryType.UIFactory, new UIFactory());
        factoryDict.Add(FactoryType.UIPanelFactory, new UIPanelFactory());
        spriteFactory = new SpriteFactory();
        audioClipFactory = new AudioClipFactory();
    }
}

游戏管理者,单例类GameManager,管理工厂管理者FactoryManager。

可向GameManager获取和放入 游戏资源和游戏物体

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// 
/// 游戏总管理,负责管理所有的管理者
/// 
public class GameManager : MonoBehaviour
{
    private static GameManager _instance;
    public static GameManager instance
    {
        get
        {
            return _instance;
        }
    }

    private FactoryManager factoryManager;

    private void Awake()
    {
        _instance = this;
        factoryManager = new FactoryManager();
    }
    //获取Sprite资源
    public Sprite GetSprite(string resourcesName)
    {
        return factoryManager.spriteFactory.GetSingleResources(resourcesName);
    }
    //获取AudioClip资源
    public AudioClip GetAudioClip(string resourcesName)
    {
        return factoryManager.audioClipFactory.GetSingleResources(resourcesName);
    }
    // 得到游戏物体资源
    public GameObject GetGameObjectResources(FactoryType factoryType,string resourcesName)
    {
        return factoryManager.factoryDict[factoryType].GetItem(resourcesName);
    }
    //将游戏物体入栈
    public void PushGameObjectToFactory(FactoryType factoryType,string itemName,GameObject item)
    {
        factoryManager.factoryDict[factoryType].PushItem(itemName, item);
    }
}

 

你可能感兴趣的:(游戏开发)