Пример #1
0
        private void Awake()
        {
            gameObject.layer = LayerMask.NameToLayer("Water");

            if (_waterObject == null)
            {
                return;
            }

            var waterObjectBoxCollider2D      = _waterObject.GetComponent <BoxCollider2D>();
            var waterObjectBuoyancyEffector2D = _waterObject.GetComponent <BuoyancyEffector2D>();

            waterObjectBuoyancyEffector2D.enabled    = false;
            waterObjectBoxCollider2D.usedByComposite = true;

            var rigidbody2D         = gameObject.AddComponent <Rigidbody2D>(); //required by the composite collider
            var compositeCollider2D = gameObject.AddComponent <CompositeCollider2D>();
            var buoyancyEffector2D  = gameObject.AddComponent <BuoyancyEffector2D>();

            rigidbody2D.isKinematic = true;

            compositeCollider2D.geometryType   = CompositeCollider2D.GeometryType.Polygons;
            compositeCollider2D.isTrigger      = true;
            compositeCollider2D.usedByEffector = true;

            buoyancyEffector2D.surfaceLevel    = waterObjectBuoyancyEffector2D.surfaceLevel;
            buoyancyEffector2D.density         = waterObjectBuoyancyEffector2D.density;
            buoyancyEffector2D.flowAngle       = waterObjectBuoyancyEffector2D.flowAngle;
            buoyancyEffector2D.flowMagnitude   = waterObjectBuoyancyEffector2D.flowMagnitude;
            buoyancyEffector2D.flowVariation   = waterObjectBuoyancyEffector2D.flowVariation;
            buoyancyEffector2D.angularDrag     = waterObjectBuoyancyEffector2D.angularDrag;
            buoyancyEffector2D.linearDrag      = waterObjectBuoyancyEffector2D.linearDrag;
            buoyancyEffector2D.colliderMask    = waterObjectBuoyancyEffector2D.colliderMask;
            buoyancyEffector2D.useColliderMask = waterObjectBuoyancyEffector2D.useColliderMask;
        }
Пример #2
0
        void DrawEdgeColliderPropertyField()
        {
            Game2DWater water2D = target as Game2DWater;

            EditorGUI.BeginChangeCheck();
            bool hasEdgeCollider = EditorGUILayout.Toggle(useEdgeCollider2DLabel, water2D.GetComponent <EdgeCollider2D> () != null);

            if (EditorGUI.EndChangeCheck())
            {
                if (hasEdgeCollider)
                {
                    EdgeCollider2D edgeCollider = water2D.gameObject.AddComponent <EdgeCollider2D> ();
                    float          xOffset, yOffset;
                    xOffset             = -water2D.WaterSize.x / 2f;
                    yOffset             = water2D.WaterSize.y / 2f;
                    edgeCollider.points = new [] {
                        new Vector2(xOffset, yOffset),
                        new Vector2(xOffset, -yOffset),
                        new Vector2(-xOffset, -yOffset),
                        new Vector2(-xOffset, yOffset)
                    };
                    water2D.OnValidate();
                }
                else
                {
                    DestroyImmediate(water2D.GetComponent <EdgeCollider2D> ());
                }
            }
        }
Пример #3
0
        private void DrawWaterWireframe()
        {
            Game2DWater    water2D  = target as Game2DWater;
            List <Vector3> vertices = new List <Vector3> ();

            water2D.GetComponent <MeshFilter> ().sharedMesh.GetVertices(vertices);
            int start, end;

            if (water2D.UseCustomBoundaries)
            {
                start = 2;
                end   = vertices.Count - 4;
            }
            else
            {
                start = 0;
                end   = vertices.Count - 2;
            }
            Matrix4x4 localToWorldMatrix = water2D.transform.localToWorldMatrix;

            using (new Handles.DrawingScope(wireframeColor, localToWorldMatrix)) {
                for (int i = start; i <= end; i += 2)
                {
                    Handles.DrawLine(vertices [i], vertices [i + 1]);
                }
            }
        }
Пример #4
0
        private void DrawWaterResizer()
        {
            Game2DWater water2D = target as Game2DWater;
            Bounds      bounds  = water2D.GetComponent <MeshRenderer> ().bounds;
            Vector3     min     = bounds.min;
            Vector3     max     = bounds.max;
            Vector3     center  = bounds.center;

            Vector3 upHandle    = new Vector3(center.x, max.y, center.z);
            Vector3 downHandle  = new Vector3(center.x, min.y, center.z);
            Vector3 rightHandle = new Vector3(max.x, center.y, center.z);
            Vector3 leftHandle  = new Vector3(min.x, center.y, center.z);

            float handlesSize = HandleUtility.GetHandleSize(center) * 0.5f;

            EditorGUI.BeginChangeCheck();
            Vector3 upPos    = Handles.Slider(upHandle, Vector3.up, handlesSize, Handles.ArrowHandleCap, 1f);
            Vector3 downPos  = Handles.Slider(downHandle, Vector3.down, handlesSize, Handles.ArrowHandleCap, 1f);
            Vector3 rightPos = Handles.Slider(rightHandle, Vector3.right, handlesSize, Handles.ArrowHandleCap, 1f);
            Vector3 leftPos  = Handles.Slider(leftHandle, Vector3.left, handlesSize, Handles.ArrowHandleCap, 1f);

            if (EditorGUI.EndChangeCheck())
            {
                Undo.RecordObject(water2D, "changing water size");
                Vector3 newCenter = new Vector3((rightPos.x + leftPos.x) / 2f, (upPos.y + downPos.y) / 2f, center.z);
                upPos    = water2D.transform.worldToLocalMatrix.MultiplyPoint(upPos);
                downPos  = water2D.transform.worldToLocalMatrix.MultiplyPoint(downPos);
                rightPos = water2D.transform.worldToLocalMatrix.MultiplyPoint(rightPos);
                leftPos  = water2D.transform.worldToLocalMatrix.MultiplyPoint(leftPos);
                Vector2 newSize = new Vector2(Mathf.Clamp(rightPos.x - leftPos.x, 0f, float.MaxValue), Mathf.Clamp(upPos.y - downPos.y, 0f, float.MaxValue));
                if (newSize.x > 0f && newSize.y > 0f)
                {
                    if (water2D.UseCustomBoundaries)
                    {
                        float halfWidth = newSize.x / 2f;
                        water2D.SecondCustomBoundary = Mathf.Clamp(water2D.SecondCustomBoundary, -halfWidth, halfWidth);
                        water2D.FirstCustomBoundary  = Mathf.Clamp(water2D.FirstCustomBoundary, -halfWidth, halfWidth);
                    }
                    water2D.WaterSize          = newSize;
                    water2D.transform.position = newCenter;
                    EditorUtility.SetDirty(water2D);
                }
            }

            if (GUI.changed)
            {
                SceneView.RepaintAll();
            }
        }
