Exemplo n.º 1
0
        static void EnsureSessionConfigured(MARSSession session, UndoBlock undoBlock, Transform overrideUserRef = null)
        {
            var sessionObject    = session.gameObject;
            var sessionTransform = session.transform;
            var changed          = false;

            // Make sure we have a properly configured MARS Camera
            if (!TestMode && session.m_CameraReference == null)
            {
                var marsCameraRef = GameObjectUtils.ExhaustiveComponentSearch <MARSCamera>(sessionObject);

                // If we can't find a MARS camera, get the main camera and create one on that
                if (marsCameraRef == null)
                {
                    LogRuntimeIssue("MARSCamera not present in the scene.  Adding one to the main camera.");

                    var createdNewCamera = false;
                    var cameraRef        = GameObjectUtils.ExhaustiveTaggedComponentSearch <Camera>(sessionObject, k_CameraTag);
                    if (cameraRef == null)
                    {
                        cameraRef        = CreateCamera(sessionTransform, undoBlock);
                        createdNewCamera = true;
                    }

                    marsCameraRef = undoBlock.AddComponent <MARSCamera>(cameraRef.gameObject);

                    if (!createdNewCamera)
                    {
                        var nearPlane    = cameraRef.nearClipPlane;
                        var cameraParent = cameraRef.transform.parent;
                        if (cameraParent == null)
                        {
                            cameraParent = sessionTransform;
                        }

                        var worldScale         = cameraParent.localScale.x;
                        var scaledMaxNearPlane = k_MaxNearPlane * worldScale;
                        if (nearPlane > scaledMaxNearPlane)
                        {
                            LogRuntimeIssue("Camera near clip plane is greater than the recommended distance. " +
                                            $"Setting near plane from {nearPlane} to {scaledMaxNearPlane}.");

                            undoBlock.RecordObject(cameraRef);
                            cameraRef.nearClipPlane = scaledMaxNearPlane;
                        }
                    }
                }

                session.m_CameraReference = marsCameraRef;
                changed = true;
            }

            const string correctBrokenSessionMessage = "If you have not customized the MARSSession game object, just delete " +
                                                       "the object and go to Create > MARS > Session.";

            var cameraTrans = session.m_CameraReference.transform;

            if (!TestMode && cameraTrans.GetComponentInParent <MARSSession>() == null)
            {
                const string cameraParentRequirementMessage = "MARSCamera must have a MARSSession object in its list of parents.";
                var          canReparent = true;
#if INCLUDE_AR_FOUNDATION
                if (cameraTrans.GetComponentInParent <ARSessionOrigin>() != null)
                {
                    canReparent = false;
                    Debug.LogError($"{cameraParentRequirementMessage} Please make sure the MARSSession component " +
                                   $"is on the same game object as the ARSessionOrigin. {correctBrokenSessionMessage}");
                }
#endif
                if (canReparent)
                {
                    LogRuntimeIssue($"{cameraParentRequirementMessage} Re-parenting.");
                    var cameraParent = cameraTrans.parent;
                    if (cameraParent)
                    {
                        sessionTransform.position   = cameraParent.position;
                        sessionTransform.rotation   = cameraParent.rotation;
                        sessionTransform.localScale = cameraParent.localScale;
                    }

                    undoBlock.SetTransformParent(cameraTrans, sessionTransform);
                    changed = true;
                }
            }

            // Look for the user object, if it's not in the scene, make a new one
            var userRef = session.m_UserReference;
            if (!TestMode && userRef == null)
            {
                userRef = session.m_CameraReference.transform.Find(k_UserName);
                if (userRef == null)
                {
                    if (overrideUserRef == null)
                    {
                        userRef = GameObjectUtils.Instantiate(MARSRuntimePrefabs.instance.UserPrefab).transform;
                    }
                    else
                    {
                        userRef = overrideUserRef;
                    }

                    userRef.name          = k_UserName;
                    userRef.parent        = session.m_CameraReference.transform;
                    userRef.localPosition = Vector3.zero;
                    userRef.localRotation = Quaternion.identity;
                    userRef.localScale    = Vector3.one;
                    undoBlock.RegisterCreatedObject(userRef.gameObject);
                }

                session.m_UserReference = userRef;
                changed = true;
            }

            // One more bizarre scenario to catch - the MARS Session and Camera being *one* object.
            // We can't just delete the MARS Session since the user might have other scripts there, so we just throw up a warning telling them to fix things manually
            if (!TestMode && session.m_CameraReference.transform == sessionTransform)
            {
                Debug.LogError("The MARS Session should be a parent of the MARSCamera, *NOT* the same object!  " +
                               $"Please correct this in your scene! {correctBrokenSessionMessage}");
            }

            if (sessionObject.activeInHierarchy == false)
            {
                LogRuntimeIssue("There is a MARS Session object in your scene that is *not* active.  Running your scene with an inactive MARS Session will cause problems.");
                sessionObject.SetActive(true);
                changed = true;
            }

            var cameraObject = session.cameraReference.gameObject;
            if (cameraObject.activeInHierarchy == false)
            {
                LogRuntimeIssue("There is a MARS Camera GameObject in your scene that is *not* active.  " +
                                "Re-enabling here, as running your scene with an inactive MARS Camera would cause problems.");

                cameraObject.SetActive(true);
                changed = true;
            }

            if (changed)
            {
                DirtySimulatableSceneIfNeeded();
            }
        }
Exemplo n.º 2
0
        static MARSSession CreateSession(UndoBlock undoBlock, bool marsBehaviorsInScene = true)
        {
            const string sessionNotPresentMessage = "MARS Session not present in the scene - creating one now.";

            if (marsBehaviorsInScene)
            {
                LogRuntimeIssue(sessionNotPresentMessage);
            }
            else if (!TestMode)
            {
                Debug.Log(sessionNotPresentMessage);
            }

            GameObject sessionObject = null;
            Camera     cameraRef     = null;

            k_Cameras.Clear();
            GameObjectUtils.GetComponentsInActiveScene(k_Cameras);
            if (k_Cameras.Count > 0)
            {
                cameraRef = k_Cameras[0];
                foreach (var camera in k_Cameras)
                {
                    if (camera.CompareTag(k_CameraTag))
                    {
                        cameraRef = camera;
                        break;
                    }
                }
            }

            if (cameraRef != null && cameraRef.transform.parent != null)
            {
                sessionObject = cameraRef.transform.parent.gameObject;
                var addToParentMessage = "Adding MARS Session component to immediate parent of main camera.";
#if INCLUDE_AR_FOUNDATION
                var arSessionOrigin = cameraRef.GetComponentInParent <ARSessionOrigin>();
                if (arSessionOrigin != null)
                {
                    sessionObject      = arSessionOrigin.gameObject;
                    addToParentMessage = "Adding MARS Session component to AR Session Origin game object.";
                }
#endif

                Debug.Log(addToParentMessage);
                undoBlock.RecordObject(sessionObject);
#if UNITY_EDITOR
                EditorGUIUtility.PingObject(sessionObject);
#endif
            }

            if (sessionObject == null)
            {
                sessionObject = new GameObject(k_ObjectName);
                sessionObject.transform.SetAsFirstSibling();
                undoBlock.RegisterCreatedObject(sessionObject);
            }

            var sessionTrans = sessionObject.transform;
            if (cameraRef == null)
            {
                cameraRef = CreateCamera(sessionTrans, undoBlock);
            }

            var cameraObj     = cameraRef.gameObject;
            var marsCameraRef = cameraObj.GetComponent <MARSCamera>();
            if (!marsCameraRef)
            {
                Debug.Log("Adding MARS Camera component to main camera.");

                // TODO: configure near plane
                marsCameraRef = undoBlock.AddComponent <MARSCamera>(cameraObj);
            }

            var session = undoBlock.AddComponent <MARSSession>(sessionObject);
            Instance = session;
            session.m_CameraReference = marsCameraRef;

            return(session);
        }