示例#1
0
        /// <summary>
        /// Sets the view type to the object with the specified type.
        /// </summary>
        /// <param name="type">The type of the ViewType which should be set.</param>
        /// <param name="immediateTransition">Should the ViewType be transitioned immediately?</param>
        public void SetViewType(System.Type type, bool immediateTransition)
        {
            if ((m_ViewType != null && m_ViewType.GetType() == type) || type == null)
            {
                return;
            }

            // The ViewTypes may not be deserialized yet.
            if (m_ViewTypeNameMap.Count == 0)
            {
                DeserializeViewTypes();
            }

            int index;

            if (!m_ViewTypeNameMap.TryGetValue(type.FullName, out index))
            {
                Debug.LogError("Error: Unable to find the view type with name " + type.FullName);
                return;
            }

            float pitch = 0f, yaw = 0f;
            var   characterRotation = Quaternion.identity;

            // ViewType will be null on startup.
            if (m_ViewType != null && m_Character != null && Application.isPlaying)
            {
                pitch             = m_ViewType.Pitch;
                yaw               = m_ViewType.Yaw;
                characterRotation = m_ViewType.CharacterRotation;
                m_ViewType.ChangeViewType(false, 0, yaw, characterRotation);

                EventHandler.ExecuteEvent(m_GameObject, "OnCameraChangeViewTypes", m_ViewType, false);
                if (m_OnChangeViewTypesEvent != null)
                {
                    m_OnChangeViewTypesEvent.Invoke(m_ViewType, false);
                }
            }

            var originalViewType = m_ViewType;

            m_ViewTypeFullName = type.FullName;
            m_ViewType         = m_ViewTypes[index];

            // Keep the first/third person view type updated to be able to switch back to the last active type.
            if (m_ViewType.FirstPersonPerspective)
            {
                m_FirstPersonViewTypeFullName = m_ViewTypeFullName;
                m_FirstPersonViewType         = m_ViewType;
            }
            else
            {
                m_ThirdPersonViewTypeFullName = m_ViewTypeFullName;
                m_ThirdPersonViewType         = m_ViewType;
            }

            // If the original view type is not null then the view type has been changed at runtime. Transition to that new view type.
            if (originalViewType != null && m_Character != null && Application.isPlaying)
            {
                m_ViewType.ChangeViewType(true, pitch, yaw, characterRotation);

                EventHandler.ExecuteEvent(m_GameObject, "OnCameraChangeViewTypes", m_ViewType, true);
                if (m_OnChangeViewTypesEvent != null)
                {
                    m_OnChangeViewTypesEvent.Invoke(m_ViewType, true);
                }
                if (originalViewType.FirstPersonPerspective != m_ViewType.FirstPersonPerspective)
                {
                    EventHandler.ExecuteEvent <bool>(m_Character, "OnCameraWillChangePerspectives", m_ViewType.FirstPersonPerspective);
                }

                // Use the transitioner if it exists.
                if (!immediateTransition && m_Transitioner != null)
                {
                    // StartTransition will return success if the transition is started.
                    if (m_Transitioner.StartTransition(originalViewType, m_ViewType))
                    {
                        return;
                    }
                    else if (m_Transitioner.IsTransitioning)
                    {
                        m_Transitioner.StopTransition(false);
                    }
                }
                else
                {
                    // If there isn't a transitioner then immediately move to the target position.
                    if (m_ViewType.RotatePriority)
                    {
                        KinematicObjectManager.SetCameraRotation(m_KinematicObjectIndex, m_ViewType.Rotate(0, 0, true));
                        KinematicObjectManager.SetCameraPosition(m_KinematicObjectIndex, m_ViewType.Move(true));
                    }
                    else
                    {
                        KinematicObjectManager.SetCameraPosition(m_KinematicObjectIndex, m_ViewType.Move(true));
                        KinematicObjectManager.SetCameraRotation(m_KinematicObjectIndex, m_ViewType.Rotate(0, 0, true));
                    }
                }

                // Execute the perspective event if the transitioner does not exist or is not active. The transitioner will execute this event when it finishes.
                if (originalViewType.FirstPersonPerspective != m_ViewType.FirstPersonPerspective)
                {
                    EventHandler.ExecuteEvent <bool>(m_Character, "OnCameraChangePerspectives", m_ViewType.FirstPersonPerspective);

                    if (m_OnChangePerspectivesEvent != null)
                    {
                        m_OnChangePerspectivesEvent.Invoke(m_ViewType.FirstPersonPerspective);
                    }
                }
            }
        }