Compare commits

...

6 Commits

Author SHA1 Message Date
何冠峰 79467bbf13 update editor assembly 2025-03-12 18:32:28 +08:00
何冠峰 c7d678282b
Merge pull request #501 from Y-way/support-macros
Update MacrosProcessor.cs
2025-03-12 17:17:47 +08:00
何冠峰 d376afc217 fix #508 2025-03-12 17:16:24 +08:00
何冠峰 a62f808591 fix #506 2025-03-12 16:36:37 +08:00
Y-way bdcf95384f Update .gitignore 2025-03-10 16:13:18 +08:00
Y-way d910af589d Update MacrosProcessor.cs
修正YooAsset相关宏在引用库不能生效的问题.
2025-03-10 15:22:44 +08:00
13 changed files with 214 additions and 21 deletions

2
.gitignore vendored
View File

@ -20,6 +20,8 @@
/Assets/StreamingAssets.meta
/Assets/Samples
/Assets/Samples.meta
/Assets/csc.rsp
/Assets/csc.rsp.meta
/UserSettings

View File

@ -0,0 +1,17 @@
using System.Collections.Generic;
namespace YooAsset.Editor
{
public class MacroDefine
{
/// <summary>
/// YooAsset版本宏定义
/// </summary>
public static readonly List<string> Macros = new List<string>()
{
"YOO_ASSET_2",
"YOO_ASSET_2_3",
"YOO_ASSET_2_3_OR_NEWER",
};
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a61e5c2ca04aab647b1ed0492086aa8f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,28 +1,15 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using UnityEditor;
namespace YooAsset.Editor
{
[InitializeOnLoad]
public class MacrosProcessor : AssetPostprocessor
public class MacroProcessor : AssetPostprocessor
{
static MacrosProcessor()
{
}
/// <summary>
/// YooAsset版本宏定义
/// </summary>
private static readonly List<string> YooAssetMacros = new List<string>()
{
"YOO_ASSET_2",
"YOO_ASSET_2_3",
"YOO_ASSET_2_3_OR_NEWER",
};
static string OnGeneratedCSProject(string path, string content)
{
XmlDocument xmlDoc = new XmlDocument();
@ -65,7 +52,7 @@ namespace YooAsset.Editor
string[] defines = childNode.InnerText.Split(';');
HashSet<string> hashSets = new HashSet<string>(defines);
foreach (string yooMacro in YooAssetMacros)
foreach (string yooMacro in MacroDefine.Macros)
{
string tmpMacro = yooMacro.Trim();
if (hashSets.Contains(tmpMacro) == false)

View File

@ -0,0 +1,115 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using UnityEditor;
using UnityEngine;
#if YOO_ASSET_EXPERIMENT
namespace YooAsset.Editor.Experiment
{
[InitializeOnLoad]
public class RspGenerator
{
// csc.rsp文件路径
private static string RspFilePath => Path.Combine(Application.dataPath, "csc.rsp");
static RspGenerator()
{
UpdateRspFile(MacroDefine.Macros, null);
}
/// <summary>
/// 更新csc.rsp文件
/// </summary>
private static void UpdateRspFile(List<string> addMacros, List<string> removeMacros)
{
var existingDefines = new HashSet<string>();
var otherLines = new List<string>();
// 1. 读取现有内容
ReadRspFile(existingDefines, otherLines);
// 2. 添加新宏
if (addMacros != null && addMacros.Count > 0)
{
addMacros.ForEach(x =>
{
if (existingDefines.Contains(x) == false)
existingDefines.Add(x);
});
}
// 3. 移除指定宏
if (removeMacros != null && removeMacros.Count > 0)
{
removeMacros.ForEach(x =>
{
existingDefines.Remove(x);
});
}
// 4. 重新生成内容
WriteRspFile(existingDefines, otherLines);
// 5. 刷新AssetDatabase
AssetDatabase.Refresh();
EditorUtility.RequestScriptReload();
}
/// <summary>
/// 读取csc.rsp文件,返回宏定义和其他行
/// </summary>
private static void ReadRspFile(HashSet<string> defines, List<string> others)
{
if (defines == null)
defines = new HashSet<string>();
if (others == null)
others = new List<string>();
if (File.Exists(RspFilePath) == false)
return;
foreach (string line in File.ReadAllLines(RspFilePath))
{
if (line.StartsWith("-define:"))
{
string[] parts = line.Split(new[] { ':' }, 2);
if (parts.Length == 2)
{
defines.Add(parts[1].Trim());
}
}
else
{
others.Add(line);
}
}
}
/// <summary>
/// 重新写入csc.rsp文件
/// </summary>
private static void WriteRspFile(HashSet<string> defines, List<string> others)
{
StringBuilder sb = new StringBuilder();
if (others != null && others.Count > 0)
{
others.ForEach(o => sb.AppendLine(o));
}
if (defines != null && defines.Count > 0)
{
foreach (string define in defines)
{
sb.AppendLine($"-define:{define}");
}
}
File.WriteAllText(RspFilePath, sb.ToString());
}
}
}
#endif

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c2662e1d33b1eea469695b68d18b1739
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -3,6 +3,16 @@ namespace YooAsset
{
public class AssetInfo
{
internal enum ELoadMethod
{
None = 0,
LoadAsset,
LoadSubAssets,
LoadAllAssets,
LoadScene,
LoadRawFile,
}
private readonly PackageAsset _packageAsset;
private string _providerGUID;
@ -21,6 +31,11 @@ namespace YooAsset
/// </summary>
public string Error { private set; get; }
/// <summary>
/// 加载方法
/// </summary>
internal ELoadMethod LoadMethod;
/// <summary>
/// 资源对象
/// </summary>

View File

@ -174,7 +174,7 @@ namespace YooAsset
}
/// <summary>
/// 获取资源依赖列表
/// 获取依赖列表
/// 注意:传入的资源对象一定合法有效!
/// </summary>
public PackageBundle[] GetAllDependencies(PackageAsset packageAsset)
@ -188,6 +188,21 @@ namespace YooAsset
return result.ToArray();
}
/// <summary>
/// 获取依赖列表
/// 注意:传入的资源包对象一定合法有效!
/// </summary>
public PackageBundle[] GetAllDependencies(PackageBundle packageBundle)
{
List<PackageBundle> result = new List<PackageBundle>(packageBundle.DependBundleIDs.Length);
foreach (var dependID in packageBundle.DependBundleIDs)
{
var dependBundle = GetMainPackageBundle(dependID);
result.Add(dependBundle);
}
return result.ToArray();
}
/// <summary>
/// 尝试获取包裹的资源
/// </summary>

View File

@ -176,7 +176,17 @@ namespace YooAsset
throw new Exception("Should never get here !");
// 注意:如果清单里未找到资源包会抛出异常!
var depends = ActiveManifest.GetAllDependencies(assetInfo.Asset);
PackageBundle[] depends;
if (assetInfo.LoadMethod == AssetInfo.ELoadMethod.LoadAllAssets)
{
var mainBundle = ActiveManifest.GetMainPackageBundle(assetInfo.Asset);
depends = ActiveManifest.GetAllDependencies(mainBundle);
}
else
{
depends = ActiveManifest.GetAllDependencies(assetInfo.Asset);
}
List<BundleInfo> result = new List<BundleInfo>(depends.Length);
foreach (var packageBundle in depends)
{

View File

@ -610,6 +610,7 @@ namespace YooAsset
private SceneHandle LoadSceneInternal(AssetInfo assetInfo, bool waitForAsyncComplete, LoadSceneMode sceneMode, LocalPhysicsMode physicsMode, bool suspendLoad, uint priority)
{
DebugCheckAssetLoadType(assetInfo.AssetType);
assetInfo.LoadMethod = AssetInfo.ELoadMethod.LoadScene;
var loadSceneParams = new LoadSceneParameters(sceneMode, physicsMode);
var handle = _resourceManager.LoadSceneAsync(assetInfo, loadSceneParams, suspendLoad, priority);
if (waitForAsyncComplete)
@ -720,6 +721,7 @@ namespace YooAsset
private AssetHandle LoadAssetInternal(AssetInfo assetInfo, bool waitForAsyncComplete, uint priority)
{
DebugCheckAssetLoadType(assetInfo.AssetType);
assetInfo.LoadMethod = AssetInfo.ELoadMethod.LoadAsset;
var handle = _resourceManager.LoadAssetAsync(assetInfo, priority);
if (waitForAsyncComplete)
handle.WaitForAsyncComplete();
@ -829,6 +831,7 @@ namespace YooAsset
private SubAssetsHandle LoadSubAssetsInternal(AssetInfo assetInfo, bool waitForAsyncComplete, uint priority)
{
DebugCheckAssetLoadType(assetInfo.AssetType);
assetInfo.LoadMethod = AssetInfo.ELoadMethod.LoadSubAssets;
var handle = _resourceManager.LoadSubAssetsAsync(assetInfo, priority);
if (waitForAsyncComplete)
handle.WaitForAsyncComplete();
@ -938,6 +941,7 @@ namespace YooAsset
private AllAssetsHandle LoadAllAssetsInternal(AssetInfo assetInfo, bool waitForAsyncComplete, uint priority)
{
DebugCheckAssetLoadType(assetInfo.AssetType);
assetInfo.LoadMethod = AssetInfo.ELoadMethod.LoadAllAssets;
var handle = _resourceManager.LoadAllAssetsAsync(assetInfo, priority);
if (waitForAsyncComplete)
handle.WaitForAsyncComplete();

View File

@ -23,7 +23,10 @@ namespace YooAsset
{
if (_assetBundle != null)
{
_assetBundle.TTUnload(true);
if (_packageBundle.Encrypted)
_assetBundle.Unload(true);
else
_assetBundle.TTUnload(true);
}
}
public override string GetBundleFilePath()

View File

@ -11,7 +11,7 @@ namespace YooAsset
private readonly IFileSystem _fileSystem;
private readonly PackageBundle _packageBundle;
private readonly AssetBundle _assetBundle;
public WXAssetBundleResult(IFileSystem fileSystem, PackageBundle packageBundle, AssetBundle assetBundle)
{
_fileSystem = fileSystem;
@ -23,7 +23,10 @@ namespace YooAsset
{
if (_assetBundle != null)
{
_assetBundle.WXUnload(true);
if (_packageBundle.Encrypted)
_assetBundle.Unload(true);
else
_assetBundle.WXUnload(true);
}
}
public override string GetBundleFilePath()