xingwei.zhu
6 年前
当前提交
1800f47e
共有 7 个文件被更改,包括 161 次插入 和 0 次删除
-
23Runtime/ui/utils/basic_types/generic_list.cs
-
10Runtime/ui/utils/basic_types/matrix.cs
-
88Runtime/ui/utils/basic_types/no_alloc_item.cs
-
19Runtime/ui/utils/basic_types/path.cs
-
21Runtime/ui/utils/basic_types/rect.cs
|
|||
using System.Collections.Generic; |
|||
|
|||
namespace Unity.UIWidgets.ui { |
|||
|
|||
public class uiList<T> : PoolItem { |
|||
List<T> list; |
|||
|
|||
public void Setup() { |
|||
this.list = this.list ?? new List<T>(128); |
|||
} |
|||
|
|||
public void Add(T item) { |
|||
this.list.Add(item); |
|||
} |
|||
|
|||
public override void Dispose() { |
|||
//clear the list immediately to avoid potential memory leak
|
|||
//otherwise, we may clear it in Setup() for lazy action
|
|||
this.list.Clear(); |
|||
base.Dispose(); |
|||
} |
|||
} |
|||
} |
|
|||
namespace Unity.UIWidgets.ui { |
|||
|
|||
public struct uiMatrix3 { |
|||
public static uiMatrix3 fromMatrix3(Matrix3 rect) { |
|||
return new uiMatrix3 { |
|||
|
|||
}; |
|||
} |
|||
} |
|||
} |
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using Unity.UIWidgets.foundation; |
|||
|
|||
namespace Unity.UIWidgets.ui { |
|||
public static class ItemPoolManager { |
|||
static readonly Dictionary<Type, List<PoolItem>> poolDict = new Dictionary<Type, List<PoolItem>>(); |
|||
|
|||
public static T alloc<T>() where T : PoolItem, new() { |
|||
if (!poolDict.ContainsKey(typeof(T))) { |
|||
var pool = new List<PoolItem>(128); |
|||
|
|||
for (int i = 0; i < 128; i++) { |
|||
var item = new T(); |
|||
item.Setup(); |
|||
|
|||
pool.Add(item); |
|||
} |
|||
|
|||
poolDict[typeof(T)] = pool; |
|||
} |
|||
|
|||
var curPool = poolDict[typeof(T)]; |
|||
if (curPool.Count == 0) { |
|||
for (int i = 0; i < 128; i++) { |
|||
var item = new T(); |
|||
item.Setup(); |
|||
|
|||
curPool.Add(item); |
|||
} |
|||
} |
|||
|
|||
var curItem = curPool[0]; |
|||
curPool.RemoveAt(0); |
|||
|
|||
return (T)curItem; |
|||
} |
|||
|
|||
public static void recycle<T>(T item) where T : PoolItem { |
|||
var typeofT = item.GetType(); |
|||
D.assert(poolDict.ContainsKey(typeofT)); |
|||
poolDict[typeofT].Add(item); |
|||
} |
|||
} |
|||
|
|||
public static class TestJob { |
|||
public static void testJob() { |
|||
var TestPoolItem = ItemPoolManager.alloc<TestPoolItem>(); |
|||
|
|||
TestPoolItem.SetLength(2f); |
|||
|
|||
TestPoolItem.Dispose(); |
|||
} |
|||
} |
|||
|
|||
public class TestPoolItem : PoolItem { |
|||
float length; |
|||
|
|||
public void SetLength(float len) { |
|||
this.length = len; |
|||
} |
|||
|
|||
public override void Dispose() { |
|||
this.length = 0; |
|||
base.Dispose(); |
|||
} |
|||
} |
|||
|
|||
public abstract class PoolItem { |
|||
//ensure that base class has a empty constructor
|
|||
public PoolItem() { |
|||
|
|||
} |
|||
|
|||
public void Setup() { |
|||
|
|||
} |
|||
|
|||
public virtual void Dispose() { |
|||
this.Recycle(); |
|||
} |
|||
|
|||
public void Recycle() { |
|||
ItemPoolManager.recycle(this); |
|||
} |
|||
} |
|||
|
|||
} |
|
|||
namespace Unity.UIWidgets.ui { |
|||
|
|||
public class uiPath : PoolItem { |
|||
Path path; |
|||
|
|||
public void Setup() { |
|||
this.path = null; |
|||
} |
|||
|
|||
public void CopyFrom(Path path) { |
|||
this.path = path; |
|||
} |
|||
|
|||
public override void Dispose() { |
|||
this.path = null; |
|||
base.Dispose(); |
|||
} |
|||
} |
|||
} |
|
|||
using UnityEditor.Experimental.UIElements; |
|||
|
|||
namespace Unity.UIWidgets.ui { |
|||
|
|||
public struct uiRect { |
|||
|
|||
public float top; |
|||
public float left; |
|||
public float width; |
|||
public float height; |
|||
|
|||
public static uiRect fromRect(Rect rect) { |
|||
return new uiRect { |
|||
top = rect.top, |
|||
left = rect.left, |
|||
width = rect.width, |
|||
height = rect.height |
|||
}; |
|||
} |
|||
} |
|||
} |
撰写
预览
正在加载...
取消
保存
Reference in new issue