Ver código fonte

输入框聚焦时,禁止图像透明

lvjincheng 3 anos atrás
pai
commit
e4324d98e4

+ 9 - 0
Assets/BowArrow/Scenes/Login.unity

@@ -2569,6 +2569,7 @@ MonoBehaviour:
   m_EditorClassIdentifier: 
   inputField: {fileID: 0}
   moveRect: {fileID: 0}
+  noTransparentOnFocused: {fileID: 257790179}
 --- !u!1 &261231100
 GameObject:
   m_ObjectHideFlags: 0
@@ -3776,6 +3777,7 @@ MonoBehaviour:
   m_EditorClassIdentifier: 
   inputField: {fileID: 0}
   moveRect: {fileID: 0}
+  noTransparentOnFocused: {fileID: 0}
 --- !u!1 &498195997
 GameObject:
   m_ObjectHideFlags: 0
@@ -7327,6 +7329,7 @@ MonoBehaviour:
   m_EditorClassIdentifier: 
   inputField: {fileID: 0}
   moveRect: {fileID: 0}
+  noTransparentOnFocused: {fileID: 0}
 --- !u!1 &1098956578
 GameObject:
   m_ObjectHideFlags: 0
@@ -7553,6 +7556,7 @@ MonoBehaviour:
   m_EditorClassIdentifier: 
   inputField: {fileID: 0}
   moveRect: {fileID: 0}
+  noTransparentOnFocused: {fileID: 1105214627}
 --- !u!1 &1107382989
 GameObject:
   m_ObjectHideFlags: 0
@@ -8079,6 +8083,7 @@ MonoBehaviour:
   m_EditorClassIdentifier: 
   inputField: {fileID: 0}
   moveRect: {fileID: 0}
+  noTransparentOnFocused: {fileID: 0}
 --- !u!1 &1198825416
 GameObject:
   m_ObjectHideFlags: 0
@@ -8596,6 +8601,7 @@ MonoBehaviour:
   m_EditorClassIdentifier: 
   inputField: {fileID: 0}
   moveRect: {fileID: 0}
+  noTransparentOnFocused: {fileID: 0}
 --- !u!1 &1381130818
 GameObject:
   m_ObjectHideFlags: 0
@@ -8959,6 +8965,7 @@ MonoBehaviour:
   m_EditorClassIdentifier: 
   inputField: {fileID: 0}
   moveRect: {fileID: 0}
+  noTransparentOnFocused: {fileID: 0}
 --- !u!1 &1490439588
 GameObject:
   m_ObjectHideFlags: 0
@@ -9389,6 +9396,7 @@ MonoBehaviour:
   m_EditorClassIdentifier: 
   inputField: {fileID: 0}
   moveRect: {fileID: 0}
+  noTransparentOnFocused: {fileID: 1545576240}
 --- !u!1 &1549087750
 GameObject:
   m_ObjectHideFlags: 0
@@ -11404,6 +11412,7 @@ MonoBehaviour:
   m_EditorClassIdentifier: 
   inputField: {fileID: 0}
   moveRect: {fileID: 0}
+  noTransparentOnFocused: {fileID: 1802940219}
 --- !u!1 &1803470176
 GameObject:
   m_ObjectHideFlags: 0

+ 10 - 0
Assets/BowArrow/Scripts/Components/InputFieldLayoutX.cs

@@ -6,6 +6,14 @@ public class InputFieldLayoutX : MonoBehaviour
 {
     public InputField inputField;//输入框组件
     public RectTransform moveRect;//输入框组件ui框,同时也用于移动
+    public MaskableGraphic noTransparentOnFocused; //聚焦时,图像不透明
+    private static Material _NoTransparentMaterial;
+    public static Material NoTransparentMaterial {
+        get {
+            if (!_NoTransparentMaterial) _NoTransparentMaterial = new Material(Shader.Find("JC/UI/InputFieldLayoutX_NoTransparent"));
+            return _NoTransparentMaterial;
+        }
+    }
 
     bool isFocused = false;
     Vector2 moveRectOriginAnchoredPos;
@@ -42,12 +50,14 @@ public class InputFieldLayoutX : MonoBehaviour
         moveRectOriginAnchoredPos = moveRect.anchoredPosition;
         moveRectOriginPos = moveRect.position;
         StartCoroutine(DelayGetKeyboardHeight());
+        if (noTransparentOnFocused) noTransparentOnFocused.material = NoTransparentMaterial;
     }
 
     void onLostFocused()
     {
         keyboardHeight_last = 0;
         moveRect.anchoredPosition = moveRectOriginAnchoredPos;
+        if (noTransparentOnFocused) noTransparentOnFocused.material = null;
     }
 
     IEnumerator DelayGetKeyboardHeight()

