在UnityEditor下,通过PrefabStage监听打开/关闭Prefab编辑事件,来进行 LayerMask 的设置。
引入 UnityEditor.Experimental.SceneManagement using UnityEditor包
初始化Load时注册监听函数
定义函数
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; using UnityEditor.Experimental.SceneManagement; namespace XXXX.NameSpace { public class PrefabStageChange { [InitializeOnLoadMethod] static void RegisterPrefabStageEvents() { PrefabStage.prefabSaving += OnSaving; PrefabStage.prefabSaved += OnSaved; PrefabStage.prefabStageClosing += OnClosing; PrefabStage.prefabStageOpened += OnOpend; } private static void OnSaving(GameObject go) { Debug.LogFormat("GameObject({0}) is saving.", go.name); } private static void OnSaved(GameObject go) { //Debug.LogFormat("GameObject({0}) is saved.", go.name); } private static void OnOpend(PrefabStage prefabStage) { if (prefabStage.prefabAssetPath.Contains("AssetBundleRes/Res")) { bool ischange = false; var root = prefabStage.prefabContentsRoot; int count = root.transform.childCount; for (int i = 0; i < count; i++) { if (root.transform.GetChild(i).gameObject.layer !=LayerMask.NameToLayer("BattleUI")) { ischange = true; root.transform.GetChild(i).gameObject.layer = LayerMask.NameToLayer("BattleUI"); } } if (root.layer !=LayerMask.NameToLayer("BattleUI")) { ischange = true; root.layer = LayerMask.NameToLayer("BattleUI"); } if (ischange) { EditorUtility.SetDirty(root); } } //Debug.LogFormat("GameObject({0}) is opend.", prefabStage.prefabAssetPath); } private static void OnClosing(PrefabStage prefabStage) { //Debug.LogFormat("GameObject({0}) is closing.", prefabStage.prefabAssetPath); } } }