Code Review

1 using UnityEngine;
2 using
UnityEditor;
3
4
5 namespace
UnityStandardAssets.Water
6 {
7     
[CustomEditor(typeof(WaterBase))]
8     
public class WaterBaseEditor : Editor
9     {
10         
public GameObject oceanBase;
11         
private WaterBase waterBase;
12         
private Material oceanMaterial = null;
13
14         
private SerializedObject serObj;
15         
private SerializedProperty sharedMaterial;
16
17
18         
public SerializedProperty waterQuality;
19         
public SerializedProperty edgeBlend;
20
21         
public void OnEnable()
22         {
23             serObj =
new SerializedObject(target);
24             sharedMaterial = serObj.FindProperty(
"sharedMaterial");
25             waterQuality = serObj.FindProperty(
"waterQuality");
26             edgeBlend = serObj.FindProperty(
"edgeBlend");
27         }
28
29         
public override void OnInspectorGUI()
30         {
31             serObj.Update();
32
33             waterBase = (WaterBase)serObj.targetObject;
34             oceanBase = ((WaterBase)serObj.targetObject).gameObject;
35             
if (!oceanBase)
36                 
return;
37
38             GUILayout.Label(
"This script helps adjusting water material properties", EditorStyles.miniBoldLabel);
39
40             EditorGUILayout.PropertyField(sharedMaterial,
new GUIContent("Material"));
41             oceanMaterial = (Material)sharedMaterial.objectReferenceValue;
42
43             
if (!oceanMaterial)
44             {
45                 sharedMaterial.objectReferenceValue = (Object)WaterEditorUtility.LocateValidWaterMaterial(oceanBase.transform);
46                 serObj.ApplyModifiedProperties();
47                 oceanMaterial = (Material)sharedMaterial.objectReferenceValue;
48                 
if (!oceanMaterial)
49                     
return;
50             }
51
52             EditorGUILayout.Separator();
53
54             GUILayout.Label(
"Overall Quality", EditorStyles.boldLabel);
55             EditorGUILayout.PropertyField(waterQuality,
new GUIContent("Quality"));
56             EditorGUILayout.PropertyField(edgeBlend,
new GUIContent("Edge blend?"));
57
58             
if (waterQuality.intValue > (int)WaterQuality.Low && !SystemInfo.supportsRenderTextures)
59                 EditorGUILayout.HelpBox(
"Water features not supported", MessageType.Warning);
60             
if (edgeBlend.boolValue && !SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.Depth))
61                 EditorGUILayout.HelpBox(
"Edge blend not supported", MessageType.Warning);
62
63             EditorGUILayout.Separator();
64
65             
bool hasShore = oceanMaterial.HasProperty("_ShoreTex");
66
67             GUILayout.Label(
"Main Colors", EditorStyles.boldLabel);
68             GUILayout.Label(
"Alpha values define blending with realtime textures", EditorStyles.miniBoldLabel);
69
70             WaterEditorUtility.SetMaterialColor(
"_BaseColor", EditorGUILayout.ColorField("Refraction", WaterEditorUtility.GetMaterialColor("_BaseColor", oceanMaterial)), oceanMaterial);
71             WaterEditorUtility.SetMaterialColor(
"_ReflectionColor", EditorGUILayout.ColorField("Reflection", WaterEditorUtility.GetMaterialColor("_ReflectionColor", oceanMaterial)), oceanMaterial);
72
73             EditorGUILayout.Separator();
74
75             GUILayout.Label(
"Main Textures", EditorStyles.boldLabel);
76             GUILayout.Label(
"Used for small waves (bumps), foam and white caps", EditorStyles.miniBoldLabel);
77
78             WaterEditorUtility.SetMaterialTexture(
"_BumpMap", (Texture)EditorGUILayout.ObjectField("Normals", WaterEditorUtility.GetMaterialTexture("_BumpMap", waterBase.sharedMaterial), typeof(Texture), false), waterBase.sharedMaterial);
79             
if (hasShore)
80                 WaterEditorUtility.SetMaterialTexture(
"_ShoreTex", (Texture)EditorGUILayout.ObjectField("Shore & Foam", WaterEditorUtility.GetMaterialTexture("_ShoreTex", waterBase.sharedMaterial), typeof(Texture), false), waterBase.sharedMaterial);
81
82             Vector4 animationTiling;
83             Vector4 animationDirection;
84
85             Vector2 firstTiling;
86             Vector2 secondTiling;
87             Vector2 firstDirection;
88             Vector2 secondDirection;
89
90             animationTiling = WaterEditorUtility.GetMaterialVector(
"_BumpTiling", oceanMaterial);
91             animationDirection = WaterEditorUtility.GetMaterialVector(
"_BumpDirection", oceanMaterial);
92
93             firstTiling =
new Vector2(animationTiling.x * 100.0F, animationTiling.y * 100.0F);
94             secondTiling =
new Vector2(animationTiling.z * 100.0F, animationTiling.w * 100.0F);
95
96             firstTiling = EditorGUILayout.Vector2Field(
"Tiling 1", firstTiling);
97             secondTiling = EditorGUILayout.Vector2Field(
"Tiling 2", secondTiling);
98
99             
//firstTiling.x = EditorGUILayout.FloatField("1st Tiling U", firstTiling.x);
100             
//firstTiling.y = EditorGUILayout.FloatField("1st Tiling V", firstTiling.y);
101             
//secondTiling.x = EditorGUILayout.FloatField("2nd Tiling U", secondTiling.x);
102             
//secondTiling.y = EditorGUILayout.FloatField("2nd Tiling V", secondTiling.y);
103
104             firstDirection =
new Vector2(animationDirection.x, animationDirection.y);
105             secondDirection =
new Vector2(animationDirection.z, animationDirection.w);
106
107             
//firstDirection.x = EditorGUILayout.FloatField("1st Animation U", firstDirection.x);
108             
//firstDirection.y = EditorGUILayout.FloatField("1st Animation V", firstDirection.y);
109             
//secondDirection.x = EditorGUILayout.FloatField("2nd Animation U", secondDirection.x);
110             
//secondDirection.y = EditorGUILayout.FloatField("2nd Animation V", secondDirection.y);
111
112             firstDirection = EditorGUILayout.Vector2Field(
"Direction 1", firstDirection);
113             secondDirection = EditorGUILayout.Vector2Field(
"Direction 2", secondDirection);
114
115             animationTiling =
new Vector4(firstTiling.x / 100.0F, firstTiling.y / 100.0F, secondTiling.x / 100.0F, secondTiling.y / 100.0F);
116             animationDirection =
new Vector4(firstDirection.x, firstDirection.y, secondDirection.x, secondDirection.y);
117
118             WaterEditorUtility.SetMaterialVector(
"_BumpTiling", animationTiling, oceanMaterial);
119             WaterEditorUtility.SetMaterialVector(
"_BumpDirection", animationDirection, oceanMaterial);
120
121             Vector4 displacementParameter = WaterEditorUtility.GetMaterialVector(
"_DistortParams", oceanMaterial);
122             Vector4 fade = WaterEditorUtility.GetMaterialVector(
"_InvFadeParemeter", oceanMaterial);
123
124             EditorGUILayout.Separator();
125
126             GUILayout.Label(
"Normals", EditorStyles.boldLabel);
127             GUILayout.Label(
"Displacement for fresnel, specular and reflection/refraction", EditorStyles.miniBoldLabel);
128
129             
float gerstnerNormalIntensity = WaterEditorUtility.GetMaterialFloat("_GerstnerIntensity", oceanMaterial);
130             gerstnerNormalIntensity = EditorGUILayout.Slider(
"Per Vertex", gerstnerNormalIntensity, -2.5F, 2.5F);
131             WaterEditorUtility.SetMaterialFloat(
"_GerstnerIntensity", gerstnerNormalIntensity, oceanMaterial);
132
133             displacementParameter.x = EditorGUILayout.Slider(
"Per Pixel", displacementParameter.x, -4.0F, 4.0F);
134             displacementParameter.y = EditorGUILayout.Slider(
"Distortion", displacementParameter.y, -0.5F, 0.5F);
135             
// fade.z = EditorGUILayout.Slider("Distance fade", fade.z, 0.0f, 0.5f);
136
137             EditorGUILayout.Separator();
138
139             GUILayout.Label(
"Fresnel", EditorStyles.boldLabel);
140             GUILayout.Label(
"Defines reflection to refraction relation", EditorStyles.miniBoldLabel);
141
142             
if (!oceanMaterial.HasProperty("_Fresnel"))
143             {
144                 
if (oceanMaterial.HasProperty("_FresnelScale"))
145                 {
146                     
float fresnelScale = EditorGUILayout.Slider("Intensity", WaterEditorUtility.GetMaterialFloat("_FresnelScale", oceanMaterial), 0.1F, 4.0F);
147                     WaterEditorUtility.SetMaterialFloat(
"_FresnelScale", fresnelScale, oceanMaterial);
148                 }
149                 displacementParameter.z = EditorGUILayout.Slider(
"Power", displacementParameter.z, 0.1F, 10.0F);
150                 displacementParameter.w = EditorGUILayout.Slider(
"Bias", displacementParameter.w, -3.0F, 3.0F);
151             }
152             
else
153             {
154                 Texture fresnelTex = (Texture)EditorGUILayout.ObjectField(
155                         
"Ramp",
156                         (Texture)WaterEditorUtility.GetMaterialTexture(
"_Fresnel",
157                         oceanMaterial),
158                         
typeof(Texture),
159                         
false);
160                 WaterEditorUtility.SetMaterialTexture(
"_Fresnel", fresnelTex, oceanMaterial);
161             }
162
163             EditorGUILayout.Separator();
164
165             WaterEditorUtility.SetMaterialVector(
"_DistortParams", displacementParameter, oceanMaterial);
166
167             
if (edgeBlend.boolValue)
168             {
169                 GUILayout.Label(
"Fading", EditorStyles.boldLabel);
170
171                 fade.x = EditorGUILayout.Slider(
"Edge fade", fade.x, 0.001f, 3.0f);
172                 
if (hasShore)
173                     fade.y = EditorGUILayout.Slider(
"Shore fade", fade.y, 0.001f, 3.0f);
174                 fade.w = EditorGUILayout.Slider(
"Extinction fade", fade.w, 0.0f, 2.5f);
175
176                 WaterEditorUtility.SetMaterialVector(
"_InvFadeParemeter", fade, oceanMaterial);
177             }
178             EditorGUILayout.Separator();
179
180             
if (oceanMaterial.HasProperty("_Foam"))
181             {
182                 GUILayout.Label(
"Foam", EditorStyles.boldLabel);
183
184                 Vector4 foam = WaterEditorUtility.GetMaterialVector(
"_Foam", oceanMaterial);
185
186                 foam.x = EditorGUILayout.Slider(
"Intensity", foam.x, 0.0F, 1.0F);
187                 foam.y = EditorGUILayout.Slider(
"Cutoff", foam.y, 0.0F, 1.0F);
188
189                 WaterEditorUtility.SetMaterialVector(
"_Foam", foam, oceanMaterial);
190             }
191
192             serObj.ApplyModifiedProperties();
193         }
194
195     }
196 }


Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *



Code gần giống!

Quản lý chương trình xử lý ảnh C#

Miễn phí
1

Phạm Tuấn Khang

Quản lý thư viện LINQ C#

Miễn phí
1

Phạm Tuấn Khang

Quản lý quán karaoke

Miễn phí
1

Phạm Tuấn Khang

Game xếp hình Tetris C#

Miễn phí
1

Phạm Tuấn Khang