Last active
April 19, 2020 15:58
-
-
Save jeffvella/1fd3b14b74dbc0208251366316d6f7ff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using Unity.Collections; | |
| using Unity.Collections.LowLevel.Unsafe; | |
| using Unity.Entities; | |
| using UnityEngine; | |
| public struct TerrainSprite | |
| { | |
| public int TerrainId; | |
| public int VariantId; | |
| public int BitMaskId; | |
| public override int GetHashCode() | |
| { | |
| int hash = 13; | |
| hash = (hash * 7) + TerrainId; | |
| hash = (hash * 7) + VariantId; | |
| hash = (hash * 7) + BitMaskId; | |
| return hash; | |
| } | |
| } | |
| public struct DataLocation | |
| { | |
| public int FirstValueIndex; | |
| public int TotalValues; | |
| } | |
| public class MyAmazingSystem : SystemBase | |
| { | |
| public NativeHashMap<int, DataLocation> _map; | |
| public NativeList<Color32> _colors; | |
| // Test Data | |
| public const int ColorsPerSprite = 64; | |
| public const int SpriteCount = 100; | |
| private NativeList<TerrainSprite> _terrainSprites; | |
| protected override void OnCreate() | |
| { | |
| _map = new NativeHashMap<int, DataLocation>(1, Allocator.Persistent); | |
| _colors = new NativeList<Color32>(ColorsPerSprite, Allocator.Persistent); | |
| CreateSomeTestData(); | |
| var startIndex = 0; | |
| for (int i = 0; i < _terrainSprites.Length; i++) | |
| { | |
| var sprite = _terrainSprites[i]; | |
| for (int j = 0; j < ColorsPerSprite; j++) | |
| _colors.Add(new Color32()); | |
| _map.Add(sprite.GetHashCode(), new DataLocation | |
| { | |
| FirstValueIndex = startIndex, | |
| // not nessecary if the color count is always the same. | |
| TotalValues = ColorsPerSprite | |
| }); | |
| startIndex += ColorsPerSprite; | |
| } | |
| } | |
| private void CreateSomeTestData() | |
| { | |
| _terrainSprites = new NativeList<TerrainSprite>(SpriteCount, Allocator.Temp); | |
| for (int i = 0; i < SpriteCount; i++) | |
| { | |
| _terrainSprites[i] = new TerrainSprite | |
| { | |
| BitMaskId = UnityEngine.Random.Range(0, int.MaxValue), | |
| TerrainId = UnityEngine.Random.Range(0, int.MaxValue), | |
| VariantId = UnityEngine.Random.Range(0, int.MaxValue), | |
| }; | |
| } | |
| } | |
| protected unsafe override void OnUpdate() | |
| { | |
| var randomSpriteIndex = UnityEngine.Random.Range(0, _terrainSprites.Length - 1); | |
| var spriteToFindColorsFor = _terrainSprites[randomSpriteIndex]; | |
| var results = new NativeArray<Color>(ColorsPerSprite, Allocator.Temp); | |
| if (_map.TryGetValue(spriteToFindColorsFor.GetHashCode(), out var location)) | |
| { | |
| var sourcePtr = (byte*)_colors.GetUnsafePtr() + location.FirstValueIndex * sizeof(Color32); | |
| var size = location.TotalValues * sizeof(Color32); | |
| UnsafeUtility.MemCpy(results.GetUnsafePtr(), sourcePtr, size); | |
| } | |
| // do something with results; | |
| } | |
| protected override void OnDestroy() | |
| { | |
| _terrainSprites.Dispose(); | |
| _map.Dispose(); | |
| _colors.Dispose(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment