guodong 1 год назад
Родитель
Сommit
9f21fca5c3

+ 4 - 2
GameClient/Assets/Game/HotUpdate/Data/PhotographSceneManager.cs

@@ -114,8 +114,10 @@ namespace GFGGame
                 tf.gameObject.AddComponent<SpriteRenderer>();
                 spr = tf.GetComponent<SpriteRenderer>();
             }
-            DressUpUtil.ChangeAssetReleaser(tf.gameObject, resPath);
-            Sprite sp = GFGAsset.Load<Sprite>(resPath);
+            //Sprite sp = GFGAsset.Load<Sprite>(resPath);
+            var handle = YooAssets.LoadAssetSync<Sprite>(resPath);
+            Sprite sp = handle.AssetObject as Sprite;
+            DressUpUtil.AddAssetReleaser(tf.gameObject, resPath, handle);
             if (sp == null) return;
             spr.sprite = sp;
             spr.size = spr.sprite.bounds.size;//将节点设置为原图大小

+ 27 - 24
GameClient/Assets/Game/HotUpdate/DressUp/DressUpUtil.cs

@@ -207,20 +207,14 @@ namespace GFGGame
             }
         }
 
-        public static void AddAssetReleaser(GameObject gameObj, string resPath)
-        {
-            var assetDisposer = gameObj.AddComponent<AssetReleaser>();
-            assetDisposer.resPath = resPath;
-        }
-
-        public static void ChangeAssetReleaser(GameObject gameObj, string resPath)
+        public static void AddAssetReleaser(GameObject gameObj, string resPath, AssetOperationHandle handle)
         {
             var assetDisposer = gameObj.GetComponent<AssetReleaser>();
             if (assetDisposer == null)
             {
                 assetDisposer = gameObj.AddComponent<AssetReleaser>();
             }
-            assetDisposer.resPath = resPath;
+            assetDisposer.SetRes(resPath, handle);
         }
 
         private static void updateLayerRes(ItemCfg itemCfg, GameObject parentObj, int layerId, bool needSetMask, bool showAni = true)
@@ -316,7 +310,6 @@ namespace GFGGame
             {
                 gameObj = new GameObject(objName);
                 gameObj.transform.SetParent(parentObj.transform, false);
-                AddAssetReleaser(gameObj, resPath);
             }
             spr = gameObj.GetComponent<SpriteRenderer>();
             if (spr == null)
@@ -326,7 +319,10 @@ namespace GFGGame
             float tx, ty;
             LoadSpritePos(resPath, out tx, out ty);
             gameObj.transform.localPosition = new Vector3(tx, ty, gameObj.transform.localPosition.z);
-            Sprite sp = GFGAsset.Load<Sprite>(resPath);
+            //Sprite sp = GFGAsset.Load<Sprite>(resPath);
+            var handle = YooAssets.LoadAssetSync<Sprite>(resPath);
+            Sprite sp = handle.AssetObject as Sprite;
+            AddAssetReleaser(gameObj, resPath, handle);
             spr.sprite = sp;
             spr.sortingOrder = sortingOrder;
 
@@ -462,29 +458,36 @@ namespace GFGGame
             {
                 return null;
             }
+            //var handle = YooAssets.LoadAssetSync<Sprite>(resPath);
+            //Sprite sp = handle.AssetObject as Sprite;
             var gameObj = GFGAsset.Load<GameObject>(resPath);
-            AddAssetReleaser(gameObj, resPath);
+            //AddAssetReleaser(gameObj, resPath);
             return gameObj;
         }
 
         public static void LoadSpritePos(string resPath, out float tx, out float ty)
         {
-            resPath = resPath.Replace(".png", ".bytes");
+            int index = resPath.LastIndexOf('.');
+            resPath = resPath.Substring(0, index) + ".bytes";
+            //resPath = resPath.Replace(".png", ".bytes");
+            //resPath = resPath.Replace(".jpg", ".bytes");
             if (YooAssets.CheckDressUpResExist(resPath))
             {
-                var asset = GFGAsset.Load<TextAsset>(resPath);
-                if (asset != null)
-                {
-                    var st = new MemoryStream(asset.bytes);
-                    var br = new BinaryReader(st);
-                    tx = br.ReadInt32() / 100f;
-                    ty = -br.ReadInt32() / 100f;
-                    GFGAsset.Release(resPath);
-                    return;
-                }
+                //var asset = GFGAsset.Load<TextAsset>(resPath);
+                var handle = YooAssets.LoadAssetSync<TextAsset>(resPath);
+                TextAsset asset = handle.AssetObject as TextAsset;
+                var st = new MemoryStream(asset.bytes);
+                var br = new BinaryReader(st);
+                tx = br.ReadInt32() / 100f;
+                ty = -br.ReadInt32() / 100f;
+                //GFGAsset.Release(resPath);
+                handle.Release();
+            }
+            else
+            {
+                tx = 0;
+                ty = 0;
             }
-            tx = 0;
-            ty = 0;
         }
 
         public static void SetRenderersOrder(GameObject gameObj, int sortingOrder, bool isAdd = false)

+ 25 - 12
GameClient/Assets/Game/HotUpdate/DressUp/SceneController.cs

@@ -1,6 +1,7 @@
 using UnityEngine;
 
 using System.Collections;
+using YooAsset;
 
 namespace GFGGame
 {
@@ -12,8 +13,10 @@ namespace GFGGame
             Transform tf = sceneObj.transform.Find("Bg");
             SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
             var resPath = ResPathUtil.GetSceneBgPath("jhsy_bg");
-            Sprite sp = GFGAsset.Load<Sprite>(resPath);
-            DressUpUtil.AddAssetReleaser(tf.gameObject, resPath);
+            //Sprite sp = GFGAsset.Load<Sprite>(resPath);
+            var handle = YooAssets.LoadAssetSync<Sprite>(resPath);
+            Sprite sp = handle.AssetObject as Sprite;
+            DressUpUtil.AddAssetReleaser(tf.gameObject, resPath, handle);
             spr.sprite = sp;
             //角色
             DressUpUtil.UpdateBody("ui_loginrole", sceneObj);
@@ -25,15 +28,19 @@ namespace GFGGame
             Transform tf = sceneObj.transform.Find("Bg");
             SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
             var resPath = ResPathUtil.GetBgImgPath("zjm_1");
-            Sprite sp = GFGAsset.Load<Sprite>(resPath);
-            DressUpUtil.AddAssetReleaser(tf.gameObject, resPath);
+            //Sprite sp = GFGAsset.Load<Sprite>(resPath);
+            var handle = YooAssets.LoadAssetSync<Sprite>(resPath);
+            Sprite sp = handle.AssetObject as Sprite;
+            DressUpUtil.AddAssetReleaser(tf.gameObject, resPath, handle);
             spr.sprite = sp;
             //背景1
             Transform tf1 = sceneObj.transform.Find("Bg1");
             SpriteRenderer spr1 = tf1.GetComponent<SpriteRenderer>();
             var resPath1 = ResPathUtil.GetBgImgPath("fuben_bg");
-            Sprite sp1 = GFGAsset.Load<Sprite>(resPath1);
-            DressUpUtil.AddAssetReleaser(tf1.gameObject, resPath);
+            //Sprite sp1 = GFGAsset.Load<Sprite>(resPath1);
+            handle = YooAssets.LoadAssetSync<Sprite>(resPath);
+            Sprite sp1 = handle.AssetObject as Sprite;
+            DressUpUtil.AddAssetReleaser(tf1.gameObject, resPath, handle);
             spr1.sprite = sp1;
             //角色
             CustomSuitData suitSavedData = CustomSuitDataManager.GetCurrentSuitData();
@@ -55,8 +62,10 @@ namespace GFGGame
             else
             {
                 var resPath = ResPathUtil.GetSceneBgPath(value);
-                Sprite sp = GFGAsset.Load<Sprite>(resPath);
-                DressUpUtil.AddAssetReleaser(bg.gameObject, resPath);
+                //Sprite sp = GFGAsset.Load<Sprite>(resPath);
+                var handle = YooAssets.LoadAssetSync<Sprite>(resPath);
+                Sprite sp = handle.AssetObject as Sprite;
+                DressUpUtil.AddAssetReleaser(bg.gameObject, resPath, handle);
 
                 if (bgSpr.sprite == null)
                 {
@@ -113,8 +122,10 @@ namespace GFGGame
                 StoryDialogDataManager.Instance.waitPicFade = true;
 
                 var resPath = ResPathUtil.GetNpcPicSPath(value);
-                Sprite sp = GFGAsset.Load<Sprite>(resPath);
-                DressUpUtil.AddAssetReleaser(tf.gameObject, resPath);
+                //Sprite sp = GFGAsset.Load<Sprite>(resPath);
+                var handle = YooAssets.LoadAssetSync<Sprite>(resPath);
+                Sprite sp = handle.AssetObject as Sprite;
+                DressUpUtil.AddAssetReleaser(tf.gameObject, resPath, handle);
                 spr.sprite = sp;
                 SetSpriteRendererAlpha(spr, 0f);
                 FairyGUI.Timers.inst.StartCoroutine(UpdateDialogPicAlpha());
@@ -157,8 +168,10 @@ namespace GFGGame
             Transform tf = sceneObj.transform.Find("Npc");
             SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
             var resPath = ResPathUtil.GetNpcPicFPath(value);
-            Sprite sp = GFGAsset.Load<Sprite>(resPath);
-            DressUpUtil.AddAssetReleaser(tf.gameObject, resPath);
+            //Sprite sp = GFGAsset.Load<Sprite>(resPath);
+            var handle = YooAssets.LoadAssetSync<Sprite>(resPath);
+            Sprite sp = handle.AssetObject as Sprite;
+            DressUpUtil.AddAssetReleaser(tf.gameObject, resPath, handle);
             spr.sprite = sp;
         }
 

+ 32 - 6
GameClient/Assets/Game/Launcher/FairyGUI/GFGGLoader.cs

@@ -1,23 +1,49 @@
 using UnityEngine;
 using FairyGUI;
+using YooAsset;
 
 namespace GFGGame
 {
     public class GFGGLoader : GLoader
     {
+        private AssetOperationHandle handle;
+
         protected override void LoadExternal()
         {
-            Texture2D texture = GFGAsset.Load<Texture2D>(this.url);
-            if (texture != null)
-                onExternalLoadSuccess(new NTexture(texture));
-            else
-                onExternalLoadFailed();
+            //Texture2D texture = GFGAsset.Load<Texture2D>(this.url);
+            handle = YooAssets.LoadAssetAsync<Texture2D>(this.url);
+            handle.Completed += Handle_Completed;
+            //if (texture != null)
+            //    onExternalLoadSuccess(new NTexture(texture));
+            //else
+            //    onExternalLoadFailed();
         }
 
         protected override void FreeExternal(NTexture texture)
         {
             //释放外部载入的资源
-            GFGAsset.Release(this.url);
+            //GFGAsset.Release(this.url);
+            if(handle != null)
+            {
+                handle.Release();
+            }
+        }
+        void Handle_Completed(AssetOperationHandle handle)
+        {
+            if(handle.GetAssetInfo().AssetPath != this.url)
+            {
+                handle.Release();
+                return;
+            }
+            Texture2D texture = handle.AssetObject as Texture2D;
+            if (texture != null)
+            {
+                onExternalLoadSuccess(new NTexture(texture));
+            }
+            else
+            {
+                onExternalLoadFailed();
+            }
         }
     }
 }

+ 14 - 5
GameClient/Assets/Game/Launcher/HotUpdateProxy/HotUpdateCodeLoader.cs

@@ -6,6 +6,7 @@ using GFGGame.Launcher;
 using System.Collections.Generic;
 using HybridCLR;
 using System.Linq;
+using YooAsset;
 
 namespace GFGGame
 {
@@ -32,7 +33,6 @@ namespace GFGGame
         {
 
 #if !UNITY_EDITOR
-            LoadMetadataForAOTAssemblies();
             StartCoroutine(StartLoadAssemblyHotfix());
 #else
             var gameAss = AppDomain.CurrentDomain.GetAssemblies().First(assembly => assembly.GetName().Name == "Game.HotUpdate");
@@ -44,15 +44,20 @@ namespace GFGGame
 
         IEnumerator StartLoadAssemblyHotfix()
         {
+            yield return LoadMetadataForAOTAssemblies();
             LogServerHelperHttp.SendNodeLog((int)LogNode.StartLoadGameDll);
             //ET.Log.Debug("ILRuntimeLauncher StartLoadAssemblyHotfix");
             yield return new WaitForSeconds(0.1f);
             var dllPath = $"{LauncherConfig.DllDirHotfix}Game.HotUpdate.dll.bytes";
-            var asset = GFGAsset.Load<TextAsset>(dllPath);
+            //var asset = GFGAsset.Load<TextAsset>(dllPath);
+            var handle = YooAssets.LoadAssetAsync<TextAsset>(dllPath);
+            yield return handle;
+            var asset = handle.AssetObject as TextAsset;
             byte[] assBytes = asset.bytes;
             var assembly = Assembly.Load(assBytes);
             LoadAssemblyJustInTime(assembly);
-            GFGAsset.Release(dllPath);
+            //GFGAsset.Release(dllPath);
+            handle.Release();
         }
 
         void LoadAssemblyJustInTime(Assembly assembly)
@@ -69,7 +74,7 @@ namespace GFGGame
         /// 为aot assembly加载原始metadata, 这个代码放aot或者热更新都行。
         /// 一旦加载后,如果AOT泛型函数对应native实现不存在,则自动替换为解释模式执行
         /// </summary>
-        private void LoadMetadataForAOTAssemblies()
+        IEnumerator LoadMetadataForAOTAssemblies()
         {
             // 可以加载任意aot assembly的对应的dll。但要求dll必须与unity build过程中生成的裁剪后的dll一致,而不能直接使用原始dll。
             // 我们在BuildProcessors里添加了处理代码,这些裁剪后的dll在打包时自动被复制到 {项目目录}/HybridCLRData/AssembliesPostIl2CppStrip/{Target} 目录。
@@ -81,10 +86,14 @@ namespace GFGGame
             foreach (var aotDllName in AOTMetaAssemblyNames)
             {
                 var dllPath = $"{LauncherConfig.DllDirAOT}{aotDllName}.bytes";
-                var asset = GFGAsset.Load<TextAsset>(dllPath);
+                //var asset = GFGAsset.Load<TextAsset>(dllPath);
+                var handle = YooAssets.LoadAssetAsync<TextAsset>(dllPath);
+                yield return handle;
+                var asset = handle.AssetObject as TextAsset;
                 byte[] dllBytes = asset.bytes;
                 // 加载assembly对应的dll,会自动为它hook。一旦aot泛型函数的native函数不存在,用解释器版本代码
                 LoadImageErrorCode err = RuntimeApi.LoadMetadataForAOTAssembly(dllBytes, mode);
+                handle.Release();
                 Debug.Log($"LoadMetadataForAOTAssembly:{aotDllName}. mode:{mode} ret:{err}");
             }
         }

+ 1 - 1
GameClient/Assets/Game/Launcher/Version/VersionController.cs

@@ -119,7 +119,7 @@ namespace GFGGame
             int downloadingMaxNum = 10;
             int failedTryAgain = 3;
             ResourcePackage package = YooAssets.GetPackage(packageName);
-            var downloaderOperation = package.CreateResourceDownloader(new string[] { "preload" }, downloadingMaxNum, failedTryAgain);
+            var downloaderOperation = package.CreateResourceDownloader(new string[] { "preload", "dynamic" }, downloadingMaxNum, failedTryAgain);
 
             if (downloaderOperation.TotalDownloadCount == 0)
             {

+ 0 - 4
GameClient/Assets/Game/Launcher/Views/LauncherView.cs

@@ -82,9 +82,6 @@ namespace GFGGame
 
             var prefab = Resources.Load<GameObject>(resPath);
             _gameObject = GameObject.Instantiate(prefab);
-            // var assetDisposer = 
-            _gameObject.AddComponent<AssetReleaser>().resPath = resPath;
-            // assetDisposer.resPath = resPath;
             _gameObject.transform.localScale = new Vector3(100, 100, 100);
             _wrapper = new GoWrapper(_gameObject);
             _ui.m_progressBar1.m_holder.SetNativeObject(_wrapper);
@@ -92,7 +89,6 @@ namespace GFGGame
             string resPath1 = "Effect/ui_dljm/Game_Open_jindut";
             var prefab1 = Resources.Load<GameObject>(resPath1);
             _gameObject1 = GameObject.Instantiate(prefab1);
-            _gameObject1.AddComponent<AssetReleaser>().resPath = resPath1;
             _gameObject1.transform.localScale = new Vector3(100, 100, 100);
             _wrapper1 = new GoWrapper(_gameObject1);
             _bar = _ui.m_progressBar1.target.GetChild("bar").asCom;

+ 23 - 10
GameClient/Assets/Game/Launcher/Xasset/AssetReleaser.cs

@@ -1,5 +1,6 @@
 using System.Collections;
 using UnityEngine;
+using YooAsset;
 
 namespace GFGGame
 {
@@ -13,20 +14,32 @@ namespace GFGGame
             {
                 return _resPath;
             }
-            set
-            {
-                if (_resPath == value) return;
-                this.OnDestroy();
-                _resPath = value;
-            }
         }
+
+        private AssetOperationHandle _handle;
+
+        public void SetRes(string resPath, AssetOperationHandle handle)
+        {
+            Dispose();
+            this._resPath = resPath;
+            this._handle = handle;
+        }
+
         private void OnDestroy()
         {
-            if (!string.IsNullOrEmpty(resPath))
-            {
-                GFGAsset.Release(resPath);
-            }
+            //if (!string.IsNullOrEmpty(resPath))
+            //{
+            //    GFGAsset.Release(resPath);
+            //}
             // resPath = string.Empty;
+            Dispose();
+        }
+
+        private void Dispose()
+        {
+            this._handle?.Release();
+            this._handle = null;
+            this._resPath = null;
         }
     }
 }