Last active
April 13, 2021 20:58
-
-
Save JohannesDeml/9b04bd62a975f3f2c8ffcdf0d7400762 to your computer and use it in GitHub Desktop.
Unity Tooltip drawer for Material properties using reflection
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
// -------------------------------------------------------------------------------------------------------------------- | |
// <copyright file="TooltipDrawer.cs" company="Supyrb"> | |
// Copyright (c) 2018 Supyrb. All rights reserved. | |
// </copyright> | |
// <author> | |
// Johannes Deml | |
// [email protected] | |
// </author> | |
// -------------------------------------------------------------------------------------------------------------------- | |
using System; | |
using System.Reflection; | |
using UnityEditor; | |
using UnityEngine; | |
namespace Supyrb | |
{ | |
/// <summary> | |
/// Draws a tooltip for material properties. <see cref="TooltipAttribute"/> | |
/// Usage: [Tooltip(Write your tooltip here without quotes)] _Color("Albedo", Color) = (1,1,1,1)) | |
/// </summary> | |
public class TooltipDrawer : MaterialPropertyDrawer | |
{ | |
private GUIContent guiContent; | |
private MethodInfo internalMethod; | |
private Type[] methodArgumentTypes; | |
private object[] methodArguments; | |
public TooltipDrawer(string tooltip) | |
{ | |
this.guiContent = new GUIContent(string.Empty, tooltip); | |
methodArgumentTypes = new[] {typeof(Rect), typeof(MaterialProperty), typeof(GUIContent)}; | |
methodArguments = new object[3]; | |
internalMethod = typeof(MaterialEditor) | |
.GetMethod("DefaultShaderPropertyInternal", BindingFlags.Instance | BindingFlags.NonPublic, | |
null, | |
methodArgumentTypes, | |
null); | |
} | |
public override void OnGUI(Rect position, MaterialProperty prop, String label, MaterialEditor editor) | |
{ | |
guiContent.text = label; | |
if (internalMethod != null) | |
{ | |
methodArguments[0] = position; | |
methodArguments[1] = prop; | |
methodArguments[2] = guiContent; | |
internalMethod.Invoke(editor, methodArguments); | |
} | |
} | |
} | |
} |
Nice solution. Are you planning on adding a permissive license?
Hey there! You can find the script (and some more) over here: https://github.com/supyrb/ConfigurableShaders/tree/master/Scripts/Editor
The license of this repo is already MIT. I hope that helps :)
Thank you :^)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice solution. Are you planning on adding a permissive license?