Пример #5
0
        public override void OnInspectorGUI()
        {
            Game2DWater water2D        = target as Game2DWater;
            bool        hasRefraction  = false;
            bool        hasReflection  = false;
            bool        hasAudioSource = water2D.GetComponent <AudioSource> () != null;

            Material water2DMaterial = water2D.GetComponent <MeshRenderer> ().sharedMaterial;

            if (water2DMaterial)
            {
                hasRefraction = water2DMaterial.IsKeywordEnabled("Water2D_Refraction");
                hasReflection = water2DMaterial.IsKeywordEnabled("Water2D_Reflection");
            }
            if (PrefabUtility.GetPrefabType(target) == PrefabType.Prefab)
            {
                hasRefraction = true;
                hasReflection = true;
            }

            serializedObject.Update();

            if (!isMultiEditing)
            {
                DrawFixScalingField();
            }

            waterPropertiesExpanded.target = EditorGUILayout.Foldout(waterPropertiesExpanded.target, waterPropertiesFoldoutLabel, true);
            using (var group = new EditorGUILayout.FadeGroupScope(waterPropertiesExpanded.faded)) {
                if (group.visible)
                {
                    EditorGUI.indentLevel++;
                    EditorGUI.BeginChangeCheck();
                    EditorGUILayout.LabelField(meshPropertiesLabel, EditorStyles.boldLabel);
                    EditorGUI.BeginChangeCheck();
                    EditorGUILayout.PropertyField(waterSize, waterSizeLabel);
                    EditorGUILayout.PropertyField(subdivisionsCountPerUnit, subdivisionsCountPerUnitLabel);

                    EditorGUILayout.Space();
                    EditorGUILayout.LabelField(wavePropertiesLabel, EditorStyles.boldLabel);
                    EditorGUILayout.PropertyField(stiffness, stiffnessLabel);
                    EditorGUILayout.PropertyField(spread, spreadLabel);
                    EditorGUILayout.Slider(damping, 0f, 1f, dampingLabel);
                    EditorGUILayout.PropertyField(useCustomBoundaries, useCustomBoundariesLabel);
                    if (useCustomBoundaries.boolValue)
                    {
                        EditorGUILayout.PropertyField(firstCustomBoundary, firstCustomBoundaryLabel);
                        EditorGUILayout.PropertyField(secondCustomBoundary, secondCustomBoundaryLabel);
                    }

                    EditorGUILayout.Space();
                    EditorGUILayout.LabelField(disturbancePropertiesLabel, EditorStyles.boldLabel);
                    EditorGUILayout.PropertyField(minimumDisturbance, minimumDisturbanceLabel);
                    EditorGUILayout.PropertyField(maximumDisturbance, maximumDisturbanceLabel);
                    EditorGUILayout.PropertyField(velocityMultiplier, velocityMultiplierLabel);

                    EditorGUILayout.Space();
                    EditorGUILayout.LabelField(collisionPropertiesLabel, EditorStyles.boldLabel);
                    EditorGUILayout.PropertyField(collisionMinimumDepth, collisionMinimumDepthLabel);
                    EditorGUILayout.PropertyField(collisionMaximumDepth, collisionMaximumDepthLabel);
                    EditorGUILayout.PropertyField(collisionMask, collisionMaskLabel);

                    EditorGUILayout.Space();
                    EditorGUILayout.LabelField(miscLabel, EditorStyles.boldLabel);
                    EditorGUILayout.Slider(buoyancyEffectorSurfaceLevel, 0f, 1f, buoyancyEffectorSurfaceLevelLabel);
                    if (!isMultiEditing)
                    {
                        DrawEdgeColliderPropertyField();
                    }

                    EditorGUI.indentLevel--;
                }
            }

            if (hasRefraction)
            {
                refractionPropertiesExpanded.target = EditorGUILayout.Foldout(refractionPropertiesExpanded.target, refractionPropertiesFoldoutLabel, true);
                using (var group = new EditorGUILayout.FadeGroupScope(refractionPropertiesExpanded.faded)) {
                    if (group.visible)
                    {
                        EditorGUI.indentLevel++;
                        EditorGUILayout.PropertyField(refractionCullingMask, cameraCullingMaskLabel);
                        EditorGUILayout.Slider(refractionRenderTextureResizeFactor, 0f, 1f, refractionRenderTextureResizeFactorLabel);
                        EditorGUI.indentLevel--;
                    }
                }
            }

            if (hasReflection)
            {
                reflectionPropertiesExpanded.target = EditorGUILayout.Foldout(reflectionPropertiesExpanded.target, reflectionPropertiesFoldoutLabel, true);
                using (var group = new EditorGUILayout.FadeGroupScope(reflectionPropertiesExpanded.faded)) {
                    if (group.visible)
                    {
                        EditorGUI.indentLevel++;
                        EditorGUILayout.PropertyField(reflectionCullingMask, cameraCullingMaskLabel);
                        EditorGUILayout.Slider(reflectionRenderTextureResizeFactor, 0f, 1f, reflectionRenderTextureResizeFactorLabel);
                        EditorGUILayout.PropertyField(reflectionZOffset, reflectionZOffsetLabel);
                        EditorGUI.indentLevel--;
                    }
                }
            }

            renderingSettingsExpanded.target = EditorGUILayout.Foldout(renderingSettingsExpanded.target, renderingSettingsFoldoutLabel, true);
            using (var group = new EditorGUILayout.FadeGroupScope(renderingSettingsExpanded.faded)) {
                if (group.visible)
                {
                    EditorGUI.indentLevel++;
                    if (hasReflection || hasRefraction)
                    {
                        EditorGUILayout.PropertyField(farClipPlane, farClipPlaneLabel);
                        EditorGUILayout.PropertyField(renderPixelLights, renderPixelLightsLabel);
                        EditorGUILayout.PropertyField(allowMSAA, allowMSAALabel);
                        EditorGUILayout.PropertyField(allowHDR, allowHDRLabel);
                    }
                    DrawSortingLayerField(sortingLayerID, sortingOrder);
                    EditorGUI.indentLevel--;
                }
            }

            if (hasAudioSource)
            {
                audioSettingsExpanded.target = EditorGUILayout.Foldout(audioSettingsExpanded.target, audioSettingsFoldoutLabel, true);
                using (var group = new EditorGUILayout.FadeGroupScope(audioSettingsExpanded.faded)) {
                    if (group.visible)
                    {
                        EditorGUI.indentLevel++;
                        EditorGUILayout.PropertyField(splashAudioClip, splashAudioClipLabel);
                        EditorGUILayout.Slider(minimumAudioPitch, -3f, 3f, minimumAudioPitchLabel);
                        EditorGUILayout.Slider(maximumAudioPitch, -3f, 3f, maximumAudioPicthLabel);
                        EditorGUILayout.HelpBox(audioPitchMessage, MessageType.None, true);
                        EditorGUI.indentLevel--;
                    }
                }
            }
            else
            {
                EditorGUILayout.HelpBox(audioSourceMesage, MessageType.None, true);
            }

            if (!hasRefraction)
            {
                EditorGUILayout.HelpBox(refractionMessage, MessageType.None, true);
            }
            if (!hasReflection)
            {
                EditorGUILayout.HelpBox(reflectionMessage, MessageType.None, true);
            }

            serializedObject.ApplyModifiedProperties();
        }
Пример #6
0
        private void Start()
        {
            if (_waterObject == null)
            {
                return;
            }

            var waterObjectBoxCollider2D = _waterObject.GetComponent <BoxCollider2D>();

            if (waterObjectBoxCollider2D != null)
            {
                waterObjectBoxCollider2D.usedByComposite = true;
            }

            var waterObjectPolygonCollider2D = _waterObject.GetComponent <PolygonCollider2D>();

            if (waterObjectPolygonCollider2D != null)
            {
                waterObjectPolygonCollider2D.usedByComposite = true;
            }

            var waterObjectBuoyancyEffector2D = _waterObject.GetComponent <BuoyancyEffector2D>();

            if (waterObjectBuoyancyEffector2D != null)
            {
                waterObjectBuoyancyEffector2D.enabled = false;

                var buoyancyEffector2D = gameObject.AddComponent <BuoyancyEffector2D>();

                buoyancyEffector2D.surfaceLevel    = _waterObject.MainModule.Position.y - transform.position.y + waterObjectBuoyancyEffector2D.surfaceLevel;
                buoyancyEffector2D.density         = waterObjectBuoyancyEffector2D.density;
                buoyancyEffector2D.flowAngle       = waterObjectBuoyancyEffector2D.flowAngle;
                buoyancyEffector2D.flowMagnitude   = waterObjectBuoyancyEffector2D.flowMagnitude;
                buoyancyEffector2D.flowVariation   = waterObjectBuoyancyEffector2D.flowVariation;
                buoyancyEffector2D.angularDrag     = waterObjectBuoyancyEffector2D.angularDrag;
                buoyancyEffector2D.linearDrag      = waterObjectBuoyancyEffector2D.linearDrag;
                buoyancyEffector2D.colliderMask    = waterObjectBuoyancyEffector2D.colliderMask;
                buoyancyEffector2D.useColliderMask = waterObjectBuoyancyEffector2D.useColliderMask;

                buoyancyEffector2D.hideFlags |= HideFlags.HideInInspector;
            }

            if (!_isConstrained)
            {
                var rigidbody2D = gameObject.AddComponent <Rigidbody2D>(); //required by the composite collider
                rigidbody2D.isKinematic = true;
                rigidbody2D.hideFlags  |= HideFlags.HideInInspector;

                var compositeCollider2D = gameObject.AddComponent <CompositeCollider2D>();
                compositeCollider2D.geometryType   = CompositeCollider2D.GeometryType.Polygons;
                compositeCollider2D.isTrigger      = true;
                compositeCollider2D.usedByEffector = true;
                compositeCollider2D.hideFlags     |= HideFlags.HideInInspector;
            }
            else
            {
                if (waterObjectBoxCollider2D != null)
                {
                    waterObjectBoxCollider2D.enabled = false;

                    var boxCollider2D = gameObject.AddComponent <BoxCollider2D>();
                    boxCollider2D.size           = new Vector2(_constrainedRegionXMax - _constrainedRegionXMin, _waterObject.MainModule.Height);
                    boxCollider2D.offset         = new Vector2((_constrainedRegionXMin + _constrainedRegionXMax) * 0.5f - transform.position.x, 0f);
                    boxCollider2D.isTrigger      = true;
                    boxCollider2D.usedByEffector = true;
                    boxCollider2D.hideFlags     |= HideFlags.HideInInspector;
                }
            }

            InstantiateWaterObjects();

            _waterSurfaceHeighestPoint = _waterObject.MainModule.Height * 0.5f;
        }