public override void Execute(Pipeline pipeline) { var unityPackageData = pipeline.Manifest.Data.OfType <UnityPackages>().ToArray(); var remappableAssets = unityPackageData .SelectMany(upd => upd.unityPackages) .SelectMany(up => up.AssetFiles) .Select(AssetDatabase.GetAssetPath) .SelectMany(path => { if (AssetDatabase.IsValidFolder(path)) { return(Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories)); } else { return(Enumerable.Repeat(path, 1)); } }) .Select(path => (path, asset: AssetDatabase.LoadAssetAtPath <Object>(path))) .SelectMany(map => { var(path, asset) = map; if (asset is GameObject goAsset) { return(goAsset.GetComponentsInChildren <MonoBehaviour>() .Select(mb => (path: path, monoScript: MonoScript.FromMonoBehaviour(mb)))); } if (asset is ScriptableObject soAsset) { return(Enumerable.Repeat((path: path, monoScript: MonoScript.FromScriptableObject(soAsset)), 1)); } return(Enumerable.Empty <(string path, MonoScript monoScript)>()); }) .Select(map => { var type = map.monoScript.GetClass(); var fileId = FileIdUtil.Compute(type); var assemblyPath = type.Assembly.Location; var libraryGuid = PackageHelper.GetAssemblyHash(assemblyPath); AssetDatabase.TryGetGUIDAndLocalFileIdentifier(map.monoScript, out string scriptGuid, out long scriptId); return(Path: map.path, ScriptReference: $"{{fileID: {scriptId}, guid: {scriptGuid}, type: 3}}", AssemblyReference: $"{{fileID: {fileId}, guid: {libraryGuid}, type: 3}}"); }) .ToArray(); if (remapFileIds) { foreach (var map in remappableAssets) { var fileText = File.ReadAllText(map.Path); fileText = fileText.Replace(map.ScriptReference, map.AssemblyReference); File.WriteAllText(map.Path, fileText); } //AssetDatabase.Refresh(); } foreach (var unityPackageDatum in unityPackageData) { foreach (var outputPath in unityPackageDatum.StagingPaths.Select(path => path.Resolve(pipeline, this))) { if (!Directory.Exists(outputPath)) { Directory.CreateDirectory(outputPath); } foreach (var unityPackage in unityPackageDatum.unityPackages) { UnityPackage.Export(unityPackage, outputPath); } } } if (remapFileIds) { foreach (var map in remappableAssets) { var fileText = File.ReadAllText(map.Path); fileText = fileText.Replace(map.AssemblyReference, map.ScriptReference); File.WriteAllText(map.Path, fileText); } //AssetDatabase.Refresh(); } }
public override void Execute(Pipeline pipeline) { var unityPackageData = pipeline.Manifest.Data.OfType <UnityPackages>().ToArray(); var unityObjects = unityPackageData .SelectMany(upd => upd.unityPackages) .SelectMany(up => up.AssetFiles) .Select(AssetDatabase.GetAssetPath) .SelectMany(path => { if (AssetDatabase.IsValidFolder(path)) { return(Directory.GetFiles(path, "*", SearchOption.AllDirectories)); } else { return(Enumerable.Repeat(path, 1)); } }) .Select(path => new AssetPath(path, AssetDatabase.LoadAssetAtPath <Object>(path))) .ToArray(); var remappableAssets = unityObjects.SelectMany(map => { var path = map.path; var asset = map.asset; if (asset is Preset preset) { var presetSo = new SerializedObject(preset); SerializedProperty m_TargetType, m_ManagedTypePPtr; m_TargetType = presetSo.FindProperty(nameof(m_TargetType)); m_ManagedTypePPtr = m_TargetType.FindPropertyRelative(nameof(m_ManagedTypePPtr)); var monoScript = m_ManagedTypePPtr.objectReferenceValue as MonoScript; return(Enumerable.Repeat(new ScriptPath(path, monoScript), 1)); } if (asset is GameObject goAsset) { return(goAsset.GetComponentsInChildren <MonoBehaviour>() .Select(mb => new ScriptPath(path, MonoScript.FromMonoBehaviour(mb)))); } if (asset is ScriptableObject soAsset) { return(Enumerable.Repeat(new ScriptPath(path, MonoScript.FromScriptableObject(soAsset)), 1)); } return(Enumerable.Empty <ScriptPath>()); }) .Select(map => { var type = map.monoScript.GetClass(); var fileId = FileIdUtil.Compute(type); var assemblyPath = type.Assembly.Location; var libraryGuid = PackageHelper.GetFileNameHash(assemblyPath); AssetDatabase.TryGetGUIDAndLocalFileIdentifier(map.monoScript, out string scriptGuid, out long scriptId); return(new RemapData(map.path, new Regex($"(\\{{fileID:)\\s*?{scriptId},(\\s*?guid:)\\s*?{scriptGuid},(\\s*?type:\\s*?\\d+\\s*?\\}})", RegexOptions.Singleline), new Regex($"(\\{{fileID:)\\s*?{fileId},(\\s*?guid:)\\s*?{libraryGuid},(\\s*?type:\\s*?\\d+\\s*?\\}})", RegexOptions.Singleline), $"$1 {scriptId},$2 {scriptGuid},$3", $"$1 {fileId},$2 {libraryGuid},$3" )); }) .ToArray(); foreach (var map in remappableAssets) { var fileText = File.ReadAllText(map.Path); fileText = map.ToAssemblyReference.Replace(fileText, map.AssemblyReference); File.WriteAllText(map.Path, fileText); } foreach (var unityPackageDatum in unityPackageData) { foreach (var outputPath in unityPackageDatum.StagingPaths.Select(path => path.Resolve(pipeline, this))) { if (!Directory.Exists(outputPath)) { Directory.CreateDirectory(outputPath); } foreach (var unityPackage in unityPackageDatum.unityPackages) { unityPackage.Export(outputPath); } } } foreach (var map in remappableAssets) { var fileText = File.ReadAllText(map.Path); fileText = map.ToScriptReference.Replace(fileText, map.ScriptReference); File.WriteAllText(map.Path, fileText); } }