Skip to content

Commit

Permalink
Add GameObjectColliderCache
Browse files Browse the repository at this point in the history
  • Loading branch information
GiovanniZambiasi committed Jul 15, 2022
1 parent 7c8a6ac commit 79d1461
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
51 changes: 51 additions & 0 deletions Runtime/GameObjectComponentCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Collections.Generic;
using UnityEngine;

namespace MiddleMast
{
public class GameObjectComponentCache<TComponent>
{
Dictionary<int, TComponent> _dictionary;
HashSet<int> _invalids;

public GameObjectComponentCache()
{
_dictionary = new Dictionary<int, TComponent>(4);
_invalids = new HashSet<int>();
}

public bool TryGetComponent(GameObject gameObject, out TComponent component)
{
int instanceID = gameObject.GetHashCode();

if (_dictionary.ContainsKey(instanceID))
{
component = _dictionary[instanceID];
return true;
}

if (_invalids.Contains(instanceID))
{
component = default;
return false;
}

if (gameObject.TryGetComponent(out component))
{
_dictionary.Add(instanceID, component);
return true;
}
else
{
_invalids.Add(instanceID);
return false;
}
}

public void Clear()
{
_dictionary.Clear();
_invalids.Clear();
}
}
}
3 changes: 3 additions & 0 deletions Runtime/GameObjectComponentCache.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.middlemast.core",
"version": "1.5.0",
"version": "1.6.0",
"displayName": "MiddleMast Core",
"description": "",
"unity": "2019.1",
Expand Down

0 comments on commit 79d1461

Please sign in to comment.