「Unity/3d/透明」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→c#のサンプル) |
(→c#のサンプル) |
||
行69: | 行69: | ||
foreach (Transform transform in transforms) | foreach (Transform transform in transforms) | ||
{ | { | ||
− | + | TransparentAllUnderObject(transform.gameObject); // 再帰処理 | |
} | } | ||
} | } | ||
} | } | ||
</pre> | </pre> |
2021年11月24日 (水) 12:30時点における版
(注意:手動では透明にできたが、コードで変更ができない・・)
以下のようなShaderを追加
Shader "Custom/SemiTransparent" { Properties { _Color ("Color", Color) = (1,1,1,1) _MainTex ("Albedo (RGB)", 2D) = "white" {} } SubShader { Tags { "RenderType"="Transparent" "Queue"="Transparent"} LOD 200 Pass{ ZWrite ON ColorMask 0 } CGPROGRAM #pragma surface surf Standard fullforwardshadows alpha:fade #pragma target 3.0 sampler2D _MainTex; struct Input { float2 uv_MainTex; }; fixed4 _Color; void surf (Input IN, inout SurfaceOutputStandard o) { fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color; o.Albedo = c.rgb; o.Metallic = 0; o.Smoothness = 0; o.Alpha = c.a; } ENDCG } FallBack "Diffuse" }
参考:https://nn-hokuson.hatenablog.com/entry/2018/01/23/202530
Materialを新規追加して、そのMaterialのShaderに上記"Custom/SemiTransparent"のShaderを追加
c#のサンプル
TransparentAllUnderObject(obj); public void TransparentAllUnderObject(GameObject obj) { if (obj.GetComponent<SkinnedMeshRenderer>() != null) { Material[] materials = obj.GetComponent<SkinnedMeshRenderer>().materials; materials[0].shader = Shader.Find("Custom/SemiTransparent"); materials[0].SetColor("_Color", new Color(1f, 1f, 1f, 0.8f)); } if (obj.GetComponent<MeshRenderer>() != null) { Material[] materials = obj.GetComponent<MeshRenderer>().materials; materials[0].shader = Shader.Find("Custom/SemiTransparent"); materials[0].SetColor("_Color", new Color(1f, 1f, 1f, 0.8f)); } Transform transforms = obj.GetComponentInChildren<Transform>(); if (transforms.childCount > 0) { foreach (Transform transform in transforms) { TransparentAllUnderObject(transform.gameObject); // 再帰処理 } } }