Skip to content

Instantly share code, notes, and snippets.

@JohannesDeml
Last active April 13, 2021 20:58
Show Gist options
  • Save JohannesDeml/9b04bd62a975f3f2c8ffcdf0d7400762 to your computer and use it in GitHub Desktop.
Save JohannesDeml/9b04bd62a975f3f2c8ffcdf0d7400762 to your computer and use it in GitHub Desktop.
Unity Tooltip drawer for Material properties using reflection
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TooltipDrawer.cs" company="Supyrb">
// Copyright (c) 2018 Supyrb. All rights reserved.
// </copyright>
// <author>
// Johannes Deml
// send@johannesdeml.com
// </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);
}
}
}
}
@JohannesDeml
Copy link
Author

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 :)

@daleeidd
Copy link

Thank you :^)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment