TH1/Unity/Assets/Scripts/TH1_Logic/Editor/QuestionnairePrefabTemplateTools.cs

75 lines
2.7 KiB
C#

using TH1_UI.View.Outside;
using UnityEditor;
using UnityEngine;
namespace TH1_Logic.Editor
{
public static class QuestionnairePrefabTemplateTools
{
private const string MainPrefabPath = "Assets/BundleResources/Prefab/UI/Outside/UIOutsideQuestionnaire.prefab";
private const string QuestionPrefabPath = "Assets/BundleResources/Prefab/UI/Outside/UIOutsideQuestionnaireQuestion.prefab";
private const string OptionPrefabPath = "Assets/BundleResources/Prefab/UI/Outside/UIOutsideQuestionnaireOption.prefab";
[MenuItem("Tools/TH1/UI/Questionnaire/Rebuild Design Templates")]
public static void RebuildDesignTemplates()
{
RebuildPrefab(MainPrefabPath, root =>
{
var view = root.GetComponent<UIOutsideQuestionnaireView>();
if (view == null)
{
Debug.LogError($"[QuestionnairePrefabTemplateTools] Missing UIOutsideQuestionnaireView on {MainPrefabPath}");
return false;
}
view.RebuildDesignTemplatesContext();
return true;
});
RebuildPrefab(QuestionPrefabPath, root =>
{
var question = root.GetComponent<UIOutsideQuestionnaireQuestionMono>();
if (question == null)
{
Debug.LogError($"[QuestionnairePrefabTemplateTools] Missing UIOutsideQuestionnaireQuestionMono on {QuestionPrefabPath}");
return false;
}
question.RebuildDesignTemplateContext();
return true;
});
RebuildPrefab(OptionPrefabPath, root =>
{
var option = root.GetComponent<UIOutsideQuestionnaireOptionMono>();
if (option == null)
{
Debug.LogError($"[QuestionnairePrefabTemplateTools] Missing UIOutsideQuestionnaireOptionMono on {OptionPrefabPath}");
return false;
}
option.RebuildDesignTemplateContext();
return true;
});
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
private static void RebuildPrefab(string assetPath, System.Func<GameObject, bool> rebuild)
{
var root = PrefabUtility.LoadPrefabContents(assetPath);
try
{
if (!rebuild(root)) return;
PrefabUtility.SaveAsPrefabAsset(root, assetPath);
Debug.Log($"[QuestionnairePrefabTemplateTools] Rebuilt design templates: {assetPath}");
}
finally
{
PrefabUtility.UnloadPrefabContents(root);
}
}
}
}