Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions Assets/UnityDebugSheet/Runtime/Core/Scripts/DebugPageBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
using UnityDebugSheet.Runtime.Foundation.PageNavigator.Modules;
using UnityDebugSheet.Runtime.Foundation.TinyRecyclerView;
using UnityEngine;
#if UNITY_6000_5_OR_NEWER
using ObjectId = UnityEngine.EntityId;
#else
using ObjectId = System.Int32;
#endif

namespace UnityDebugSheet.Runtime.Core.Scripts
{
Expand All @@ -12,7 +17,7 @@ public abstract class DebugPageBase : Page, IRecyclerViewCellProvider, IRecycler
private readonly PriorityList<int> _itemIds = new PriorityList<int>();
private readonly Dictionary<int, int> _itemIdToDataIndexMap = new Dictionary<int, int>();
private readonly List<ItemInfo> _itemInfos = new List<ItemInfo>();
private readonly Dictionary<int, string> _objIdToPrefabKeyMap = new Dictionary<int, string>();
private readonly Dictionary<ObjectId, string> _objIdToPrefabKeyMap = new Dictionary<ObjectId, string>();

private readonly Dictionary<string, ObjectPool<GameObject>> _prefabPools =
new Dictionary<string, ObjectPool<GameObject>>();
Expand Down Expand Up @@ -73,6 +78,15 @@ protected virtual void OnDestroy()
pool.Clear();
}

private static ObjectId GetObjectId(Object obj)
{
#if UNITY_6000_5_OR_NEWER
return obj.GetEntityId();
#else
return obj.GetInstanceID();
#endif
}

GameObject IRecyclerViewCellProvider.GetCell(int dataIndex)
{
var prefabKey = _itemInfos[dataIndex].PrefabKey;
Expand All @@ -87,14 +101,14 @@ GameObject IRecyclerViewCellProvider.GetCell(int dataIndex)
}

var obj = pool.Use();
_objIdToPrefabKeyMap[obj.GetInstanceID()] = prefabKey;
_objIdToPrefabKeyMap[GetObjectId(obj)] = prefabKey;
obj.SetActive(true);
return obj;
}

void IRecyclerViewCellProvider.ReleaseCell(GameObject obj)
{
var prefabKey = _objIdToPrefabKeyMap[obj.GetInstanceID()];
var prefabKey = _objIdToPrefabKeyMap[GetObjectId(obj)];
var pool = _prefabPools[prefabKey];
pool.Release(obj);
obj.SetActive(false);
Expand Down
13 changes: 11 additions & 2 deletions Assets/UnityDebugSheet/Runtime/Core/Scripts/DebugSheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;
#if UNITY_6000_5_OR_NEWER
using ObjectId = UnityEngine.EntityId;
#else
using ObjectId = System.Int32;
#endif

namespace UnityDebugSheet.Runtime.Core.Scripts
{
Expand All @@ -17,8 +22,8 @@ public sealed class DebugSheet : MonoBehaviour, IPageContainerCallbackReceiver
{
private const float ThresholdInch = 0.24f;

private static readonly Dictionary<int, DebugSheet> InstanceCacheByTransform =
new Dictionary<int, DebugSheet>();
private static readonly Dictionary<ObjectId, DebugSheet> InstanceCacheByTransform =
new Dictionary<ObjectId, DebugSheet>();

[SerializeField] private bool _singleton = true;

Expand Down Expand Up @@ -197,7 +202,11 @@ public static DebugSheet Of(Transform transform, bool useCache = true)
/// <returns></returns>
public static DebugSheet Of(RectTransform rectTransform, bool useCache = true)
{
#if UNITY_6000_5_OR_NEWER
var id = rectTransform.GetEntityId();
#else
var id = rectTransform.GetInstanceID();
#endif

if (useCache && InstanceCacheByTransform.TryGetValue(id, out var container)) return container;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.UI;
#if UNITY_6000_5_OR_NEWER
using ObjectId = UnityEngine.EntityId;
#else
using ObjectId = System.Int32;
#endif

namespace UnityDebugSheet.Runtime.Foundation.PageNavigator
{
[AddComponentMenu("Scripts/Page Container (Unity Debug Sheet)")]
[RequireComponent(typeof(RectMask2D))]
public sealed class PageContainer : MonoBehaviour
{
private static readonly Dictionary<int, PageContainer> InstanceCacheByTransform =
new Dictionary<int, PageContainer>();
private static readonly Dictionary<ObjectId, PageContainer> InstanceCacheByTransform =
new Dictionary<ObjectId, PageContainer>();

private static readonly Dictionary<string, PageContainer> InstanceCacheByName =
new Dictionary<string, PageContainer>();
Expand Down Expand Up @@ -99,7 +104,7 @@ private void OnDestroy()
_orderedPageIds.Clear();

InstanceCacheByName.Remove(_name);
var keysToRemove = new List<int>();
var keysToRemove = new List<ObjectId>();
foreach (var cache in InstanceCacheByTransform)
if (Equals(cache.Value))
keysToRemove.Add(cache.Key);
Expand Down Expand Up @@ -127,7 +132,11 @@ public static PageContainer Of(Transform transform, bool useCache = true)
/// <returns></returns>
public static PageContainer Of(RectTransform rectTransform, bool useCache = true)
{
#if UNITY_6000_5_OR_NEWER
var id = rectTransform.GetEntityId();
#else
var id = rectTransform.GetInstanceID();
#endif
if (useCache && InstanceCacheByTransform.TryGetValue(id, out var container))
return container;

Expand Down