protected virtual void OnSceneGUI() { NearInteractionTouchable t = (NearInteractionTouchable)target; if (Event.current.type == EventType.Repaint) { UnityEditor.Handles.color = handleColor; Vector3 center = t.transform.TransformPoint(t.localCenter); float arrowSize = UnityEditor.HandleUtility.GetHandleSize(center) * 0.75f; UnityEditor.Handles.ArrowHandleCap(0, center, Quaternion.LookRotation(t.transform.rotation * t.localForward), arrowSize, EventType.Repaint); Vector3 rightDelta = t.transform.localToWorldMatrix.MultiplyVector(t.LocalRight * t.bounds.x / 2); Vector3 upDelta = t.transform.localToWorldMatrix.MultiplyVector(t.localUp * t.bounds.y / 2); Vector3[] points = new Vector3[4]; points[0] = center + rightDelta + upDelta; points[1] = center - rightDelta + upDelta; points[2] = center - rightDelta - upDelta; points[3] = center + rightDelta - upDelta; UnityEditor.Handles.DrawSolidRectangleWithOutline(points, fillColor, handleColor); } }
public override void OnInspectorGUI() { base.OnInspectorGUI(); NearInteractionTouchable t = (NearInteractionTouchable)target; RectTransform rt = t.GetComponent <RectTransform>(); if (rt != null) { // Resize Helper if (rt.sizeDelta != t.bounds) { UnityEditor.EditorGUILayout.HelpBox("Bounds do not match the RectTransform size", UnityEditor.MessageType.Warning); if (GUILayout.Button("Fix Bounds")) { UnityEditor.Undo.RecordObject(t, "Fix Bounds"); t.bounds = rt.sizeDelta; } } if (t.GetComponentInParent <Canvas>() != null && t.localForward != new Vector3(0, 0, -1)) { UnityEditor.EditorGUILayout.HelpBox("Unity UI generally has forward facing away from the front. The LocalForward direction specified does not match the expected forward direction.", UnityEditor.MessageType.Warning); if (GUILayout.Button("Fix Forward Direction")) { UnityEditor.Undo.RecordObject(t, "Fix Forward Direction"); t.localForward = new Vector3(0, 0, -1); } } } }
public override void OnPreSceneQuery() { if (Rays == null) { Rays = new RayStep[1]; } // Check proximity NearInteractionTouchable newClosestTouchable = null; { closestDistance = distFront; // NOTE: Start at distFront for cutoff foreach (var prox in NearInteractionTouchable.Instances) { if (prox.ColliderEnabled) { float dist = prox.DistanceToSurface(Position); if (dist < closestDistance) { closestDistance = dist; newClosestTouchable = prox; } } } } // Determine ray direction Vector3 rayDirection = Rotation * Vector3.forward; if (newClosestTouchable != null) { rayDirection = -newClosestTouchable.Forward; } // Build ray (poke from in front to the back of the pointer position) Vector3 start = Position - distBack * rayDirection; Vector3 end = Position + distFront * rayDirection; Rays[0].UpdateRayStep(ref start, ref end); // Check if the currently touched object is still part of the new touchable. if (currentTouchableObjectDown != null) { if (!IsObjectPartOfTouchable(currentTouchableObjectDown, newClosestTouchable)) { TryRaisePokeUp(Result.CurrentPointerTarget, Position); } } line.SetPosition(0, Position); line.SetPosition(1, end); // Set new touchable only now: If we have to raise a poke-up event for the previous touchable object, // we need to to so using the previous touchable in TryRaisePokeUp(). closestProximityTouchable = newClosestTouchable; IsActive = IsNearObject; visuals.SetActive(IsNearObject); }
private static bool IsObjectPartOfTouchable(GameObject targetObject, NearInteractionTouchable touchable) { return(targetObject != null && touchable != null && (targetObject == touchable.gameObject || // Descendant game objects are touchable as well. In particular, this is needed to be able to send // touch events to Unity UI control elements. (targetObject.transform != null && touchable.gameObject.transform != null && targetObject.transform.IsChildOf(touchable.gameObject.transform)))); }
public override void OnInspectorGUI() { base.OnInspectorGUI(); NearInteractionTouchable t = (NearInteractionTouchable)target; BoxCollider bc = t.GetComponent <BoxCollider>(); RectTransform rt = t.GetComponent <RectTransform>(); if (bc != null) { // project size to local coordinate system Vector2 adjustedSize = new Vector2( Math.Abs(Vector3.Dot(bc.size, t.LocalRight)), Math.Abs(Vector3.Dot(bc.size, t.LocalUp))); // Resize helper if (adjustedSize != t.Bounds) { UnityEditor.EditorGUILayout.HelpBox("Bounds do not match the BoxCollider size", UnityEditor.MessageType.Warning); if (GUILayout.Button("Fix Bounds")) { UnityEditor.Undo.RecordObject(t, "Fix Bounds"); t.Bounds = adjustedSize; } } // Recentre helper if (t.LocalCenter != bc.center + Vector3.Scale(bc.size / 2.0f, t.LocalForward)) { UnityEditor.EditorGUILayout.HelpBox("Center does not match the BoxCollider center", UnityEditor.MessageType.Warning); if (GUILayout.Button("Fix Center")) { UnityEditor.Undo.RecordObject(t, "Fix Center"); t.LocalCenter = bc.center + Vector3.Scale(bc.size / 2.0f, t.LocalForward); } } } else if (rt != null) { // Resize Helper if (rt.sizeDelta != t.Bounds) { UnityEditor.EditorGUILayout.HelpBox("Bounds do not match the RectTransform size", UnityEditor.MessageType.Warning); if (GUILayout.Button("Fix Bounds")) { UnityEditor.Undo.RecordObject(t, "Fix Bounds"); t.Bounds = rt.sizeDelta; } } if (t.GetComponentInParent <Canvas>() != null && t.LocalForward != new Vector3(0, 0, -1)) { UnityEditor.EditorGUILayout.HelpBox("Unity UI generally has forward facing away from the front. The LocalForward direction specified does not match the expected forward direction.", UnityEditor.MessageType.Warning); if (GUILayout.Button("Fix Forward Direction")) { UnityEditor.Undo.RecordObject(t, "Fix Forward Direction"); t.SetLocalForward(new Vector3(0, 0, -1)); } } } // Perpendicular forward/up vectors helpers if (!t.AreLocalVectorsOrthogonal) { UnityEditor.EditorGUILayout.HelpBox("Local Forward and Local Up are not perpendicular.", UnityEditor.MessageType.Warning); if (GUILayout.Button("Fix Local Up")) { UnityEditor.Undo.RecordObject(t, "Fix Local Up"); t.SetLocalForward(t.LocalForward); } if (GUILayout.Button("Fix Local Forward")) { UnityEditor.Undo.RecordObject(t, "Fix Local Forward"); t.SetLocalUp(t.LocalUp); } } }