@@ -485,36 +485,74 @@ private static float DrawSingleVectorSlider(float setting, string label)
485485 return x ;
486486 }
487487
488+ private static bool _drawColorHex ;
488489 private static void DrawColor ( SettingEntryBase obj )
489490 {
490- var setting = ( Color ) obj . Get ( ) ;
491+ var colorValue = ( Color ) obj . Get ( ) ;
492+
493+ GUI . changed = false ;
491494
492495 if ( ! _colorCache . TryGetValue ( obj , out var cacheEntry ) )
493496 {
494- cacheEntry = new ColorCacheEntry { Tex = new Texture2D ( 40 , 10 , TextureFormat . ARGB32 , false ) , Last = setting } ;
495- cacheEntry . Tex . FillTexture ( setting ) ;
497+ var tex = new Texture2D ( 100 , 20 , TextureFormat . ARGB32 , false ) ;
498+ cacheEntry = new ColorCacheEntry { Tex = tex , Last = colorValue } ;
499+ FillTex ( colorValue , tex ) ;
496500 _colorCache [ obj ] = cacheEntry ;
497501 }
498502
499- GUILayout . Label ( "R" , GUILayout . ExpandWidth ( false ) ) ;
500- setting . r = GUILayout . HorizontalSlider ( setting . r , 0f , 1f , GUILayout . ExpandWidth ( true ) ) ;
501- GUILayout . Label ( "G" , GUILayout . ExpandWidth ( false ) ) ;
502- setting . g = GUILayout . HorizontalSlider ( setting . g , 0f , 1f , GUILayout . ExpandWidth ( true ) ) ;
503- GUILayout . Label ( "B" , GUILayout . ExpandWidth ( false ) ) ;
504- setting . b = GUILayout . HorizontalSlider ( setting . b , 0f , 1f , GUILayout . ExpandWidth ( true ) ) ;
505- GUILayout . Label ( "A" , GUILayout . ExpandWidth ( false ) ) ;
506- setting . a = GUILayout . HorizontalSlider ( setting . a , 0f , 1f , GUILayout . ExpandWidth ( true ) ) ;
503+ GUILayout . BeginVertical ( ) ;
504+ {
505+ GUILayout . BeginHorizontal ( ) ;
506+ {
507+ GUILayout . Label ( cacheEntry . Tex , GUILayout . ExpandWidth ( false ) ) ;
507508
508- GUILayout . Space ( 4 ) ;
509+ var colorStr = _drawColorHex ? "#" + ColorUtility . ToHtmlStringRGBA ( colorValue ) : $ "{ colorValue . r : F2} { colorValue . g : F2} { colorValue . b : F2} { colorValue . a : F2} ";
510+ var newColorStr = GUILayout . TextField ( colorStr , GUILayout . ExpandWidth ( true ) ) ;
511+ if ( GUI . changed && colorStr != newColorStr )
512+ {
513+ if ( _drawColorHex )
514+ {
515+ if ( ColorUtility . TryParseHtmlString ( newColorStr , out var parsedColor ) )
516+ colorValue = parsedColor ;
517+ }
518+ else
519+ {
520+ var split = newColorStr . Split ( ' ' ) ;
521+ if ( split . Length == 4 && float . TryParse ( split [ 0 ] , out var r ) && float . TryParse ( split [ 1 ] , out var g ) && float . TryParse ( split [ 2 ] , out var b ) && float . TryParse ( split [ 3 ] , out var a ) )
522+ colorValue = new Color ( r , g , b , a ) ;
523+ }
524+ }
509525
510- if ( setting != cacheEntry . Last )
526+ _drawColorHex = GUILayout . Toggle ( _drawColorHex , "Hex" , GUILayout . ExpandWidth ( false ) ) ;
527+ }
528+ GUILayout . EndHorizontal ( ) ;
529+ GUILayout . BeginHorizontal ( ) ;
530+ {
531+ GUILayout . Label ( "R" , GUILayout . ExpandWidth ( false ) ) ;
532+ colorValue . r = GUILayout . HorizontalSlider ( colorValue . r , 0f , 1f , GUILayout . ExpandWidth ( true ) ) ;
533+ GUILayout . Label ( "G" , GUILayout . ExpandWidth ( false ) ) ;
534+ colorValue . g = GUILayout . HorizontalSlider ( colorValue . g , 0f , 1f , GUILayout . ExpandWidth ( true ) ) ;
535+ GUILayout . Label ( "B" , GUILayout . ExpandWidth ( false ) ) ;
536+ colorValue . b = GUILayout . HorizontalSlider ( colorValue . b , 0f , 1f , GUILayout . ExpandWidth ( true ) ) ;
537+ GUILayout . Label ( "A" , GUILayout . ExpandWidth ( false ) ) ;
538+ colorValue . a = GUILayout . HorizontalSlider ( colorValue . a , 0f , 1f , GUILayout . ExpandWidth ( true ) ) ;
539+ }
540+ GUILayout . EndHorizontal ( ) ;
541+ }
542+ GUILayout . EndVertical ( ) ;
543+
544+ if ( colorValue != cacheEntry . Last )
511545 {
512- obj . Set ( setting ) ;
513- cacheEntry . Tex . FillTexture ( setting ) ;
514- cacheEntry . Last = setting ;
546+ obj . Set ( colorValue ) ;
547+ FillTex ( colorValue , cacheEntry . Tex ) ;
548+ cacheEntry . Last = colorValue ;
515549 }
516550
517- GUILayout . Label ( cacheEntry . Tex , GUILayout . ExpandWidth ( false ) ) ;
551+ void FillTex ( Color color , Texture2D tex )
552+ {
553+ if ( color . a < 1f ) tex . FillTextureCheckerboard ( ) ;
554+ tex . FillTexture ( color ) ;
555+ }
518556 }
519557
520558 private sealed class ColorCacheEntry
0 commit comments