125 lines
4.0 KiB
C#
125 lines
4.0 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Reflection;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
#if UNITY_2018_1_OR_NEWER
|
|
public class RuntimeGizmosPreset : AssetPostprocessor
|
|
{
|
|
static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
|
|
{
|
|
foreach (string s in importedAssets)
|
|
{
|
|
if (s.Contains("RuntimeGizmosPreset.cs"))
|
|
{
|
|
AddLayer("Gizmos");
|
|
AddLayer("Nav");
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
static void AddTag(string tag)
|
|
{
|
|
if (!HasTag(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 (!HasLayer(layer))
|
|
{
|
|
SerializedObject tagMagager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/Tagmanager.asset"));
|
|
SerializedProperty it = tagMagager.GetIterator();
|
|
while (it.NextVisible(true))
|
|
{
|
|
if (it.name.Equals("layers"))
|
|
{
|
|
for (int i = 0; i < it.arraySize; i++)
|
|
{
|
|
if (i <= 7)
|
|
{
|
|
continue;
|
|
}
|
|
SerializedProperty sp = it.GetArrayElementAtIndex(i);
|
|
if (string.IsNullOrEmpty(sp.stringValue))
|
|
{
|
|
sp.stringValue = layer;
|
|
tagMagager.ApplyModifiedProperties();
|
|
Debug.Log("success");
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
static bool HasTag(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 HasLayer(string layer)
|
|
{
|
|
SerializedObject tagManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/Tagmanager.asset"));
|
|
SerializedProperty it = tagManager.GetIterator();
|
|
while (it.NextVisible(true))
|
|
{
|
|
if (it.name.Equals("layers"))
|
|
{
|
|
for (int i = 0; i < it.arraySize; i++)
|
|
{
|
|
if (i <= 7)
|
|
{
|
|
continue;
|
|
}
|
|
SerializedProperty sp = it.GetArrayElementAtIndex(i);
|
|
if (!string.IsNullOrEmpty(sp.stringValue))
|
|
{
|
|
if (sp.stringValue.Equals(layer))
|
|
{
|
|
sp.stringValue = string.Empty;
|
|
tagManager.ApplyModifiedProperties();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for (int i = 0; i < UnityEditorInternal.InternalEditorUtility.layers.Length; i++)
|
|
{
|
|
if (UnityEditorInternal.InternalEditorUtility.layers[i].Contains(layer))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
}
|
|
#endif |