Пример #1
0
        /// <summary>
        /// Initialize the default values.
        /// </summary>
        protected override void Awake()
        {
            base.Awake();

            m_GameObject       = gameObject;
            m_Transform        = transform;
            m_ForceObject      = m_GameObject.GetCachedComponent <IForceObject>();
            m_Rigidbody        = m_GameObject.GetCachedComponent <Rigidbody>();
            m_AttributeManager = GetComponent <AttributeManager>();
            if (!string.IsNullOrEmpty(m_HealthAttributeName))
            {
                m_HealthAttribute = m_AttributeManager.GetAttribute(m_HealthAttributeName);
            }
            if (!string.IsNullOrEmpty(m_ShieldAttributeName))
            {
                m_ShieldAttribute = m_AttributeManager.GetAttribute(m_ShieldAttributeName);
            }
            m_AliveLayer = m_GameObject.layer;
#if ULTIMATE_CHARACTER_CONTROLLER_MULTIPLAYER
            m_NetworkInfo          = m_GameObject.GetCachedComponent <INetworkInfo>();
            m_NetworkHealthMonitor = m_GameObject.GetCachedComponent <INetworkHealthMonitor>();
            if (m_NetworkInfo != null && m_NetworkHealthMonitor == null)
            {
                Debug.LogError("Error: The object " + m_GameObject.name + " must have a NetworkHealthMonitor component.");
            }
#endif

            if (m_Hitboxes != null && m_Hitboxes.Length > 0)
            {
                m_ColliderHitboxMap = new Dictionary <Collider, Hitbox>();
                for (int i = 0; i < m_Hitboxes.Length; ++i)
                {
                    m_ColliderHitboxMap.Add(m_Hitboxes[i].Collider, m_Hitboxes[i]);
                }
                m_RaycastHits        = new RaycastHit[m_MaxHitboxCollisionCount];
                m_RaycastHitComparer = new Utility.UnityEngineUtility.RaycastHitComparer();
            }

            EventHandler.RegisterEvent(m_GameObject, "OnRespawn", OnRespawn);
        }
Пример #2
0
        /// <summary>
        /// Initializes the default values.
        /// </summary>
        private void Awake()
        {
            if (m_GameObject != null)
            {
                return;
            }

            m_GameObject       = gameObject;
            m_CameraController = m_GameObject.GetCachedComponent <CameraController>();
            if (m_CameraController == null)
            {
                // If the camera controller is null then the component has been added to the first person child camera.
                m_CameraController = gameObject.GetCachedParentComponent <CameraController>();
                m_SwapMask         = SwapMask.FirstPerson;
            }
            else
            {
                var viewTypes = m_CameraController.ViewTypes;
                // The component exists on the main camera GameObject. If there is no child first person camera then the single component is responsible for
                // swapping both first and third person renderer materials.
                for (int i = 0; i < viewTypes.Length; ++i)
                {
                    if (viewTypes[i] is ViewTypes.FirstPerson)
                    {
                        var firstPersonViewType = viewTypes[i] as ViewTypes.FirstPerson;
#if ULTIMATE_CHARACTER_CONTROLLER_UNIVERSALRP || ULTIMATE_CHARACTER_CONTROLLER_HDRP
                        m_SwapMask = firstPersonViewType.OverlayRenderType == ViewTypes.FirstPerson.ObjectOverlayRenderType.SecondCamera ? SwapMask.ThirdPerson : (SwapMask.FirstPerson | SwapMask.ThirdPerson);
#else
                        m_SwapMask = firstPersonViewType.UseFirstPersonCamera ? SwapMask.ThirdPerson : (SwapMask.FirstPerson | SwapMask.ThirdPerson);
#endif
#if UNITY_EDITOR
                        if ((m_SwapMask & SwapMask.ThirdPerson) != 0)
                        {
                            // Ensure the child camera has the Material Swapper component.
                            if (firstPersonViewType.FirstPersonCamera != null)
                            {
                                var firstPersonMaterialSwapper = firstPersonViewType.FirstPersonCamera.GetComponent <MaterialSwapper>();
                                if (firstPersonMaterialSwapper == null)
                                {
                                    Debug.LogWarning("Warning: The First Person Camera should have the Material Swapper component added to the GameObject.");
                                }
                                else
                                {
                                    firstPersonMaterialSwapper.Awake();
                                }
                            }
                        }
#endif
                        break;
                    }
                }
#if THIRD_PERSON_CONTROLLER
                m_ObjectFader = m_CameraController.GetComponent <ThirdPersonController.Camera.ObjectFader>();
                if (m_ObjectFader != null)
                {
                    m_ObjectFader.IndependentCharacterFadeCount += 1;
                }
#endif
            }
            m_Camera = m_CameraController.GetComponent <Camera>();

            // Instantiate the storage objects based on the swap mode.
            if ((m_SwapMask & SwapMask.FirstPerson) != 0)
            {
                m_AddedFirstPersonRenderers     = new HashSet <Renderer>();
                m_FirstPersonRenderers          = new List <Renderer>();
                m_FirstPersonOriginalMaterials  = new List <Material[]>();
                m_FirstPersonInvisibleMaterials = new List <Material[]>();
                m_FirstPersonBaseObjects        = new HashSet <GameObject>();
            }
            if ((m_SwapMask & SwapMask.ThirdPerson) != 0)
            {
                m_AddedThirdPersonRenderers     = new HashSet <Renderer>();
                m_ThirdPersonRenderers          = new List <Renderer>();
                m_ThirdPersonOriginalMaterials  = new List <Material[]>();
                m_ThirdPersonInvisibleMaterials = new List <Material[]>();
            }

            enabled = false;
            EventHandler.RegisterEvent <GameObject>(m_CameraController.gameObject, "OnCameraAttachCharacter", OnAttachCharacter);
        }
Пример #3
0
        /// <summary>
        /// Attaches the component to the specified character.
        /// </summary>
        /// <param name="character">The handler to attach the camera to.</param>
        protected virtual void OnAttachCharacter(GameObject character)
        {
            // Don't do anything if the character is the same.
            if (m_Character != null && character == m_Character)
            {
                return;
            }

            if (m_Character != null)
            {
                // The character is being changed.
                if (m_FirstPersonRenderers != null)
                {
                    m_AddedFirstPersonRenderers.Clear();
                    m_FirstPersonRenderers.Clear();
                    m_FirstPersonOriginalMaterials.Clear();
                    m_FirstPersonInvisibleMaterials.Clear();
                    m_FirstPersonBaseObjects.Clear();
                }
                if (m_ThirdPersonRenderers != null)
                {
                    m_AddedThirdPersonRenderers.Clear();
                    m_ThirdPersonRenderers.Clear();
                    m_ThirdPersonOriginalMaterials.Clear();
                    m_ThirdPersonInvisibleMaterials.Clear();
                }
                EventHandler.UnregisterEvent <bool>(m_Character, "OnCameraChangePerspectives", OnChangePerspectives);
                EventHandler.UnregisterEvent <Item>(m_Character, "OnInventoryAddItem", OnAddItem);
                EventHandler.UnregisterEvent(m_Character, "OnRespawn", OnRespawn);
                m_Character = null;
            }

            enabled = !m_ManualSwap && character != null;
            if (character == null)
            {
                return;
            }

            var characterLocomotion = character.GetCachedComponent <UltimateCharacterLocomotion>();

            m_Character = characterLocomotion.gameObject;
            m_FirstPersonPerspective = characterLocomotion.FirstPersonPerspective;
#if THIRD_PERSON_CONTROLLER
            // Force the character into using third person materials so the correct materials can be cached.
            var perspectiveMonitor = character.GetCachedComponent <ThirdPersonController.Character.PerspectiveMonitor>();
            if (perspectiveMonitor != null)
            {
                perspectiveMonitor.UpdateThirdPersonMaterials(true);
            }
#endif

            if ((m_SwapMask & SwapMask.FirstPerson) != 0)
            {
                var firstPersonBaseObjects = character.GetComponentsInChildren <FirstPersonBaseObject>(true);
                for (int i = 0; i < firstPersonBaseObjects.Length; ++i)
                {
                    CacheFirstPersonRenderers(firstPersonBaseObjects[i].gameObject);
                    // Remember the base objects so they are not added again if a runtime item is picked up.
                    m_FirstPersonBaseObjects.Add(firstPersonBaseObjects[i].gameObject);
                }
            }

            if ((m_SwapMask & SwapMask.ThirdPerson) != 0)
            {
                var thirdPersonObjects = character.GetComponentsInChildren <ThirdPersonObject>(true);
                for (int i = 0; i < thirdPersonObjects.Length; ++i)
                {
                    CacheThirdPersonRenderers(thirdPersonObjects[i].gameObject);
                }
            }

#if THIRD_PERSON_CONTROLLER
            if (perspectiveMonitor != null)
            {
                perspectiveMonitor.UpdateThirdPersonMaterials(false);
            }
#endif

            EventHandler.RegisterEvent <bool>(m_Character, "OnCameraChangePerspectives", OnChangePerspectives);
            EventHandler.RegisterEvent <Item>(m_Character, "OnInventoryAddItem", OnAddItem);
            EventHandler.RegisterEvent(m_Character, "OnRespawn", OnRespawn);

            // Assume the first person objects are not rendering.
            EnableThirdPersonMaterials();
        }