When a project is packaged as UnityPackage, after importing other projects, the customized Tag and Layer in the project will be lost. The following method will automatically write its Tag or Layer into the user’s project when importing the UnityPackage package. Packing the following scripts with resources together, so that when other projects are imported into the UnityPackage package of the project, the program will call Addtag and Addlayer to add. judge.
The following code only adds one tag and a layer, modify the addition of N multiple tags and layers
Put the script under the project
USING System;
using system.collections;
using system.reflection;
using unityeditor;
using unityEngine;
public class newbehaviourscript: AssetPostProcessor
{{
Private Static String [] mytags = {"aaaa", "bbbb", "cccc", "dddd"};
Private Static String [] mylayers = {"aaaa", "bbbb", "cccc", "dddd"};
STATIC VOID ONPOTPROCESSALASSETS (String [] ImportedAssets, String [] deletedAssets, String [] Movedassets, String [] MovedfromasEtPaths)
{{
Foreach (String s in ImportEdassets)
{{
Ifquals ("Assets/NewBehaviourscript.cs")
{{
Foreach (String tag in mytags)
{{
Addtag (tag);
}
Foreach (String Layer in Mylayers)
{{
Addlayer (layer);
}
Addtag ("seld"); <span style = "font-family: arial, helvetica, sans-server; font-size: 12px;"> // Add a tag </span> called Self </span>
AddLayer ("Self"); <span style = "font-family: arial, helvetica, sans-server; font-size: 12px;"> // Add a layer called Ruoruo </span>
Return;
}
}
}
Static Void Addtag (String Tag)
{{
if (! Ishastag (tag))
{{
SerializedObject Tagmanager = New SerializedObject (AssetDataBase.loadallassetsatpath ("ProjectSettings/Tagmanager.asSet"););
Serializedproperty it = tagmanager.getIterator ();
While (IT.nextVisible (TRUE)))
{{
if (IT.NAME == "Tags")
{{
for (int i = 0; I <T.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"););
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 (unityEditorInterinal.internaleditorutility.tags [i] .contains (tag))
Return true;
}
Return false;
}
Static Bool Ishaslayer (String Layer)
{{
For (int i = 0; I <UnityEditorInalnal.internaleditorutility.Layers.Length; I ++)
{{
if (unityEditorInterinal.internaleditorutility.Layers [i] .contains (layer))
Return true;
}
Return false;
}
}
to MOMO, Original address: http://www.xuanyusong.com/archives/3169