+ 119 - 0
Assets/BowArrow/Scripts/Components/InputFieldLayoutX_NoTransparent.shader

@@ -0,0 +1,119 @@
+//验证码拖拽
+Shader "JC/UI/InputFieldLayoutX_NoTransparent"
+{
+    Properties
+    {
+        [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
+        _Color("Tint", Color) = (1,1,1,1)
+ 
+        _StencilComp("Stencil Comparison", Float) = 8
+        _Stencil("Stencil ID", Float) = 0
+        _StencilOp("Stencil Operation", Float) = 0
+        _StencilWriteMask("Stencil Write Mask", Float) = 255
+        _StencilReadMask("Stencil Read Mask", Float) = 255
+ 
+        _ColorMask("Color Mask", Float) = 15
+ 
+        [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip("Use Alpha Clip", Float) = 0
+    }
+ 
+    SubShader
+    {
+        Tags
+        {
+            "Queue" = "Transparent"
+            "IgnoreProjector" = "True"
+            "RenderType" = "Transparent"
+            "PreviewType" = "Plane"
+            "CanUseSpriteAtlas" = "True"
+        }
+ 
+        Stencil
+        {
+            Ref[_Stencil]
+            Comp[_StencilComp]
+            Pass[_StencilOp]
+            ReadMask[_StencilReadMask]
+            WriteMask[_StencilWriteMask]
+        }
+ 
+        Cull Off
+        Lighting Off
+        ZWrite Off
+        ZTest[unity_GUIZTestMode]
+        Blend SrcAlpha OneMinusSrcAlpha
+        ColorMask[_ColorMask]
+ 
+        Pass
+        {
+            CGPROGRAM
+#pragma vertex vert
+#pragma fragment frag
+ 
+#include "UnityCG.cginc"
+#include "UnityUI.cginc"
+ 
+#pragma multi_compile __ UNITY_UI_ALPHACLIP
+ 
+            struct appdata_t
+            {
+float4 vertex   :
+                POSITION;
+float4 color    :
+                COLOR;
+float2 texcoord :
+                TEXCOORD0;
+            };
+ 
+            struct v2f
+            {
+float4 vertex   :
+                SV_POSITION;
+fixed4 color :
+                COLOR;
+half2 texcoord  :
+                TEXCOORD0;
+float4 worldPosition :
+                TEXCOORD1;
+            };
+ 
+            fixed4 _Color;
+            fixed4 _TextureSampleAdd;
+            float4 _ClipRect;
+ 
+            float4 _MainTex_TexelSize;
+ 
+            v2f vert(appdata_t IN)
+            {
+                v2f OUT;
+                OUT.worldPosition = IN.vertex;
+                OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);
+ 
+                OUT.texcoord = IN.texcoord;
+ 
+#ifdef UNITY_HALF_TEXEL_OFFSET
+                OUT.vertex.xy += (_ScreenParams.zw - 1.0)*float2(-1,1);
+#endif
+ 
+                OUT.color = IN.color * _Color;
+                return OUT;
+            }
+ 
+            sampler2D _MainTex;
+ 
+            fixed4 frag(v2f IN) : SV_Target
+            {
+                half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;
+ 
+                // color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
+                color.a = 1;
+ 
+#ifdef UNITY_UI_ALPHACLIP
+                clip(color.a - 0.001);
+#endif
+                return color;
+            }
+            ENDCG
+        }
+    }
+}

+ 10 - 0
Assets/BowArrow/Scripts/Components/InputFieldLayoutX_NoTransparent.shader.meta

@@ -0,0 +1,10 @@
+fileFormatVersion: 2
+guid: 4e3d2e09fd46d1b43ae60ab82c3c43db
+ShaderImporter:
+  externalObjects: {}
+  defaultTextures: []
+  nonModifiableTextures: []
+  preprocessorOverride: 0
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 1 - 0
ProjectSettings/GraphicsSettings.asset

@@ -38,6 +38,7 @@ GraphicsSettings:
   - {fileID: 10770, guid: 0000000000000000f000000000000000, type: 0}
   - {fileID: 4800000, guid: df93545e4cca6364891b1ad84ca610cf, type: 3}
   - {fileID: 4800000, guid: b2e62371dd265fd45abe88cc8685221c, type: 3}
+  - {fileID: 4800000, guid: 4e3d2e09fd46d1b43ae60ab82c3c43db, type: 3}
   m_PreloadedShaders: []
   m_SpritesDefaultMaterial: {fileID: 10754, guid: 0000000000000000f000000000000000, type: 0}
   m_CustomRenderPipeline: {fileID: 0}