From 73e2a2633c79ac0813f5bf86103901d299f6a68a Mon Sep 17 00:00:00 2001 From: hevinci Date: Mon, 26 Jun 2023 18:31:37 +0800 Subject: [PATCH] update extension sample MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 增加了GameObjectAssetReference示例脚本。 --- .../Runtime/GameObjectAssetReference.cs | 73 +++++++++++++++++++ .../Runtime/GameObjectAssetReference.cs.meta | 11 +++ 2 files changed, 84 insertions(+) create mode 100644 Assets/YooAsset/Samples~/Extension Sample/Scripts/Runtime/GameObjectAssetReference.cs create mode 100644 Assets/YooAsset/Samples~/Extension Sample/Scripts/Runtime/GameObjectAssetReference.cs.meta diff --git a/Assets/YooAsset/Samples~/Extension Sample/Scripts/Runtime/GameObjectAssetReference.cs b/Assets/YooAsset/Samples~/Extension Sample/Scripts/Runtime/GameObjectAssetReference.cs new file mode 100644 index 0000000..0092ae4 --- /dev/null +++ b/Assets/YooAsset/Samples~/Extension Sample/Scripts/Runtime/GameObjectAssetReference.cs @@ -0,0 +1,73 @@ +using UnityEngine; +using YooAsset; + +#if UNITY_EDITOR +[UnityEditor.CustomEditor(typeof(GameObjectAssetReference), true)] +public class GameObjectAssetReferenceInspector : UnityEditor.Editor +{ + private bool _init = false; + private GameObject _cacheObject; + + public override void OnInspectorGUI() + { + GameObjectAssetReference mono = (GameObjectAssetReference)target; + + if (_init == false) + { + _init = true; + if (string.IsNullOrEmpty(mono.AssetGUID) == false) + { + string assetPath = UnityEditor.AssetDatabase.GUIDToAssetPath(mono.AssetGUID); + if (string.IsNullOrEmpty(assetPath) == false) + { + _cacheObject = UnityEditor.AssetDatabase.LoadAssetAtPath(assetPath); + } + } + } + + GameObject go = (GameObject)UnityEditor.EditorGUILayout.ObjectField(_cacheObject, typeof(GameObject), false); + if (go != _cacheObject) + { + _cacheObject = go; + string assetPath = UnityEditor.AssetDatabase.GetAssetPath(go); + mono.AssetGUID = UnityEditor.AssetDatabase.AssetPathToGUID(assetPath); + UnityEditor.EditorUtility.SetDirty(target); + } + + UnityEditor.EditorGUILayout.LabelField("Asset GUID", mono.AssetGUID); + } +} +#endif + + +public class GameObjectAssetReference : MonoBehaviour +{ + [HideInInspector] + public string AssetGUID = ""; + + private AssetOperationHandle _handle; + + public void Start() + { + var package = YooAssets.GetPackage("DefaultPackage"); + var assetInfo = package.GetAssetInfoByGUID(AssetGUID); + _handle = package.LoadAssetAsync(assetInfo); + _handle.Completed += Handle_Completed; + } + public void OnDestroy() + { + if (_handle != null) + { + _handle.Release(); + _handle = null; + } + } + + private void Handle_Completed(AssetOperationHandle handle) + { + if (handle.Status == EOperationStatus.Succeed) + { + handle.InstantiateSync(this.transform); + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/Extension Sample/Scripts/Runtime/GameObjectAssetReference.cs.meta b/Assets/YooAsset/Samples~/Extension Sample/Scripts/Runtime/GameObjectAssetReference.cs.meta new file mode 100644 index 0000000..062d368 --- /dev/null +++ b/Assets/YooAsset/Samples~/Extension Sample/Scripts/Runtime/GameObjectAssetReference.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1534f1a1b207ad542bf1fc73da8b4316 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: