Skip to content

Commit

Permalink
feat: add hash asset ID validating functions
Browse files Browse the repository at this point in the history
  • Loading branch information
insthync committed Sep 9, 2024
1 parent dc6d6c2 commit 3d54f39
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,21 @@ public AssetReferenceLiteNetLibBehaviour(string guid) : base(guid)
#if UNITY_EDITOR
public AssetReferenceLiteNetLibBehaviour(LiteNetLibBehaviour behaviour) : base(AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(behaviour)))
{
if (behaviour != null && behaviour.TryGetComponent(out LiteNetLibIdentity identity))
if (behaviour == null)
{
hashAssetId = identity.HashAssetId;
Debug.Log($"[AssetReferenceLiteNetLibBehaviour] Set `hashAssetId` to `{hashAssetId}`, name: {behaviour.name}");
hashAssetId = 0;
Debug.LogWarning($"[AssetReferenceLiteNetLibBehaviour] Cannot find behaviour, so set `hashAssetId` to `0`");
return;
}
else
LiteNetLibIdentity identity = behaviour.GetComponentInParent<LiteNetLibIdentity>();
if (identity == null)
{
hashAssetId = 0;
Debug.LogWarning($"[AssetReferenceLiteNetLibBehaviour] Cannot find behaviour, so set `hashAssetId` to `0`");
Debug.LogWarning($"[AssetReferenceLiteNetLibBehaviour] Cannot find identity, so set `hashAssetId` to `0`");
return;
}
hashAssetId = identity.HashAssetId;
Debug.Log($"[AssetReferenceLiteNetLibBehaviour] Set `hashAssetId` to `{hashAssetId}`, name: {behaviour.name}");
}
#endif

Expand Down Expand Up @@ -57,22 +62,17 @@ public override bool SetEditorAsset(Object value)

public new AsyncOperationHandle<TBehaviour> InstantiateAsync(Vector3 position, Quaternion rotation, Transform parent = null)
{
return Addressables.ResourceManager.CreateChainOperation(Addressables.InstantiateAsync(RuntimeKey, position, rotation, parent, false), GetComponentChainOperation);
return Addressables.ResourceManager.CreateChainOperation(Addressables.InstantiateAsync(RuntimeKey, position, rotation, parent, false), AssetReferenceUtils.CreateGetComponentCompletedOperation<TBehaviour>);
}

public new AsyncOperationHandle<TBehaviour> InstantiateAsync(Transform parent = null, bool instantiateInWorldSpace = false)
{
return Addressables.ResourceManager.CreateChainOperation(Addressables.InstantiateAsync(RuntimeKey, parent, instantiateInWorldSpace, false), GetComponentChainOperation);
return Addressables.ResourceManager.CreateChainOperation(Addressables.InstantiateAsync(RuntimeKey, parent, instantiateInWorldSpace, false), AssetReferenceUtils.CreateGetComponentCompletedOperation<TBehaviour>);
}

public new AsyncOperationHandle<TBehaviour> LoadAssetAsync()
{
return Addressables.ResourceManager.CreateChainOperation(base.LoadAssetAsync<GameObject>(), GetComponentChainOperation);
}

private static AsyncOperationHandle<TBehaviour> GetComponentChainOperation(AsyncOperationHandle<GameObject> handler)
{
return Addressables.ResourceManager.CreateCompletedOperation(handler.Result.GetComponent<TBehaviour>(), string.Empty);
return Addressables.ResourceManager.CreateChainOperation(base.LoadAssetAsync<GameObject>(), AssetReferenceUtils.CreateGetComponentCompletedOperation<TBehaviour>);
}

public override bool ValidateAsset(Object obj)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,14 @@ public AssetReferenceLiteNetLibIdentity(string guid) : base(guid)
#if UNITY_EDITOR
public AssetReferenceLiteNetLibIdentity(LiteNetLibIdentity identity) : base(AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(identity)))
{
if (identity != null)
{
hashAssetId = identity.HashAssetId;
Debug.Log($"[AssetReferenceLiteNetLibIdentity] Set `hashAssetId` to `{hashAssetId}`, name: {identity.name}");
}
else
if (identity == null)
{
hashAssetId = 0;
Debug.LogWarning($"[AssetReferenceLiteNetLibIdentity] Cannot find identity, so set `hashAssetId` to `0`");
return;
}
hashAssetId = identity.HashAssetId;
Debug.Log($"[AssetReferenceLiteNetLibBehaviour] Set `hashAssetId` to `{hashAssetId}`, name: {identity.name}");
}
#endif

Expand All @@ -58,6 +56,31 @@ public override bool SetEditorAsset(Object value)
return false;
}
}

public virtual bool ValidateHashAssetID()
{
GameObject editorAsset = this.editorAsset as GameObject;
if (!editorAsset)
{
return false;
}
int newHashAssetId;
if (editorAsset.TryGetComponent(out LiteNetLibIdentity identity))
{
newHashAssetId = identity.HashAssetId;
}
else
{
return false;
}
if (hashAssetId == newHashAssetId)
{
return false;
}
hashAssetId = newHashAssetId;
Debug.Log($"Hash asset ID validated, hash asset ID changed to {hashAssetId}");
return true;
}
#endif
}
}
5 changes: 5 additions & 0 deletions Scripts/GameApi/LiteNetLibIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ public int HashAssetId
{
get
{
if (!Application.isPlaying)
{
// Not playing yet, maybe in editor, so force reset hash asset ID
_hashAssetId = null;
}
if (!_hashAssetId.HasValue)
{
unchecked
Expand Down

0 comments on commit 3d54f39

Please sign in to comment.