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
4 changes: 3 additions & 1 deletion Packages/src/Runtime/Internal/Utilities/Misc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ internal static class Misc
{
public static T[] FindObjectsOfType<T>() where T : Object
{
#if UNITY_2023_1_OR_NEWER
#if UNITY_6000_4_OR_NEWER
return Object.FindObjectsByType<T>(FindObjectsInactive.Include);
#elif UNITY_2023_1_OR_NEWER
return Object.FindObjectsByType<T>(FindObjectsInactive.Include, FindObjectsSortMode.None);
#else
return Object.FindObjectsOfType<T>();
Expand Down
16 changes: 16 additions & 0 deletions Packages/src/Runtime/Internal/Utilities/ObjectRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ namespace Coffee.UIEffectInternal
internal class ObjectRepository<T> where T : Object
{
private readonly Dictionary<Hash128, Entry> _cache = new Dictionary<Hash128, Entry>(8);
#if UNITY_6000_4_OR_NEWER
private readonly Dictionary<EntityId, Hash128> _objectKey = new Dictionary<EntityId, Hash128>(8);
#else
private readonly Dictionary<int, Hash128> _objectKey = new Dictionary<int, Hash128>(8);
#endif
private readonly string _name;
private readonly Action<T> _onRelease;
private readonly Stack<Entry> _pool = new Stack<Entry>(8);
Expand Down Expand Up @@ -130,7 +134,11 @@ private void Add(Hash128 hash, ref T obj, T newObject)
newEntry.hash = hash;
newEntry.reference = 1;
_cache[hash] = newEntry;
#if UNITY_6000_4_OR_NEWER
_objectKey[newObject.GetEntityId()] = hash;
#else
_objectKey[newObject.GetInstanceID()] = hash;
#endif
Logging.Log(_name, $"<color=#03c700>Add</color>(total#{count}): {newEntry}");
Release(ref obj);
obj = newObject;
Expand All @@ -146,7 +154,11 @@ public void Release(ref T obj)

// Find and release the entry.
Profiler.BeginSample("(COF)[ObjectRepository] Release");
#if UNITY_6000_4_OR_NEWER
var id = obj.GetEntityId();
#else
var id = obj.GetInstanceID();
#endif
if (_objectKey.TryGetValue(id, out var hash)
&& _cache.TryGetValue(hash, out var entry))
{
Expand Down Expand Up @@ -175,7 +187,11 @@ private void Remove(Entry entry)

Profiler.BeginSample("(COF)[ObjectRepository] Remove");
_cache.Remove(entry.hash);
#if UNITY_6000_4_OR_NEWER
_objectKey.Remove(entry.storedObject.GetEntityId());
#else
_objectKey.Remove(entry.storedObject.GetInstanceID());
#endif
_pool.Push(entry);
entry.reference = 0;
Logging.Log(_name, $"<color=#f29e03>Remove</color>(total#{_cache.Count}): {entry}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ public override int GetHashCode()
}
}

#if UNITY_6000_4_OR_NEWER
private Dictionary<EntityId, string> _cachedOptionalShaders = new Dictionary<EntityId, string>();
#else
private Dictionary<int, string> _cachedOptionalShaders = new Dictionary<int, string>();
#endif

[SerializeField]
private List<StringPair> m_OptionalShaders = new List<StringPair>();
Expand All @@ -71,7 +75,11 @@ public Shader FindOptionalShader(Shader shader,
if (!shader) return null;

// Already cached.
#if UNITY_6000_4_OR_NEWER
var id = shader.GetEntityId();
#else
var id = shader.GetInstanceID();
#endif
if (_cachedOptionalShaders.TryGetValue(id, out var optionalShaderName))
{
return Shader.Find(optionalShaderName);
Expand Down Expand Up @@ -270,7 +278,13 @@ internal void RegisterVariant(Material material, string path)
_sb.Length--; // Remove last space.
}

var hash = new Hash128((uint)shader.GetInstanceID(), (uint)GetContentsHash(_sb), 0, 0);
#if UNITY_6000_4_OR_NEWER
ulong shaderIdFull = EntityId.ToULong(shader.GetEntityId());
uint shaderId = (uint)(shaderIdFull ^ (shaderIdFull >>32));
#else
uint shaderId = (uint)shader.GetInstanceID();
#endif
var hash = new Hash128(shaderId, (uint)GetContentsHash(_sb), 0, 0);
if (_cachedVariants.TryGetValue(hash, out var result))
{
Profiler.EndSample();
Expand Down
19 changes: 17 additions & 2 deletions Packages/src/Runtime/UIEffectBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ public abstract class UIEffectBase : UIBehaviour, IMeshModifier, IMaterialModifi

public Material effectMaterial => _material;
public Graphic graphic => _graphic ? _graphic : _graphic = GetComponent<Graphic>();
#if UNITY_6000_4_OR_NEWER
public virtual EntityId effectId => GetEntityId();
#else
public virtual uint effectId => (uint)GetInstanceID();
#endif
public virtual float actualSamplingScale => 1;
public virtual bool canModifyShape => true;

Expand Down Expand Up @@ -188,8 +192,19 @@ public virtual Material GetModifiedMaterial(Material baseMaterial)

Profiler.BeginSample("(UIE)[UIEffect] GetModifiedMaterial");
var samplingScaleId = (uint)(Mathf.InverseLerp(0.01f, 100, actualSamplingScale) * uint.MaxValue);
var rootId = (uint)(transitionRoot ? transitionRoot.GetInstanceID() : 0);
var hash = new Hash128((uint)baseMaterial.GetInstanceID(), effectId, samplingScaleId, rootId);
#if UNITY_6000_4_OR_NEWER
ulong matIdFull = EntityId.ToULong(baseMaterial.GetEntityId());
uint matId = (uint)(matIdFull ^ (matIdFull >> 32));
ulong rootIdFull = transitionRoot ? EntityId.ToULong(transitionRoot.GetEntityId()) : 0;
uint rootId = (uint)(rootIdFull ^ (rootIdFull >> 32));
ulong effIdFull = EntityId.ToULong(effectId);
uint effId = (uint)(effIdFull ^ (effIdFull >>32));
var hash = new Hash128(matId, effId, samplingScaleId, rootId);
#else
uint matId = (uint)baseMaterial.GetInstanceID();
uint rootId = transitionRoot ? (uint)transitionRoot.GetInstanceID() : 0;
var hash = new Hash128(matId, effectId, samplingScaleId, rootId);
#endif
if (!MaterialRepository.Valid(hash, _material))
{
Profiler.BeginSample("(UIE)[UIEffect] GetModifiedMaterial > Get or create material");
Expand Down
10 changes: 9 additions & 1 deletion Packages/src/Runtime/UIEffectReplica.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Linq;
using System.Linq;
using Coffee.UIEffectInternal;
using UnityEngine;

Expand Down Expand Up @@ -99,11 +99,19 @@ public bool allowToModifyMeshShape

public override bool canModifyShape => m_AllowToModifyMeshShape;

#if UNITY_6000_4_OR_NEWER
public override EntityId effectId => target
? target.effectId
: preset
? preset.GetEntityId()
: GetEntityId();
#else
public override uint effectId => target
? target.effectId
: preset
? (uint)preset.GetInstanceID()
: (uint)GetInstanceID();
#endif


public override UIEffectContext context
Expand Down
Loading