Skip to content

Instantly share code, notes, and snippets.

@baba-s
Created March 23, 2024 00:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baba-s/7e305195a37fe8ad26ade4943bad74bc to your computer and use it in GitHub Desktop.
Save baba-s/7e305195a37fe8ad26ade4943bad74bc to your computer and use it in GitHub Desktop.
using UnityEditor;
using UnityEngine;
namespace Kogane.Internal
{
[InitializeOnLoad]
internal static class LineRendererConstantColorPropertyField
{
static LineRendererConstantColorPropertyField()
{
_ = new EditorGUIUtility.PropertyCallbackScope( Callback );
static void Callback
(
Rect rect,
SerializedProperty serializedProperty
)
{
if ( serializedProperty.serializedObject.targetObject.GetType().Name != nameof( LineRenderer ) ) return;
if ( serializedProperty.propertyPath != "m_Parameters.colorGradient" ) return;
var color = serializedProperty.gradientValue.colorKeys[ 0 ].color;
using var changeCheckScope = new EditorGUI.ChangeCheckScope();
var newColor = EditorGUILayout.ColorField( "Constant Color", color );
if ( !changeCheckScope.changed ) return;
var newGradient = new Gradient();
var newAlpha = newColor.a;
newGradient.SetKeys
(
new GradientColorKey[] { new( newColor, 0 ), new( newColor, 1 ) },
new GradientAlphaKey[] { new( newAlpha, 0 ), new( newAlpha, 1 ) }
);
serializedProperty.gradientValue = newGradient;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment