From 69b9c1cc100a6a065aa8e7aa7de60d0ae90bd9b6 Mon Sep 17 00:00:00 2001 From: shenjianxing <”315615051@qq.com“> Date: Mon, 16 Dec 2024 13:25:22 +0800 Subject: [PATCH] =?UTF-8?q?xml=E9=85=8D=E7=BD=AE=E4=BB=8Eab=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E4=B8=AD=E7=A7=BB=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ResKit/Scripts/Framework/ResFactory.cs | 15 +- .../ResKit/Scripts/LocalTextSupport.meta | 8 + .../LocalTextSupport/LocalTextSupport.cs | 278 ++++++++++++++++++ .../LocalTextSupport/LocalTextSupport.cs.meta | 11 + Assets/Scripts/Global/Global.cs | 7 +- Assets/Scripts/Launch.cs | 5 +- 6 files changed, 318 insertions(+), 6 deletions(-) create mode 100644 Assets/QFramework/Toolkits/ResKit/Scripts/LocalTextSupport.meta create mode 100644 Assets/QFramework/Toolkits/ResKit/Scripts/LocalTextSupport/LocalTextSupport.cs create mode 100644 Assets/QFramework/Toolkits/ResKit/Scripts/LocalTextSupport/LocalTextSupport.cs.meta diff --git a/Assets/QFramework/Toolkits/ResKit/Scripts/Framework/ResFactory.cs b/Assets/QFramework/Toolkits/ResKit/Scripts/Framework/ResFactory.cs index 9dd09224..9928f067 100644 --- a/Assets/QFramework/Toolkits/ResKit/Scripts/Framework/ResFactory.cs +++ b/Assets/QFramework/Toolkits/ResKit/Scripts/Framework/ResFactory.cs @@ -73,7 +73,8 @@ namespace QFramework AssetBundleSceneResCreator, new NetImageResCreator(), new LocalImageResCreator(), - new LocalAudioResCreator() + new LocalAudioResCreator(), + new LocalTextResCreator() }; } @@ -114,4 +115,16 @@ namespace QFramework return LocalAudioRes.Allocate(resSearchKeys.AssetName); } } + public class LocalTextResCreator : IResCreator + { + public bool Match(ResSearchKeys resSearchKeys) + { + return resSearchKeys.AssetName.StartsWith("localtext:"); + } + + public IRes Create(ResSearchKeys resSearchKeys) + { + return LocalTextRes.Allocate(resSearchKeys.AssetName); + } + } } \ No newline at end of file diff --git a/Assets/QFramework/Toolkits/ResKit/Scripts/LocalTextSupport.meta b/Assets/QFramework/Toolkits/ResKit/Scripts/LocalTextSupport.meta new file mode 100644 index 00000000..07a0bda9 --- /dev/null +++ b/Assets/QFramework/Toolkits/ResKit/Scripts/LocalTextSupport.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6012cdb558a59ef4db3dd78e36951e0f +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/QFramework/Toolkits/ResKit/Scripts/LocalTextSupport/LocalTextSupport.cs b/Assets/QFramework/Toolkits/ResKit/Scripts/LocalTextSupport/LocalTextSupport.cs new file mode 100644 index 00000000..52f1e4ed --- /dev/null +++ b/Assets/QFramework/Toolkits/ResKit/Scripts/LocalTextSupport/LocalTextSupport.cs @@ -0,0 +1,278 @@ +using UnityEngine; + +namespace QFramework +{ + using UnityEngine; + using System.Collections; + using UnityEngine.Networking; + + public static class LocalTextResUtil + { + public static string ToLocalTextResName(this string selfFilePath) + { + return string.Format("localtext:{0}", selfFilePath); + } + } + + /// + /// ͨļ + /// + public class LocalTextRes : Res + { + private string mFullPath; + private string mHashCode; + private object mRawAsset; + + UnityWebRequest request; + public static LocalTextRes Allocate(string name) + { + var res = SafeObjectPool.Instance.Allocate(); + if (res != null) + { + res.AssetName = name; + res.SetUrl(name.Substring(10)); + } + return res; + } + + public void SetDownloadProgress(int totalSize, int download) + { + + } + + public string LocalResPath + { + get { return string.Format("{0}{1}", AssetBundlePathHelper.PersistentDataPath4Photo, mHashCode); } + } + + public virtual object RawAsset + { + get { return mRawAsset; } + } + + public bool NeedDownload + { + get { return RefCount > 0; } + } + + public string Url + { + get { return mFullPath; } + } + + public int FileSize + { + get { return 1; } + } + + public void SetUrl(string url) + { + if (string.IsNullOrEmpty(url)) + { + return; + } + + mFullPath = url; + mHashCode = string.Format("Photo_{0}", mFullPath.GetHashCode()); + } + + public override bool UnloadImage(bool flag) + { + return false; + } + + public override bool LoadSync() + { + return false; + } + + public override void LoadAsync() + { + if (!CheckLoadAble()) + { + return; + } + + if (string.IsNullOrEmpty(mAssetName)) + { + return; + } + + DoLoadWork(); + } + + private void DoLoadWork() + { + State = ResState.Loading; + + OnDownLoadResult(true); + + //ⱾļǷ + /* + if (!File.Exists(LocalResPath)) + { + ResDownloader.S.AddDownloadTask(this); + } + else + { + OnDownLoadResult(true); + } + */ + } + + protected override void OnReleaseRes() + { + if (mAsset != null) + { + GameObject.Destroy(mAsset); + mAsset = null; + } + + mRawAsset = null; + } + + public override void Recycle2Cache() + { + SafeObjectPool.Instance.Recycle(this); + } + + public override void OnRecycled() + { + + } + + public void DeleteOldResFile() + { + //throw new NotImplementedException(); + } + + public void OnDownLoadResult(bool result) + { + if (!result) + { + OnResLoadFaild(); + return; + } + + if (RefCount <= 0) + { + State = ResState.Waiting; + return; + } + + ResMgr.Instance.PushIEnumeratorTask(this); + } + + + public override IEnumerator DoLoadAsync(System.Action finishCallback) + { + using (UnityWebRequest request = UnityWebRequest.Get(mFullPath)) + { + request.downloadHandler = new DownloadHandlerBuffer(); + yield return request.SendWebRequest(); + + if (request.result != UnityWebRequest.Result.Success) + { + Debug.LogError(string.Format("Res:{0}, WWW Errors:{1}", mFullPath, request.error)); + OnResLoadFaild(); + finishCallback(); + yield break; + } + else + { + // Convert the downloaded data to an AudioClip + mAsset = new StringAsset(request.downloadHandler.text); + } + + if (RefCount <= 0) + { + OnResLoadFaild(); + finishCallback(); + yield break; + } + } + + State = ResState.Ready; + + finishCallback(); + } + + protected override float CalculateProgress() + { + if (request == null) + { + return 0; + } + + return request.downloadProgress; + } + + /* + public IEnumerator StartIEnumeratorTask2(Action finishCallback) + { + if (refCount <= 0) + { + OnResLoadFaild(); + finishCallback(); + yield break; + } + + WWW www = new WWW("file://" + LocalResPath); + yield return www; + if (www.error != null) + { + Log.E("WWW Error:" + www.error); + OnResLoadFaild(); + finishCallback(); + yield break; + } + + if (!www.isDone) + { + Log.E("NetImageRes WWW Not Done! Url:" + m_Url); + OnResLoadFaild(); + finishCallback(); + + www.Dispose(); + www = null; + + yield break; + } + + if (refCount <= 0) + { + OnResLoadFaild(); + finishCallback(); + + www.Dispose(); + www = null; + yield break; + } + + TimeDebugger dt = new TimeDebugger("Tex"); + dt.Begin("LoadingTask"); + Texture2D tex = www.texture; + tex.Compress(true); + dt.End(); + dt.Dump(-1); + + m_Asset = tex; + www.Dispose(); + www = null; + + resState = eResState.kReady; + + finishCallback(); + } + */ + } +} +public class StringAsset : ScriptableObject +{ + public string text; + public StringAsset(string text) + { + this.text = text; + } + +} \ No newline at end of file diff --git a/Assets/QFramework/Toolkits/ResKit/Scripts/LocalTextSupport/LocalTextSupport.cs.meta b/Assets/QFramework/Toolkits/ResKit/Scripts/LocalTextSupport/LocalTextSupport.cs.meta new file mode 100644 index 00000000..d42f12ed --- /dev/null +++ b/Assets/QFramework/Toolkits/ResKit/Scripts/LocalTextSupport/LocalTextSupport.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 999be03da3ba22844a61e7af236752c1 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Global/Global.cs b/Assets/Scripts/Global/Global.cs index 34dcf0bf..d79fe11b 100644 --- a/Assets/Scripts/Global/Global.cs +++ b/Assets/Scripts/Global/Global.cs @@ -12,9 +12,10 @@ public class Global : Singleton public XMLTool.AppData appData; public Module curModule; - public static string deviceIconsPath = Application.dataPath + "/../" + "Data/DeviceIcons/"; - public static string audioPath = Application.dataPath + "/../" + "Data/Audio/"; - + public static string dataPath = Application.dataPath + "/../Data"; + public static string deviceIconsPath = dataPath + "/DeviceIcons/"; + public static string audioPath = dataPath + "/Audio/"; + public static string appXmlPath = dataPath + "/App.xml"; public enum AppType { UnKnow = 1 << 0, diff --git a/Assets/Scripts/Launch.cs b/Assets/Scripts/Launch.cs index db470373..d9752ff7 100644 --- a/Assets/Scripts/Launch.cs +++ b/Assets/Scripts/Launch.cs @@ -18,11 +18,12 @@ public class Launch : MonoBehaviour IEnumerator StartApp() { yield return ResKit.InitAsync(); - loader.Add2Load(App_xml.APP, (success, res) => + + loader.Add2Load(Global.appXmlPath.ToLocalTextResName(), (success, res) => { if (success) { - Global.Instance.appData = XmlParser.ParseXml(res.Asset.As().text); + Global.Instance.appData = XmlParser.ParseXml(res.Asset.As().text); } });