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

52 lines
2.0 KiB
C#

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("/", "_"));
protected override Object AssetToPostProcess => AssetDatabase.LoadAssetAtPath<GameObject>(PrefabPath);
protected override string OutputAssetPath =>
$"{ProcessedResourceDir}/{Prefab.name}_{SCENE_POST_PROCESS_NAME}.prefab";
protected override void Init(PostProcessInstructor postProcessInstructor)
{
base.Init(postProcessInstructor);
Prefab = PrefabUtility.GetNearestPrefabInstanceRoot(postProcessInstructor);
}
protected override PostProcessInstructor Clone()
{
PrefabUtility.ApplyPrefabInstance(Prefab, InteractionMode.AutomatedAction);
AssetDatabase.CopyAsset(PrefabPath, OutputAssetPath);
AssetDatabase.Refresh();
ClonedPrefab =
PrefabUtility.InstantiatePrefab(AssetDatabase.LoadAssetAtPath<GameObject>(OutputAssetPath)) as GameObject;
if (ClonedPrefab == null)
{
Debug.LogError("Clone Prefab is null");
return null;
}
var result = ClonedPrefab.GetComponentInChildren<PostProcessInstructor>();
return result;
}
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);
}
}