|
namespace SomeTests |
|
{ |
|
public class SharedStaticTests : SimpleTestFixture |
|
{ |
|
public unsafe static class MyData |
|
{ |
|
private static readonly SharedStatic<Container> SharedData; |
|
|
|
private class Key { } |
|
|
|
static MyData() |
|
{ |
|
SharedData = SharedStatic<Container>.GetOrCreate<Key>(); |
|
} |
|
|
|
private struct SomeData1 |
|
{ |
|
public UnsafeList Value; |
|
} |
|
|
|
private struct Container |
|
{ |
|
public bool SomethingElse; |
|
public SomeData1 SomeData1; |
|
} |
|
|
|
public static void SetValue<T>(NativeArray<T> arr) where T : struct |
|
{ |
|
SharedData.Data.SomeData1.Value = new UnsafeList(arr.GetUnsafePtr(), arr.Length); |
|
} |
|
|
|
public static ref readonly T GetSome<T>(int index) where T : struct |
|
{ |
|
return ref UnsafeUtilityEx.ArrayElementAsRef<T>(SharedData.Data.SomeData1.Value.Ptr, index); |
|
} |
|
} |
|
|
|
[Test] |
|
public void Test1() |
|
{ |
|
var testData = new NativeArray<int>(10, Allocator.Temp); |
|
for (int i = 0; i < testData.Length; i++) |
|
testData[i] = i; |
|
|
|
MyData.SetValue(testData); |
|
|
|
Assert.AreEqual(5, MyData.GetSome<int>(5)); |
|
|
|
m_World.GetOrCreateSystem<TestSystem>().Update(); |
|
} |
|
|
|
public class TestSystem : SystemBase |
|
{ |
|
protected override void OnUpdate() |
|
{ |
|
var result = 0; |
|
|
|
Job.WithCode(() => |
|
{ |
|
result = MyData.GetSome<int>(5); |
|
|
|
}).Run(); |
|
|
|
Assert.AreEqual(5, result); |
|
} |
|
} |
|
} |
|
|
|
public class SimpleTestFixture |
|
{ |
|
protected World m_PreviousWorld; |
|
protected World m_World; |
|
protected EntityManager m_Manager; |
|
|
|
|
|
[SetUp] |
|
virtual public void Setup() |
|
{ |
|
m_PreviousWorld = World.DefaultGameObjectInjectionWorld; |
|
m_World = World.DefaultGameObjectInjectionWorld = new World("Test World"); |
|
m_Manager = m_World.EntityManager; |
|
} |
|
|
|
[TearDown] |
|
virtual public void TearDown() |
|
{ |
|
if (m_Manager != null) |
|
{ |
|
m_World.Dispose(); |
|
m_World = null; |
|
|
|
World.DefaultGameObjectInjectionWorld = m_PreviousWorld; |
|
m_PreviousWorld = null; |
|
m_Manager = null; |
|
} |
|
} |
|
} |
|
} |