UnityEditor》AssetPostprocessor 控制导入的脚本
类中的方法字段
public string assetPath { get; set; }
public AssetImporter assetImporter { get; }
public Texture2D preview { get; set; }
public virtual int GetPostprocessOrder();
public virtual uint GetVersion();
public void LogError(string warning);
public void LogError(string warning, [DefaultValue("null")] UnityEngine.Object context);
public void LogWarning(string warning);
public void LogWarning(string warning, [DefaultValue("null")] UnityEngine.Object context);
可以注意下projectsetting 下的assets 文件的数据
以下时TagManger的数据
这些内容手动配置是无法更改引擎内的数据的
//Mono挂在网上的脚本新版本存在两个问题
>layer 层 在index8之后才能设置
>tag 默认size为 0
可能时mono 16年的帖子针对5x版本可行但是针对17版本不可执行
代码:
using UnityEditor;
public class M_LayerExtent : AssetPostprocessor {
static string TagsAdd = "momo&yang";
static string TagsLayer = "ruoruo&hang";
private const string ScriptsPath = "Assets/Editor/M_LayerExtent.cs";
static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
{
foreach (string s in importedAssets)
{
//此脚本需要存在的路径
if (s.Equals(ScriptsPath))
{
string[] tags = TagsAdd.Split('&');
string[] Layers = TagsLayer.Split('&');
foreach (var tag in tags) AddTag(tag);
foreach (var layer in Layers) AddLayer(layer);
//Debug.Log("loading....");
return;
}
}
}
static void AddTag(string tag)
{
if (!isHasTag(tag))
{
SerializedObject tagManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);
SerializedProperty it = tagManager.GetIterator();
//Debug.Log("prop is:"+it);
while (it.NextVisible(true))
{
if (it.name == "tags")
{
//Debug.Log("NextVisible is:" + it.name+" size is:"+it.arraySize);
it.arraySize++;
for (int i = 0; i < it.arraySize; i++)
{
SerializedProperty dataPoint = it.GetArrayElementAtIndex(i);
//Debug.Log("loading.... " + dataPoint.stringValue);
if (string.IsNullOrEmpty(dataPoint.stringValue))
{
dataPoint.stringValue = tag;
tagManager.ApplyModifiedProperties();
//Debug.Log("is loading tag");
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))
{
//Debug.Log("prop is:"+ it.name);
if (it.name == "layers")
{
for (int i = 0; i < it.arraySize; i++)
{
SerializedProperty dataPoint = it.GetArrayElementAtIndex(i+1);
//Debug.Log("loading.... " + dataPoint.stringValue+" i:"+i);
//默认前把层无法设置
if (string.IsNullOrEmpty(dataPoint.stringValue)&&i>=8)
{
dataPoint.stringValue = layer;
tagManager.ApplyModifiedProperties();
//Debug.Log("is loading layer");
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;
}
}