http://www.xiaobao1993.com/886.html

来源与小宝个人笔记【稍作修改】

//使用  Prefabs/Resources/stone1

ResourceMgr.GetInstance().CreateGameObject ("stone1",image,true);

using UnityEngine;
using System.Collections; public class ResourceMgr : MonoBehaviour
{
#region 初始化
private static ResourceMgr mInstance; /// <summary>
/// 获取资源加载实例
/// </summary>
/// <returns></returns>
public static ResourceMgr GetInstance()
{
if(mInstance == null)
{
mInstance = new GameObject("_ResourceMgr").AddComponent<ResourceMgr>();
}
return mInstance;
}
private ResourceMgr()
{
Debug.LogError ("---------------");
hashtable = new Hashtable();
}
#endregion /// <summary> 资源缓存容器 </summary>
private Hashtable hashtable;
/// <summary>
/// Load 资源
/// </summary>
/// <typeparam name="T">资源类型</typeparam>
/// <param name="path">资源路径</param>
/// <param name="cacheAsset">是否要缓存资源</param>
/// <returns></returns>
public T Load<T>(string path, bool cache) where T : Object
{
if (hashtable.Contains(path))
{
return hashtable[path] as T;
} Debug.Log(string.Format("Load assset frome resource folder,path:{0},cache:{1}", path, cache));
T assetObj = Resources.Load<T>(path);
if (assetObj == null)
{
Debug.LogWarning("Resources中找不到资源:" + path);
}
if (cache)
{
hashtable.Add(path, assetObj);
Debug.Log("Asset对象被缓存,Resource'path=" + path);
}
return assetObj;
} /// <summary>
/// 创建Resource中GameObject对象
/// </summary>
/// <param name="path"资源路径</param>
/// <param name="cacheAsset">是否要缓存Asset对象</param>
/// <returns></returns>
public GameObject CreateGameObject(string path,GameObject parent, bool cache)
{
GameObject assetObj = Load<GameObject>(path, cache);
GameObject go = Instantiate(assetObj) as GameObject;
if (go != null && parent != null)
{
Transform t = go.transform;
t.parent = parent.transform;
t.localPosition = Vector3.zero;
t.localRotation = Quaternion.identity;
t.localScale = Vector3.one;
go.layer = parent.layer;
}
if (go == null)
{
Debug.LogWarning("从Resource创建对象失败:" + path);
}
return go;
} // Use this for initialization
void Start () { } // Update is called once per frame
void Update () { }
}

  

最新文章

  1. 《移动端浏览器Touch事件判断手指滑动方向方法》
  2. MySQL中进行模糊搜索的一些问题
  3. idea转eclipse 设置注意。
  4. JS中 计算器的简单制作
  5. SQL变量、运算符、分支、循环语句
  6. 自定义圆形控件RoundImageView并认识一下attr.xml
  7. javascript 一个关于时间排序的算法(一个页面多个倒计时排序)
  8. oracle 字符集转换:AL32UTF8-&gt;ZHS16GBK
  9. great C++ socket library
  10. Android SDK教程
  11. nodeJS之路径PATH模块
  12. Mac 下安装node.js
  13. Ajax 的优势和不足
  14. tuxedo开发
  15. 关于Ajax的认识和封装(小记)
  16. Collection 接口
  17. curl 模拟表单post文件
  18. Android 音视频深入 十一 FFmpeg和AudioTrack播放声音(附源码下载)
  19. c#cookie读取写入操作
  20. Sitecore安装(手动方式)

热门文章

  1. ntp服务器池列表
  2. ubuntu下的Samba配置:使每个用户可以用自己的用户名和密码登录自己的home目录
  3. hdu 5495 LCS
  4. qsort的几种用法
  5. Grails重定向以及Render、Session的使用
  6. wso2esb源码编译总结
  7. Base64编码和解码算法
  8. Android应用程序组件Content Provider的启动过程源代码分析
  9. activity之栈管理
  10. iOS 字体设置