当前位置:   article > 正文

unity 资源加载框架 《七》资源加载和引用计数_assetbundle 引用计数

assetbundle 引用计数

资源加载和引用计数

public class ResMgr : MonoSingleton<ResMgr>
	{
        //全局资源池
		public List<Res> SharedLoadedReses = new List<Res>();

	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
public class ResLoader
	{	
		public T LoadSync<T>(string assetName) where T : Object
		{
			var res = GetOrCreateRes(assetName);

			if (res != null)
			{
				return res.Asset as T;
			}
			
			// 真正加载资源
			res = CreateRes(assetName);

			res.LoadSync();
			
			return res.Asset as T;
		}

		public void LoadAsync<T>(string assetName, Action<T> onLoaded) where T : Object
		{
			// 查询当前的 资源记录
			var res = GetOrCreateRes(assetName);

			if (res != null)
			{
				onLoaded(res.Asset as T);
				
				return;
			}

			// 真正加载资源
			res = CreateRes(assetName);

			res.LoadAsync(loadedRes =>
			{
				onLoaded(loadedRes.Asset as T);
			});
		}


		public void ReleaseAll()
		{
			mResRecord.ForEach(loadedAsset => loadedAsset.Release());

			mResRecord.Clear();
		}
		
		
		#region private	
		private List<Res> mResRecord = new List<Res>();

		private Res GetOrCreateRes(string assetName)
		{
			// 查询当前的 资源记录
			var res = GetResFromRecord(assetName);

			if (res != null)
			{
				return res;
			}

			// 查询全局资源池
			res = GetFromResMgr(assetName);

			if (res != null)
			{
				AddRes2Record(res);

				return res;
			}

			return res;
		}
		
		private Res CreateRes(string assetName)
		{
			Res res = null;

			if (assetName.StartsWith("resources://"))
			{
				res = new ResourcesRes(assetName);
			}
			else
			{
				res = new AssetBundleRes(assetName);
			}			
			
			ResMgr.Instance.SharedLoadedReses.Add(res);
			
			AddRes2Record(res);

			return res;
		}

		private Res GetResFromRecord(string assetName)
		{
			return mResRecord.Find(loadedAsset => loadedAsset.Name == assetName);
		}

		private Res GetFromResMgr(string assetName)
		{
			return ResMgr.Instance.SharedLoadedReses.Find(loadedAsset => loadedAsset.Name == assetName);
		}

		private void AddRes2Record(Res resFromResMgr)
		{
			mResRecord.Add(resFromResMgr);
				
			resFromResMgr.Retain();
		}
		
		#endregion
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/341576
推荐阅读
相关标签
  

闽ICP备14008679号