1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- /// <summary>
- /// 引用计数类
- /// </summary>
- public class Reference
- {
- private List<Object> _requires;
- public int refCount;
- public virtual void AddReferance()
- {
- refCount++;
- }
- public virtual void Release()
- {
- refCount--;
- }
- /// <summary>
- /// 引用
- /// </summary>
- /// <param name="obj"></param>
- public void Require(Object obj)
- {
- if(_requires == null)
- {
- _requires = new List<Object>();
- _requires.Add(obj);
- AddReferance();
- }
- }
- /// <summary>
- /// 取消引用
- /// </summary>
- /// <param name="obj"></param>
- public void Dequire(Object obj)
- {
- if (_requires == null)
- return;
- if (_requires.Remove(obj))
- Release();
- }
- public bool IsUnused()
- {
- if(_requires != null)
- {
- for (var i = 0; i < _requires.Count; i++)
- {
- var item = _requires[i];
- if (item != null)
- continue;
- Release();
- _requires.RemoveAt(i);
- i--;
- }
- if (_requires.Count == 0)
- _requires = null;
- }
- return refCount <= 0;
- }
- }
|