diff --git a/Scripts/GameApi/AddressableAssets/AssetReferenceLiteNetLibBehaviour.cs b/Scripts/GameApi/AddressableAssets/AssetReferenceLiteNetLibBehaviour.cs index ea28fd1..20b5fbf 100644 --- a/Scripts/GameApi/AddressableAssets/AssetReferenceLiteNetLibBehaviour.cs +++ b/Scripts/GameApi/AddressableAssets/AssetReferenceLiteNetLibBehaviour.cs @@ -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(); + 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 @@ -57,22 +62,17 @@ public override bool SetEditorAsset(Object value) public new AsyncOperationHandle 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); } public new AsyncOperationHandle 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); } public new AsyncOperationHandle LoadAssetAsync() { - return Addressables.ResourceManager.CreateChainOperation(base.LoadAssetAsync(), GetComponentChainOperation); - } - - private static AsyncOperationHandle GetComponentChainOperation(AsyncOperationHandle handler) - { - return Addressables.ResourceManager.CreateCompletedOperation(handler.Result.GetComponent(), string.Empty); + return Addressables.ResourceManager.CreateChainOperation(base.LoadAssetAsync(), AssetReferenceUtils.CreateGetComponentCompletedOperation); } public override bool ValidateAsset(Object obj) diff --git a/Scripts/GameApi/AddressableAssets/AssetReferenceLiteNetLibIdentity.cs b/Scripts/GameApi/AddressableAssets/AssetReferenceLiteNetLibIdentity.cs index 506ebae..3b24088 100644 --- a/Scripts/GameApi/AddressableAssets/AssetReferenceLiteNetLibIdentity.cs +++ b/Scripts/GameApi/AddressableAssets/AssetReferenceLiteNetLibIdentity.cs @@ -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 @@ -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 } } \ No newline at end of file diff --git a/Scripts/GameApi/LiteNetLibIdentity.cs b/Scripts/GameApi/LiteNetLibIdentity.cs index 47f4bf0..8adac00 100644 --- a/Scripts/GameApi/LiteNetLibIdentity.cs +++ b/Scripts/GameApi/LiteNetLibIdentity.cs @@ -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