Skip to content

Instantly share code, notes, and snippets.

@jeffvella
Created April 6, 2020 16:36
Show Gist options
  • Select an option

  • Save jeffvella/b90ca308fdd47f7566687d9efc0da17a to your computer and use it in GitHub Desktop.

Select an option

Save jeffvella/b90ca308fdd47f7566687d9efc0da17a to your computer and use it in GitHub Desktop.
An example of storing data in SharedStatic.
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;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment