| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- #if !UNITY_2019_3_OR_NEWER
- #define CINEMACHINE_PHYSICS
- #define CINEMACHINE_PHYSICS_2D
- #endif
- using UnityEngine;
- using UnityEditor;
- using System;
- using System.Collections.Generic;
- namespace Cinemachine.Editor
- {
- #if CINEMACHINE_PHYSICS || CINEMACHINE_PHYSICS_2D
- [CustomEditor(typeof(CinemachineConfiner))]
- [CanEditMultipleObjects]
- internal sealed class CinemachineConfinerEditor : BaseEditor<CinemachineConfiner>
- {
- /// <summary>Get the property names to exclude in the inspector.</summary>
- /// <param name="excluded">Add the names to this list</param>
- protected override void GetExcludedPropertiesInInspector(List<string> excluded)
- {
- base.GetExcludedPropertiesInInspector(excluded);
- CinemachineBrain brain = CinemachineCore.Instance.FindPotentialTargetBrain(Target.VirtualCamera);
- bool ortho = brain != null ? brain.OutputCamera.orthographic : false;
- if (!ortho)
- excluded.Add(FieldPath(x => x.m_ConfineScreenEdges));
- #if CINEMACHINE_PHYSICS && CINEMACHINE_PHYSICS_2D
- if (Target.m_ConfineMode == CinemachineConfiner.Mode.Confine2D)
- excluded.Add(FieldPath(x => x.m_BoundingVolume));
- else
- excluded.Add(FieldPath(x => x.m_BoundingShape2D));
- #endif
- }
- public override void OnInspectorGUI()
- {
- BeginInspector();
- #if CINEMACHINE_PHYSICS && CINEMACHINE_PHYSICS_2D
- if (Target.m_ConfineMode == CinemachineConfiner.Mode.Confine2D)
- {
- #endif
- #if CINEMACHINE_PHYSICS_2D
- if (Target.m_BoundingShape2D == null)
- EditorGUILayout.HelpBox("A Bounding Shape is required.", MessageType.Warning);
- else if (Target.m_BoundingShape2D.GetType() != typeof(PolygonCollider2D)
- && Target.m_BoundingShape2D.GetType() != typeof(CompositeCollider2D))
- {
- EditorGUILayout.HelpBox(
- "Must be a PolygonCollider2D or CompositeCollider2D.",
- MessageType.Warning);
- }
- else if (Target.m_BoundingShape2D.GetType() == typeof(CompositeCollider2D))
- {
- CompositeCollider2D poly = Target.m_BoundingShape2D as CompositeCollider2D;
- if (poly.geometryType != CompositeCollider2D.GeometryType.Polygons)
- {
- EditorGUILayout.HelpBox(
- "CompositeCollider2D geometry type must be Polygons",
- MessageType.Warning);
- }
- }
- #endif
- #if CINEMACHINE_PHYSICS && CINEMACHINE_PHYSICS_2D
- }
- else
- {
- #endif
- #if CINEMACHINE_PHYSICS
- if (Target.m_BoundingVolume == null)
- EditorGUILayout.HelpBox("A Bounding Volume is required.", MessageType.Warning);
- else if (Target.m_BoundingVolume.GetType() != typeof(BoxCollider)
- && Target.m_BoundingVolume.GetType() != typeof(SphereCollider)
- && Target.m_BoundingVolume.GetType() != typeof(CapsuleCollider))
- {
- EditorGUILayout.HelpBox(
- "Must be a BoxCollider, SphereCollider, or CapsuleCollider.",
- MessageType.Warning);
- }
- #endif
- #if CINEMACHINE_PHYSICS && CINEMACHINE_PHYSICS_2D
- }
- #endif
- DrawRemainingPropertiesInInspector();
- }
- [DrawGizmo(GizmoType.Active | GizmoType.Selected, typeof(CinemachineConfiner))]
- private static void DrawColliderGizmos(CinemachineConfiner confiner, GizmoType type)
- {
- CinemachineVirtualCameraBase vcam = (confiner != null) ? confiner.VirtualCamera : null;
- if (vcam != null && confiner.IsValid)
- {
- Matrix4x4 oldMatrix = Gizmos.matrix;
- Color oldColor = Gizmos.color;
- Gizmos.color = Color.yellow;
- #if CINEMACHINE_PHYSICS && CINEMACHINE_PHYSICS_2D
- if (confiner.m_ConfineMode == CinemachineConfiner.Mode.Confine3D)
- {
- #endif
- #if CINEMACHINE_PHYSICS
- Transform t = confiner.m_BoundingVolume.transform;
- Gizmos.matrix = Matrix4x4.TRS(t.position, t.rotation, t.lossyScale);
- Type colliderType = confiner.m_BoundingVolume.GetType();
- if (colliderType == typeof(BoxCollider))
- {
- BoxCollider c = confiner.m_BoundingVolume as BoxCollider;
- Gizmos.DrawWireCube(c.center, c.size);
- }
- else if (colliderType == typeof(SphereCollider))
- {
- SphereCollider c = confiner.m_BoundingVolume as SphereCollider;
- Gizmos.DrawWireSphere(c.center, c.radius);
- }
- else if (colliderType == typeof(CapsuleCollider))
- {
- CapsuleCollider c = confiner.m_BoundingVolume as CapsuleCollider;
- Vector3 size = Vector3.one * c.radius * 2;
- switch (c.direction)
- {
- case 0: size.x = c.height; break;
- case 1: size.y = c.height; break;
- case 2: size.z = c.height; break;
- }
- Gizmos.DrawWireCube(c.center, size);
- }
- else if (colliderType == typeof(MeshCollider))
- {
- MeshCollider c = confiner.m_BoundingVolume as MeshCollider;
- Gizmos.DrawWireMesh(c.sharedMesh);
- }
- else
- {
- // Just draw an AABB - not very nice!
- Gizmos.matrix = oldMatrix;
- Bounds bounds = confiner.m_BoundingVolume.bounds;
- Gizmos.DrawWireCube(t.position, bounds.extents * 2);
- }
- #endif
- #if CINEMACHINE_PHYSICS && CINEMACHINE_PHYSICS_2D
- }
- else
- {
- #endif
- #if CINEMACHINE_PHYSICS_2D
- Transform t = confiner.m_BoundingShape2D.transform;
- Gizmos.matrix = Matrix4x4.TRS(t.position, t.rotation, t.lossyScale);
- Type colliderType = confiner.m_BoundingShape2D.GetType();
- if (colliderType == typeof(PolygonCollider2D))
- {
- PolygonCollider2D poly = confiner.m_BoundingShape2D as PolygonCollider2D;
- for (int i = 0; i < poly.pathCount; ++i)
- DrawPath(poly.GetPath(i), -1);
- }
- else if (colliderType == typeof(CompositeCollider2D))
- {
- CompositeCollider2D poly = confiner.m_BoundingShape2D as CompositeCollider2D;
- Vector2[] path = new Vector2[poly.pointCount];
- Vector2 revertCompositeColliderScale = new Vector2(1f / t.lossyScale.x, 1f / t.lossyScale.y);
- for (int i = 0; i < poly.pathCount; ++i)
- {
- int numPoints = poly.GetPath(i, path);
- for (int j = 0; j < path.Length; ++j)
- {
- path[j] *= revertCompositeColliderScale;
- }
- DrawPath(path, numPoints);
- }
- }
- #endif
- #if CINEMACHINE_PHYSICS && CINEMACHINE_PHYSICS_2D
- }
- #endif
- Gizmos.color = oldColor;
- Gizmos.matrix = oldMatrix;
- }
- }
- static void DrawPath(Vector2[] path, int numPoints)
- {
- if (numPoints < 0)
- numPoints = path.Length;
- if (numPoints > 0)
- {
- Vector2 v0 = path[numPoints-1];
- for (int j = 0; j < numPoints; ++j)
- {
- Vector2 v = path[j];
- Gizmos.DrawLine(v0, v);
- v0 = v;
- }
- }
- }
- }
- #endif
- }
|