Last active
January 31, 2023 14:16
-
-
Save PythagoRascal/c2f501058820a5c35f513503baa9296c to your computer and use it in GitHub Desktop.
PretendTextures
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.IO; | |
| using System.Reflection; | |
| using UnityEditor; | |
| using UnityEditor.AssetImporters; | |
| using UnityEngine; | |
| using UnityEngine.Assertions; | |
| using Object = UnityEngine.Object; | |
| namespace PretendTextures | |
| { | |
| [ScriptedImporter(IMPORTER_VERSION, IMPORTER_EXTENSION)] | |
| public class GradientTexture : ScriptedImporter | |
| { | |
| private const int IMPORTER_VERSION = 1; | |
| private const string IMPORTER_EXTENSION = "gradienttex"; | |
| private const string ASSET_CONTENT = "GradientTexture asset.\nRelevant settings in the associated `.meta` file.\n"; | |
| [SerializeField] private Gradient Gradient = new Gradient(); | |
| [SerializeField] [Min(1)] private int TextureWidth = 32; | |
| [SerializeField] [Min(1)] private int TextureHeight = 1; | |
| [SerializeField] private FilterMode FilterMode = FilterMode.Bilinear; | |
| [SerializeField] private TextureWrapMode WrapMode = TextureWrapMode.Clamp; | |
| [MenuItem("Assets/Create/PretendTextures/GradientTexture")] | |
| private static void CreateGradientTexture() | |
| { | |
| string fileName = $"GradientTexture.{IMPORTER_EXTENSION}"; | |
| string fullFilePath = GetAssetCreationPath(fileName); | |
| ProjectWindowUtil.CreateAssetWithContent(fullFilePath, ASSET_CONTENT); | |
| } | |
| public override void OnImportAsset(AssetImportContext ctx) | |
| { | |
| Texture2D texture = GradientToTexture( | |
| Gradient, | |
| TextureWidth, TextureHeight, | |
| FilterMode, WrapMode | |
| ); | |
| ctx.AddObjectToAsset("MainAsset", texture, texture); | |
| ctx.SetMainObject(texture); | |
| } | |
| private static string GetAssetCreationPath(string fileName) | |
| { | |
| string creationDirectory = null; | |
| Object[] selectedAssets = Selection.GetFiltered<Object>(SelectionMode.Assets); | |
| foreach (Object obj in selectedAssets) | |
| { | |
| string assetPath = AssetDatabase.GetAssetPath(obj); | |
| if (string.IsNullOrEmpty(assetPath) || !File.Exists(assetPath)) | |
| continue; | |
| creationDirectory = Path.GetDirectoryName(assetPath); | |
| break; | |
| } | |
| if (string.IsNullOrEmpty(creationDirectory)) | |
| { | |
| Type projectWindowUtilType = typeof(ProjectWindowUtil); | |
| MethodInfo getActiveFolderPathMethod = | |
| projectWindowUtilType.GetMethod("GetActiveFolderPath", BindingFlags.Static | BindingFlags.NonPublic); | |
| string path = getActiveFolderPathMethod?.Invoke(null, new object[0]).ToString(); | |
| creationDirectory = path; | |
| } | |
| Assert.IsFalse(string.IsNullOrEmpty(creationDirectory)); | |
| if (!creationDirectory.EndsWith("/")) | |
| creationDirectory += "/"; | |
| string fullFilePath = AssetDatabase.GenerateUniqueAssetPath(creationDirectory + fileName); | |
| return fullFilePath; | |
| } | |
| private static Texture2D GradientToTexture | |
| ( | |
| Gradient gradient, | |
| int width = 32, int height = 1, | |
| FilterMode filterMode = FilterMode.Bilinear, | |
| TextureWrapMode wrapMode = TextureWrapMode.Clamp | |
| ) | |
| { | |
| var gradientTexture = new Texture2D(width, height, TextureFormat.RGB24, true, false) | |
| { | |
| filterMode = filterMode, | |
| wrapMode = wrapMode, | |
| anisoLevel = 1, | |
| }; | |
| float inverseWidth = (1f / width); | |
| for (int y = 0; y < height; y++) | |
| { | |
| for (int x = 0; x < width; x++) | |
| { | |
| float time = x * inverseWidth; | |
| Color gradientColor = gradient.Evaluate(time); | |
| gradientTexture.SetPixel(x, y, gradientColor); | |
| } | |
| } | |
| gradientTexture.Apply(); | |
| return gradientTexture; | |
| } | |
| } | |
| [CustomEditor(typeof(GradientTexture), true)] | |
| public class GradientTextureEditor : ScriptedImporterEditor | |
| { | |
| private SerializedProperty _gradientProperty; | |
| private SerializedProperty _textureWidthProperty; | |
| private SerializedProperty _textureHeightProperty; | |
| private SerializedProperty _filterModeProperty; | |
| private SerializedProperty _wrapModeProperty; | |
| public override void OnEnable() | |
| { | |
| base.OnEnable(); | |
| Initialize(); | |
| } | |
| public override void OnInspectorGUI() | |
| { | |
| serializedObject.Update(); | |
| EditorGUILayout.PropertyField(_gradientProperty); | |
| EditorGUILayout.PropertyField(_textureWidthProperty); | |
| EditorGUILayout.PropertyField(_textureHeightProperty); | |
| EditorGUILayout.PropertyField(_filterModeProperty); | |
| EditorGUILayout.PropertyField(_wrapModeProperty); | |
| serializedObject.ApplyModifiedProperties(); | |
| ApplyRevertGUI(); | |
| } | |
| private void Initialize() | |
| { | |
| _gradientProperty = serializedObject.FindProperty("Gradient"); | |
| _textureWidthProperty = serializedObject.FindProperty("TextureWidth"); | |
| _textureHeightProperty = serializedObject.FindProperty("TextureHeight"); | |
| _filterModeProperty = serializedObject.FindProperty("FilterMode"); | |
| _wrapModeProperty = serializedObject.FindProperty("WrapMode"); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment