Exemplo n.º 1
0
        public IEnumerator Load(string id, Transform parent = null)
        {
            OnLoading?.Invoke(LoadingEventType.LoadingStart);

            if (!m_loadedPlayerModels.TryGetValue(id, out GameObject player))
            {
                Progress = 0;
                var handle = Addressables.InstantiateAsync(id, parent.position, parent.rotation);

                while (!handle.IsDone)
                {
                    Progress = handle.PercentComplete;
                    OnLoading?.Invoke(LoadingEventType.LoadingProgressChange);
                    yield return(null);
                }

                Progress = 1;

                if (handle.Status != AsyncOperationStatus.Succeeded)
                {
                    OnLoading?.Invoke(LoadingEventType.LoadingFailed);

                    throw new NullReferenceException("Failed to load prefab : " + id);
                }
                else
                {
                    player = handle.Result;
                    m_loadedPlayerModels.Add(id, player);
                }
            }


            if (!player)
            {
                throw new NullReferenceException("Game object not found in prefab : " + id);
            }
            else
            {
                // Disable existing model
                if (m_playerLook)
                {
                    var oldPlayer = m_playerLook.gameObject;
                    oldPlayer.SetActive(false);
                }

                player.SetActive(true);
                m_playerView = player.GetComponent <TennisPlayerView>();
                m_playerLook = player.GetComponent <PlayerLook>();

                if (m_playerView && m_racket)
                {
                    m_playerView.SetRacket(m_racket.transform);
                    m_playerView.SetShotPoint(m_racket.ShotPoint);
                }

                if (m_playerLook)
                {
                    m_playerLook.SetRootPath(RootAssetPath);
                    m_playerLook.RacketRenderer = m_racket?.RacketRenderer;
                }

                var ikInterpolation = player.GetComponent <IKInterpolation>();
                if (ikInterpolation && m_racket)
                {
                    ikInterpolation.LeftHandTarget  = m_racket.LeftHandIKTarget;
                    ikInterpolation.RightHandTarget = m_racket.RightHandIKTarget;
                }
            }

            if (m_matchData != null)
            {
                m_playerView?.Initialize(m_matchData.MatchPlayer, m_matchData.ReverseDisplay, m_signalBus, m_matchSimulationManager);
                m_playerLook?.InitializeLook(m_matchData.PlayerLookData, m_matchData.TechData, m_matchData.OutfitData, m_matchData.ShoesData);
            }
            else
            {
                m_playerLook?.SetGender((int)Gender);
                m_playerLook?.SetSkin(SkinType);
                SetHairdo(Hairdo);
                m_playerLook?.SetCoiffure(Hairdo, HairColor);
                m_playerLook?.SetWristbands((int)Wristbands, AccessoryColor);
                m_playerView?.UpdateHandedness(Handedness == Handedness.LeftHanded);
            }

            OnLoading?.Invoke(LoadingEventType.LoadingDone);
        }