unity 设置 layer 代码是自己在一个项目里自定义了自己的tag和layer,但是将自己的项目打包以后,导入一个新建的项目时,自定义的tag于Layer丢失。
在这里为大家实现在导入unitypackage的时候,将自定义的tag或者layer写进用户的项目里。
直接在这里为大家贴代码:
在这里为大家实现在导入unitypackage的时候,将自定义的tag或者layer写进用户的项目里。
直接在这里为大家贴代码:
- using System;
- using System.Collections;
- using System.Reflection;
- using UnityEditor;
- using UnityEngine;
- public class NewBehaviourScript :AssetPostprocessor
- {
- static void OnPostprocessAllAssets (string[] importedAssets, string[] deletedAssets,string[] movedAssets, string[] movedFromAssetPaths)
- {
- foreach(string s in importedAssets)
- {
- if (s.Equals("Assets/NewBehaviourScript.cs"))
- {
- //增加一个叫momo的tag
- AddTag("momo");
- //增加一个叫ruoruo的layer
- AddLayer("ruoruo");
- return ;
- }
- }
- }
- static void AddTag(string tag)
- {
- if(!isHasTag(tag))
- {
- SerializedObject tagManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);
- SerializedProperty it = tagManager.GetIterator();
- while (it.NextVisible(true))
- {
- if(it.name == "tags")
- {
- for (int i = 0; i < it.arraySize; i++)
- {
- SerializedProperty dataPoint = it.GetArrayElementAtIndex(i);
- if(string.IsNullOrEmpty(dataPoint.stringValue)){
- dataPoint.stringValue = tag;
- tagManager.ApplyModifiedProperties();
- return;
- }
- }
- }
- }
- }
- }
- static void AddLayer(string layer)
- {
- if(!isHasLayer(layer))
- {
- SerializedObject tagManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);
- SerializedProperty it = tagManager.GetIterator();
- while (it.NextVisible(true))
- {
- if(it.name.StartsWith("User Layer"))
- {
- if(it.type == "string" )
- {
- if(string.IsNullOrEmpty(it.stringValue)){
- it.stringValue = layer;
- tagManager.ApplyModifiedProperties();
- return;
- }
- }
- }
- }
- }
- }
- static bool isHasTag(string tag)
- {
- for (int i = 0; i < UnityEditorInternal.InternalEditorUtility.tags.Length; i++) {
- if (UnityEditorInternal.InternalEditorUtility.tags[i].Contains(tag))
- return true;
- }
- return false;
- }
- static bool isHasLayer(string layer)
- {
- for (int i = 0; i < UnityEditorInternal.InternalEditorUtility.layers.Length; i++) {
- if (UnityEditorInternal.InternalEditorUtility.layers[i].Contains(layer))
- return true;
- }
- return false;
- }
- }