123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589 |
- #if UNITY_EDITOR
- using System.Runtime.CompilerServices;
- using UnityEditor;
- using UnityEngine;
- namespace MTE
- {
- public enum TerrainMaterialType
- {
- BuiltInStandard,
- BuiltInLegacyDiffuse,
- BuiltInLegacySpecular,
- Custom,
- LWRPTerrainLit_Removed,
- URPTerrainLit,
- Unknown,
- }
- public static class CompatibilityUtil
- {
- public static TerrainLayer[] GetSplatLayers(TerrainData terrainData)
- {
- return terrainData.terrainLayers;
- }
- public static void SetSplatLayers(TerrainData terrainData, TerrainLayer[] layers)
- {
- terrainData.terrainLayers = layers;
- }
- public static Texture2D GetSplatLayerNormalMap(TerrainLayer splatLayer)
- {
- if (!splatLayer)
- {
- return null;
- }
- return splatLayer.normalMapTexture;
- }
- public static Texture2D GetSplatLayerDiffuseTexture(TerrainLayer splatLayer)
- {
- if (!splatLayer)
- {
- return null;
- }
- return splatLayer.diffuseTexture;
- }
- public static float GetSplatLayerMetallic(TerrainLayer splatLayer)
- {
- if (!splatLayer)
- {
- return 0;
- }
- return splatLayer.metallic;
- }
- public static float GetSplatLayerSmoothness(TerrainLayer splatLayer)
- {
- if (!splatLayer)
- {
- return 0;
- }
- return splatLayer.smoothness;
- }
- public static Texture2D GetMaskTexture(TerrainLayer splatLayer)
- {
- if (!splatLayer)
- {
- return null;
- }
- return splatLayer.maskMapTexture;
- }
- public static TerrainLayer[] CreateSplatLayers(int number)
- {
- return new TerrainLayer[number];
- }
- public static TerrainLayer CreateSplatLayer()
- {
- return new TerrainLayer();
- }
- public static void SetSplatLayerDiffueTexture(TerrainLayer splatLayer, Texture2D diffuseTexture)
- {
- splatLayer.diffuseTexture = diffuseTexture;
- }
- public static void SetSplatLayerNormalMap(TerrainLayer splatLayer, Texture2D normalMap)
- {
- splatLayer.normalMapTexture = normalMap;
- }
- public static void SaveSplatLayer(TerrainLayer splat, string name)
- {
- AssetDatabase.CreateAsset(splat, name);
- }
- /// <summary>
- /// Create a prefab file for a GameObject.
- /// </summary>
- /// <param name="obj">the GameObject</param>
- /// <param name="relativePath">Unity file path of the prefab</param>
- public static void CreatePrefab(GameObject obj, string relativePath)
- {
- PrefabUtility.SaveAsPrefabAsset(obj, relativePath);
- }
- /// <summary>
- /// Check if a gameObject is the root of a instantiated prefab.
- /// </summary>
- /// <param name="gameObject"></param>
- /// <returns></returns>
- public static bool IsPrefab(GameObject gameObject)
- {
- return PrefabUtility.IsAnyPrefabInstanceRoot(gameObject);
- }
- public static bool IsInstanceOfPrefab(GameObject obj, GameObject prefab)
- {
- return PrefabUtility.GetCorrespondingObjectFromOriginalSource(obj) == prefab;
- }
-
- public static Object GetPrefabRoot(GameObject instance)
- {
- return PrefabUtility.GetCorrespondingObjectFromSource(instance);
- }
- //This field will invoke a compile error if this version isn't officially supported by MTE.
- public const string VersionCheckString =
- #if UNITY_2018_4
- "2018.4"
- #elif UNITY_2019_3
- "2019.3"
- #elif UNITY_2019_4
- "2019.4"
- #elif UNITY_2020_1
- "2020.1"
- #elif UNITY_2020_2
- "2020.2"
- #elif UNITY_2020_3
- "2020.3"
- #elif UNITY_2021_1
- "2021.1"
- #elif UNITY_2021_2
- "2021.2"
- #elif UNITY_2021_3_OR_NEWER
- #warning Might be a unsupported Unity Version, not tested yet.
- "2021.3+"
- #else
- #error Unsupported Unity Version. You can try to fix this file or report the issue.
- #endif
- ;
-
- public static bool IsWebRequestError(UnityEngine.Networking.UnityWebRequest request)
- {
- #if UNITY_2018_4 || UNITY_2019_3 || UNITY_2019_4 || UNITY_2020_1
- return request.isNetworkError || request.isHttpError;
- #elif UNITY_2020_2 || UNITY_2020_3 || UNITY_2021_1 || UNITY_2021_2_OR_NEWER
- return request.result == UnityEngine.Networking.UnityWebRequest.Result.ConnectionError
- || request.result == UnityEngine.Networking.UnityWebRequest.Result.ProtocolError;
- #else
- #error not supported on any Unity build targets
- #endif
- }
- public static object AttachOnSceneGUI(System.Action<SceneView> action)
- {
- #if UNITY_2018_4
- SceneView.OnSceneFunc func = view => action(view);
- SceneView.onSceneGUIDelegate += func;
- return func;
- #elif UNITY_2019_3 || UNITY_2019_4 || UNITY_2020_1 || UNITY_2020_2 || UNITY_2020_3 || UNITY_2021_1 || UNITY_2021_2_OR_NEWER
- SceneView.duringSceneGui += action;
- return action;
- #else
- #error not supported on any Unity build targets
- #endif
- }
- public static void DetachOnSceneGUI(object cachedOnSceneGUI)
- {
- #if UNITY_2018_4
- SceneView.OnSceneFunc func = (SceneView.OnSceneFunc)cachedOnSceneGUI;
- SceneView.onSceneGUIDelegate -= func;
- #elif UNITY_2019_3 || UNITY_2019_4 || UNITY_2020_1 || UNITY_2020_2 || UNITY_2020_3 || UNITY_2021_1 || UNITY_2021_2_OR_NEWER
- var action = (System.Action<SceneView>)cachedOnSceneGUI;
- SceneView.duringSceneGui -= action;
- #else
- #error not supported on any Unity build targets
- #endif
- }
- public static void DontOptimizeMesh(ModelImporter importer)
- {
- #if UNITY_2019_3 || UNITY_2019_4 || UNITY_2020_1 || UNITY_2020_2 || UNITY_2020_3 || UNITY_2021_1 || UNITY_2021_2_OR_NEWER
- importer.optimizeMeshVertices = false;
- importer.optimizeMeshPolygons = false;
- importer.weldVertices = false;
- #elif UNITY_2018_4
- importer.optimizeMesh = false;
- #else
- #error not supported on any Unity build targets
- #endif
- }
- public static TerrainMaterialType GetTerrainMaterialType(Terrain terrain)
- {
- #if UNITY_2018_4
- switch (terrain.materialType)
- {
- case Terrain.MaterialType.BuiltInStandard:
- return TerrainMaterialType.BuiltInStandard;
- case Terrain.MaterialType.BuiltInLegacyDiffuse:
- return TerrainMaterialType.BuiltInLegacyDiffuse;
- case Terrain.MaterialType.BuiltInLegacySpecular:
- return TerrainMaterialType.BuiltInLegacySpecular;
- case Terrain.MaterialType.Custom:
- return TerrainMaterialType.Custom;
- default:
- throw new System.ArgumentOutOfRangeException();
- }
- #elif UNITY_2019_3 || UNITY_2019_4 || UNITY_2020_1 || UNITY_2020_2 || UNITY_2020_3 || UNITY_2021_1 || UNITY_2021_2_OR_NEWER
- var material = terrain.materialTemplate;
- if (material == null)
- {
- Debug.LogErrorFormat(
- "Terrain<{0}> is using an empty material." +
- "The conversion result is probably not right." +
- " Please check the material of the terrain",
- terrain.name);
- return TerrainMaterialType.Custom;
- }
- if (material.name == "Default-Terrain-Standard")
- {
- return TerrainMaterialType.BuiltInStandard;
- }
- if (material.name == "Default-Terrain-Diffuse")
- {
- return TerrainMaterialType.BuiltInLegacyDiffuse;
- }
- if (material.name == "Default-Terrain-Specular")
- {
- return TerrainMaterialType.BuiltInLegacySpecular;
- }
- if (material.shader != null
- && material.shader.name == "Lightweight Render Pipeline/Terrain/Lit")
- {
- return TerrainMaterialType.LWRPTerrainLit_Removed;
- }
- if (material.shader != null
- && material.shader.name == "Universal Render Pipeline/Terrain/Lit")
- {
- return TerrainMaterialType.URPTerrainLit;
- }
- var materialFileRelativePath = AssetDatabase.GetAssetPath(material);
- Debug.LogErrorFormat(
- "Terrain<{0}> is using a material<{1}> at {2} that is unknown to MTE." +
- "The conversion result is probably not right.",
- terrain.name, material.name, materialFileRelativePath);
- return TerrainMaterialType.Unknown;
- #else
- #error not supported on any Unity build targets
- #endif
- }
- public static Color GetTerrainMaterialSpecularColor(Terrain terrain)
- {
- #if UNITY_2018_4
- return terrain.legacySpecular;
- #elif UNITY_2019_3 || UNITY_2019_4 || UNITY_2020_1 || UNITY_2020_2 || UNITY_2020_3 || UNITY_2021_1 || UNITY_2021_2_OR_NEWER
- var material = terrain.materialTemplate;
- if (material.HasProperty("_SpecColor"))
- {
- return material.GetColor("_SpecColor");
- }
- return Color.black;
- #else
- #error not supported on any Unity build targets
- #endif
- }
- public static float GetTerrainMaterialShininess(Terrain terrain)
- {
- #if UNITY_2018_4
- return terrain.legacyShininess;
- #elif UNITY_2019_3 || UNITY_2019_4 || UNITY_2020_1 || UNITY_2020_2 || UNITY_2020_3 || UNITY_2021_1 || UNITY_2021_2_OR_NEWER
- var material = terrain.materialTemplate;
- return material.GetFloat("_Shininess");
- #else
- #error not supported on any Unity build targets
- #endif
- }
- public static int GetHeightmapWidth(TerrainData terrainData)
- {
- #if UNITY_2018_4
- return terrainData.heightmapWidth;
- #elif UNITY_2019_3 || UNITY_2019_4 || UNITY_2020_1 || UNITY_2020_2 || UNITY_2020_3 || UNITY_2021_1 || UNITY_2021_2_OR_NEWER
- return terrainData.heightmapResolution;
- #endif
- }
- public static int GetHeightmapHeight(TerrainData terrainData)
- {
- #if UNITY_2018_4
- return terrainData.heightmapHeight;
- #elif UNITY_2019_3 || UNITY_2019_4 || UNITY_2020_1 || UNITY_2020_2 || UNITY_2020_3 || UNITY_2021_1 || UNITY_2021_2_OR_NEWER
- return terrainData.heightmapResolution;
- #endif
- }
- public static void SetTerrainMaterialType(Terrain terrain, TerrainMaterialType materialType)
- {
- switch (materialType)
- {
- case TerrainMaterialType.BuiltInStandard:
- {
- #if UNITY_2018_4
- terrain.materialType = Terrain.MaterialType.BuiltInStandard;
- #elif UNITY_2019_3 || UNITY_2019_4 || UNITY_2020_1 || UNITY_2020_2 || UNITY_2020_3 || UNITY_2021_1 || UNITY_2021_2_OR_NEWER
- var material = Resources.Load<Material>("unity_builtin_extra/Default-Terrain-Standard");
- terrain.materialTemplate = material;
- #else
- #error not supported on any Unity build targets
- #endif
- }
- break;
- case TerrainMaterialType.BuiltInLegacyDiffuse:
- {
- #if UNITY_2018_4
- terrain.materialType = Terrain.MaterialType.BuiltInLegacyDiffuse;
- #elif UNITY_2019_3 || UNITY_2019_4 || UNITY_2020_1 || UNITY_2020_2 || UNITY_2020_3 || UNITY_2021_1 || UNITY_2021_2_OR_NEWER
- var material = Resources.Load<Material>("unity_builtin_extra/Default-Terrain-Diffuse");
- terrain.materialTemplate = material;
- #else
- #error not supported on any Unity build targets
- #endif
- }
- break;
- case TerrainMaterialType.BuiltInLegacySpecular:
- {
- #if UNITY_2018_4
- terrain.materialType = Terrain.MaterialType.BuiltInLegacySpecular;
- #elif UNITY_2019_3 || UNITY_2019_4 || UNITY_2020_1 || UNITY_2020_2 || UNITY_2020_3 || UNITY_2021_1 || UNITY_2021_2_OR_NEWER
- var material = Resources.Load<Material>("unity_builtin_extra/Default-Terrain-Specular");
- terrain.materialTemplate = material;
- #else
- #error not supported on any Unity build targets
- #endif
- }
- break;
- default:
- throw new System.NotSupportedException("Cannot set terrain material.");
- }
- }
- //Unity 2021.2.0 changed
- //https://unity3d.com/unity/whats-new/2021.2.0
- //Graphics: Changed: Renamed Texture2D.Resize to Reinitialize.
- public static void ReinitializeTexture2D(Texture2D texture, int newWidth, int newHeight)
- {
- #if UNITY_2018_4 || UNITY_2019_3 || UNITY_2019_4 || UNITY_2020_1 || UNITY_2020_2 || UNITY_2020_3 || UNITY_2021_1
- texture.Resize(newWidth, newHeight);
- #elif UNITY_2021_2_OR_NEWER
- texture.Reinitialize(newWidth, newHeight);
- #endif
- }
- public static byte[] EncodeTextureToPNG(Texture2D texture)
- {
- return ImageConversion.EncodeToPNG(texture);
- }
- public static bool GetTextureReadable(Texture texture)
- {
- return texture.isReadable;
- }
- public static bool BeginFoldoutHeaderGroup(bool foldout, string content)
- {
- #if UNITY_2018_4
- return GUILayout.Toggle(foldout, content, "button");
- #elif UNITY_2019_3 || UNITY_2019_4 || UNITY_2020_1 || UNITY_2020_2 || UNITY_2020_3 || UNITY_2021_1 || UNITY_2021_2_OR_NEWER
- return EditorGUILayout.BeginFoldoutHeaderGroup(foldout, content);
- #else
- #error not supported on any Unity build targets
- #endif
- }
- public static void EndFoldoutHeaderGroup()
- {
- #if UNITY_2018_4
- #elif UNITY_2019_3 || UNITY_2019_4 || UNITY_2020_1 || UNITY_2020_2 || UNITY_2020_3 || UNITY_2021_1 || UNITY_2021_2_OR_NEWER
- EditorGUILayout.EndFoldoutHeaderGroup();
- #else
- #error not supported on any Unity build targets
- #endif
- }
- //FBXExporter support
- public static System.Func<string, UnityEngine.Object, string> exportFbxMethod = null;
- public static bool InitFbxExport()
- {
- var ModelExporterType = System.Type.GetType("UnityEditor.Formats.Fbx.Exporter.ModelExporter, Unity.Formats.Fbx.Editor");
- if (ModelExporterType == null)
- {
- return false;
- }
- var method = ModelExporterType.GetMethod("ExportObject");
- if (method == null)
- {
- Debug.LogError("Failed to fetch the method: UnityEditor.Formats.Fbx.Exporter.ModelExporter.");
- return false;
- }
- exportFbxMethod = (System.Func<string, UnityEngine.Object, string>)System.Delegate.CreateDelegate(typeof(System.Func<string, UnityEngine.Object, string>), method);
- //Debug.Log("Successfully fetched the method: UnityEditor.Formats.Fbx.Exporter.ModelExporter().");
- return true;
- }
-
- private static Texture2D MakeTexture(int width, int height, Color color)
- {
- Color[] pixels = new Color[width * height];
- for (int i = 0; i < pixels.Length; i++)
- {
- pixels[i] = color;
- }
- Texture2D result = new Texture2D(width, height);
- result.SetPixels(pixels);
- result.Apply();
- return result;
- }
- public static GUIStyle GetGridListStyle()
- {
- var editorLabelStyle = EditorGUIUtility.GetBuiltinSkin(EditorSkin.Inspector).label;
- var gridListStyle = new GUIStyle(editorLabelStyle)
- {
- fixedWidth = 0,
- fixedHeight = 0,
- stretchWidth = true,
- stretchHeight = true,
- alignment = TextAnchor.MiddleCenter
- };
- Color32 hoverColor = Color.grey;
- Color32 selectedColor = new Color32(62, 125, 231, 255);
- gridListStyle.onHover.background = MakeTexture(1, 1, selectedColor);
- gridListStyle.hover.background = MakeTexture(1, 1, hoverColor);
- gridListStyle.normal.background = MakeTexture(1, 1, Color.clear);
- gridListStyle.onNormal.background = MakeTexture(1, 1, selectedColor);
- //gridListStyle.hover.background = MakeTexture(1, 1, new Color32(255, 255, 255, 62));
- //gridListStyle.onNormal.background = MakeTexture(1, 1, new Color32(62, 125, 231, 255));
- return gridListStyle;
- }
- public static object GetRenderPipelineAsset()
- {
- return UnityEngine.Rendering.GraphicsSettings.renderPipelineAsset;
- }
- public static void SetTextureFormatToA8(TextureImporterPlatformSettings textureSettings)
- {
- textureSettings.format = TextureImporterFormat.R8;
- }
- public static int FindPropertyIndex(Shader shader, string propertyName)
- {
- #if UNITY_2018_4
- var propertyCount = ShaderUtil.GetPropertyCount(shader);
- for (int i = 0; i < propertyCount; i++)
- {
- var name = ShaderUtil.GetPropertyName(shader, i);
- if (name == propertyName)
- {
- return i;
- }
- }
- return -1;
- #elif UNITY_2019_3 || UNITY_2019_4 || UNITY_2020_1 || UNITY_2020_2 || UNITY_2020_3 || UNITY_2020_3 || UNITY_2021_1 || UNITY_2021_2_OR_NEWER
- return shader.FindPropertyIndex(propertyName);
- #else
- #error not supported on any Unity build targets
- #endif
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static byte GetColor32ChannelValue(Color32 color32, int channel)
- {
- #if UNITY_2018_4
- switch (channel)
- {
- case 0:
- return color32.r;
- case 1:
- return color32.g;
- case 2:
- return color32.b;
- case 3:
- return color32.a;
- default:
- throw new System.IndexOutOfRangeException("Invalid Color32 index(" + channel.ToString() + ")!");
- }
- #elif UNITY_2019_3 || UNITY_2019_4 || UNITY_2020_1 || UNITY_2020_2 || UNITY_2020_3 || UNITY_2020_3 || UNITY_2021_1 || UNITY_2021_2_OR_NEWER
- return color32[channel];
- #endif
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static void SetColor32ChannelValue(ref Color32 color32, int channel, byte value)
- {
- #if UNITY_2018_4
- switch (channel)
- {
- case 0:
- color32.r = value;
- break;
- case 1:
- color32.g = value;
- break;
- case 2:
- color32.b = value;
- break;
- case 3:
- color32.a = value;
- break;
- default:
- throw new System.IndexOutOfRangeException("Invalid Color32 index(" + channel.ToString() + ")!");
- }
- #elif UNITY_2019_3 || UNITY_2019_4 || UNITY_2020_1 || UNITY_2020_2 || UNITY_2020_3 || UNITY_2020_3 || UNITY_2021_1 || UNITY_2021_2_OR_NEWER
- color32[channel] = value;
- #endif
- }
- public class GUI
- {
- public static float Slider(
- Rect position,
- float value,
- float size,
- float start,
- float end,
- GUIStyle slider,
- GUIStyle thumb,
- bool horiz,
- int id)
- {
- return UnityEngine.GUI.Slider(
- position,
- value,
- size,
- start,
- end,
- slider,
- thumb,
- horiz,
- id);
- }
- }
- public class EditorGUILayoutEx
- {
- public static bool BeginFoldout(bool foldout, string content)
- {
- #if UNITY_2018_4
- return GUILayout.Toggle(foldout, content, "button");
- #elif UNITY_2019_3 || UNITY_2019_4 || UNITY_2020_1 || UNITY_2020_2 || UNITY_2020_3 || UNITY_2020_3 || UNITY_2021_1 || UNITY_2021_2_OR_NEWER
- return EditorGUILayout.BeginFoldoutHeaderGroup(foldout, content);
- #endif
- }
- public static void EndFoldout()
- {
- #if UNITY_2018_4
- //nothing to do
- #elif UNITY_2019_3 || UNITY_2019_4 || UNITY_2020_1 || UNITY_2020_2 || UNITY_2020_3 || UNITY_2020_3 || UNITY_2021_1 || UNITY_2021_2_OR_NEWER
- EditorGUILayout.EndFoldoutHeaderGroup();
- #endif
- }
- }
- }
- }
- #endif
|