经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » 游戏设计 » 查看文章
Unity3D|-使用ScriptableObject脚本化对象来制作一个简单的对象池
来源:cnblogs  作者:米开朗基强  时间:2019/2/15 9:22:44  对本文有异议

ScriptableObject是一个用于生成单独Asset的结构。同时,它也能被称为是Unity中用于处理序列化的结构。

可以作为我们存储资源数据的有效方案。同时此资源可以作为我们AB包的有效资源!

ScriptableObject的特点:

  • 不需要绑定到物体对象。
  • 存放于编辑器或者作为一种资源存储。
  • 操作方便,可视化动态修改。
  • 读取数据方便,ScriptableObject已经是可序列化的数据。
  • 可以在项目之间很好的复用而不会丢失数据。

 

 

注意的点

  • 制作ScriptableObject,为了制作ScriptableObject,继承ScriptableObject基类是首先要做的事情。这个时候,类名与Asset名必须要一样。另外,ScriptableObject的限制和MonoBehaviour是一样的。
  • 实例化,通过ScriptableObject.CreateInstance来生成ScriptableObject。使用new操作符来生成是不行的。理由和MonoBehaviour是一样的,Unity生成Object的时候必须经过序列化。
  • 保存,实例化完成后是将Object作为Asset进行保存。通过AssetDatabase.CreateAsset就能够生成Asset。 Asset的后缀名必须是.asset。如果是其他后缀名的话,Unity会无法识别。

 

  1. GameObjectPoolList 继承自ScriptableObject,内容只有一个GameObjectPool类型的List,用于存储所有的池子
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. public class GameObjectPoolList : ScriptableObject
  5. {
  6. //继承自ScriptableObject 表示把类GameObjectPoolList变成可以自定义资源配置的文件
  7. public List<GameObjectPool> poolList;
  8. }

 

  1. GameObjectPool 类是具体的池子,取对象的方法:遍历池子,找到第一个隐藏的对象将其可见并返回使用,找不到隐藏可用的对象时候就首先检查池子的对象数是否超过了最大容量,是的话就删除第一个对象,否则不作操作,接下来再创建一个新的游戏对象使用。
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Collections.Generic;
  5. /// <summary>
  6. /// 资源池
  7. /// </summary>
  8. [Serializable]
  9. public class GameObjectPool {
  10. [SerializeField]
  11. public string name;
  12. [SerializeField]
  13. private GameObject prefab;
  14. [SerializeField]
  15. private int maxAmount;
  16. [NonSerialized]
  17. private List<GameObject> goList = new List<GameObject>();
  18. /// <summary>
  19. /// 表示从资源池中获取一个实例
  20. /// </summary>
  21. public GameObject GetInst()
  22. {
  23. foreach (GameObject go in goList)
  24. {
  25. if (go.activeInHierarchy == false)
  26. {
  27. go.SetActive(true);
  28. return go;
  29. }
  30. }
  31. if (goList.Count >= maxAmount)
  32. {
  33. GameObject.Destroy(goList[0]);
  34. goList.RemoveAt(0);
  35. }
  36. GameObject temp = GameObject.Instantiate(prefab) as GameObject;
  37. goList.Add(temp);
  38. return temp;
  39. }
  40. }

 

 

最后是拓展编辑器的内容以及保存Asset

  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEditor;
  4. public class PoolManagerEditor {
  5. [MenuItem("Manager/Crate GameObjectPoolConfig")]
  6. static void CreateGameObjectPoolList()
  7. {
  8. GameObjectPoolList poolList = ScriptableObject.CreateInstance<GameObjectPoolList>();
  9. string path = PoolManager.PoolConfigPath;
  10. AssetDatabase.CreateAsset(poolList,path);
  11. AssetDatabase.SaveAssets();
  12. }
  13. }

 

 

参考博客:https://blog.csdn.net/xdestiny110/article/details/79678922

 

原文链接:http://www.cnblogs.com/wq-KingStrong/p/10374693.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号