NLDClient-yudde/ProjectNLD/Assets/Editor/Scene/PostProcesser/PrefabPostProcessor.cs

52 lines
2.0 KiB
C#
Raw Normal View History

2024-01-15 20:25:49 +08:00
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
public class PrefabPostProcessor : PostProcessor
{
private GameObject Prefab { get; set; }
private GameObject ClonedPrefab { get; set; }
private string PrefabPath => PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(_processInstructor);
protected override string ResourcesBaseDir => PREFAB_POSTPROCESS_DIR;
protected override string PostProcessFolderName => Path.GetFileNameWithoutExtension(PrefabPath.Replace("/", "_"));
2024-01-16 18:37:02 +08:00
protected override Object AssetToPostProcess => AssetDatabase.LoadAssetAtPath<GameObject>(PrefabPath);
protected override string OutputAssetPath =>
$"{ProcessedResourceDir}/{Prefab.name}_{SCENE_POST_PROCESS_NAME}.prefab";
2024-01-15 20:25:49 +08:00
protected override void Init(PostProcessInstructor postProcessInstructor)
{
base.Init(postProcessInstructor);
Prefab = PrefabUtility.GetNearestPrefabInstanceRoot(postProcessInstructor);
}
protected override PostProcessInstructor Clone()
{
2024-01-16 14:58:20 +08:00
PrefabUtility.ApplyPrefabInstance(Prefab, InteractionMode.AutomatedAction);
2024-01-16 18:37:02 +08:00
AssetDatabase.CopyAsset(PrefabPath, OutputAssetPath);
2024-01-15 20:25:49 +08:00
AssetDatabase.Refresh();
ClonedPrefab =
2024-01-16 18:37:02 +08:00
PrefabUtility.InstantiatePrefab(AssetDatabase.LoadAssetAtPath<GameObject>(OutputAssetPath)) as GameObject;
2024-01-15 20:25:49 +08:00
if (ClonedPrefab == null)
{
Debug.LogError("Clone Prefab is null");
return null;
}
var result = ClonedPrefab.GetComponentInChildren<PostProcessInstructor>();
return result;
}
2024-01-16 14:23:05 +08:00
protected override void AfterAll(bool destroyAfterPostProcess)
{
PrefabUtility.ApplyPrefabInstance(ClonedPrefab, InteractionMode.AutomatedAction);
EditorUtil.AddPathToAddressable(ProcessedResourceDir, PREFAB_ADDRESSABLE_GROUP);
if (destroyAfterPostProcess)
Object.DestroyImmediate(ClonedPrefab);
//EditorGUIUtility.PingObject(ClonedPrefab);
}
2024-01-15 20:25:49 +08:00
}