| 直接看代码,编译后通过改写 ProjectSetting/tagmanager 实现预设Layer效果,并且可以扩展更多关于Layer的功能 using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
[InitializeOnLoad]
public class LayerTest
{
    
    public enum InnerSettingLayer
    {
        PostProcessing = 8,
        RealityScene = 9,
        bgCamera = 10,
        maskCamera = 11,
        arCamera =12,
        panorama = 13,
    }
    static LayerTest()
    {
        SerializedObject tagManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);
        SerializedProperty layers = tagManager.FindProperty("layers");
        if (layers == null || !layers.isArray)
        {
            Debug.LogWarning("Can't set up the layers.  It's possible the format of the layers and tags data has changed in this version of Unity.");
            Debug.LogWarning("Layers is null: " + (layers == null));
            return;
        }
        
        foreach (var item in Enum.GetNames(typeof(InnerSettingLayer)))
        {
            int value = (int)Enum.Parse(typeof(InnerSettingLayer), item);
            if (value > 20)
            {
                EditorUtility.DisplayDialog("警告", "Layer 10-20层 限制为编辑器内部使用,用户在20层后进行设置","知道了");
                continue;
            }
            SerializedProperty layerSP = layers.GetArrayElementAtIndex(value);
            if (layerSP.stringValue == item)
            {
                
            }
            else
            {
                if (string.IsNullOrEmpty(layerSP.stringValue))
                {
                    layerSP.stringValue = item;
                }
                else
                {
                    if (EditorUtility.DisplayDialog("提示", "当前Layer:" + layerSP.stringValue + "不等于 预设值:" + item, "使用预设值", "忽略"))
                    {
                        layerSP.stringValue = item;
                    }
                }
            }
          
           
        }
        tagManager.ApplyModifiedProperties();
    }
}
 |