コード例 #1
0
        // 开启协程移动,每100毫秒移动一次,并且协程取消的时候会计算玩家真实移动
        // 比方说玩家移动了250毫秒,玩家有新的目标,这时旧的移动协程结束,将计算250毫秒移动的位置,而不是300毫秒移动的位置
        public async ETTask StartMove(CancellationToken cancellationToken)
        {
            Unit unit = this.GetParent <Unit>();

            this.StartPos  = unit.Position;
            this.StartTime = TimeHelper.Now();
            float distance = (this.Target - this.StartPos).magnitude;

            if (Math.Abs(distance) < 0.1f)
            {
                return;
            }


            this.needTime = (long)(distance / this.moveSpeed * 1000);

            TimerComponent timerComponent = Game.Scene.GetComponent <TimerComponent>();

            // 协程如果取消,将算出玩家的真实位置,赋值给玩家
            cancellationToken.Register(() =>
            {
                long timeNow = TimeHelper.Now();
                if (timeNow - this.StartTime >= this.needTime)
                {
                    unit.Position = this.Target;

                    if (this.GetParent <Unit>().UnitType == UnitType.Player)
                    {
                        Console.WriteLine(" MoveComponent-77-unitPos: " + unit.UnitType + "  " + unit.Position.ToString());
                    }
                }
                else
                {
                    float amount  = (timeNow - this.StartTime) * 1f / this.needTime;
                    unit.Position = Vector3.Lerp(this.StartPos, this.Target, amount);

                    if (this.GetParent <Unit>().UnitType == UnitType.Player)
                    {
                        Console.WriteLine(" MoveComponent-87-unitPos: " + unit.UnitType + "  " + unit.Position.ToString());
                    }
                }
            });

            while (true)
            {
                await timerComponent.WaitAsync(50, cancellationToken); ///20190728 把50改为150 又改回为50

                long timeNow = TimeHelper.Now();

                if (timeNow - this.StartTime >= this.needTime)
                {
                    unit.Position = this.Target;

                    if (this.GetParent <Unit>().UnitType == UnitType.Player)
                    {
                        Console.WriteLine(" MoveComponent-104-unitPos: " + unit.UnitType + "  " + unit.Position.ToString());
                    }

                    break;
                }

                float amount = (timeNow - this.StartTime) * 1f / this.needTime;
                unit.Position = Vector3.Lerp(this.StartPos, this.Target, amount);
            }
        }
コード例 #2
0
 public void Awake()
 {
     Instance = this;
 }
コード例 #3
0
        private async ETVoid StartAsync()
        {
            try
            {
                Application.runInBackground = true;
#if UNITY_IOS || UNITY_ANDROID
                // Application.runInBackground = true;
                // Application.targetFrameRate = 60;
                Screen.sleepTimeout = SleepTimeout.NeverSleep;
#endif

#if LOG_ZSX
                var nodes = GameObject.Find("LoginCanvas");
                if (nodes != null)
                {
                    var node = nodes.GetComponentsInChildren <RectTransform>();
                    for (int i = 0; i < node.Length; i++)
                    {
                        node[i].gameObject.SetActive(false);
                    }
                    nodes.SetActive(true);
                }
#endif

                SynchronizationContext.SetSynchronizationContext(OneThreadSynchronizationContext.Instance);

                DontDestroyOnLoad(gameObject);
                Game.EventSystem.Add(DLLType.Model, typeof(Init).Assembly);

                Game.Scene.AddComponent <TimerComponent>();
                Game.Scene.AddComponent <GlobalConfigComponent>();
                Game.Scene.AddComponent <NetOuterComponent>();
                Game.Scene.AddComponent <ResourcesComponent>();
                Game.Scene.AddComponent <LocalResourcesComponent>();
                // Game.Scene.AddComponent<PlayerComponent>();
                // Game.Scene.AddComponent<UnitComponent>();

                Game.Scene.AddComponent <NetHttpComponent>();

                Game.Scene.AddComponent <UIComponent>();
                Game.Scene.AddComponent <NetLineSwitchComponent>();

#if UNITY_IOS
                TimerComponent iOSTimerComponent = Game.Scene.GetComponent <TimerComponent>();
                while (true)
                {
                    await iOSTimerComponent.WaitAsync(200);

                    if (UnityEngine.Application.internetReachability != NetworkReachability.NotReachable)
                    {
                        break;
                    }
                }
#endif

                // 安装包更新
                await InstallPacketHelper.CheckInstallPacket();

                if (InstallPacketHelper.NeedInstallPacket())
                {
                    InstallPacketDownloaderComponent mInstallPacketDownloaderComponent = Game.Scene.GetComponent <InstallPacketDownloaderComponent>();

                    if (null != mInstallPacketDownloaderComponent)
                    {
                        bool mWaitting = true;

                        string[] mArr         = mInstallPacketDownloaderComponent.remoteInstallPacketConfig.Msg.Split(new string[] { "@%" }, StringSplitOptions.None);
                        int      mTmpLanguage = PlayerPrefsMgr.mInstance.GetInt(PlayerPrefsKeys.KEY_LANGUAGE, 2);
                        if (mTmpLanguage < 0)
                        {
                            mTmpLanguage = 0;
                        }
                        int mGroup = mArr.Length / 3;
                        if (mTmpLanguage < mGroup)
                        {
                            UIComponent.Instance.ShowNoAnimation(UIType.UIDialog,
                                                                 new UIDialogComponent.DialogData()
                            {
                                type          = UIDialogComponent.DialogData.DialogType.Commit,
                                title         = mArr[mTmpLanguage],
                                content       = mArr[mTmpLanguage + mGroup],
                                contentCommit = mArr[mTmpLanguage + 2 * mGroup],
                                contentCancel = string.Empty,
                                actionCommit  = () => { mWaitting = false; },
                                actionCancel  = null
                            });

                            TimerComponent mUpdateTimerComponent = Game.Scene.GetComponent <TimerComponent>();
                            while (mWaitting)
                            {
                                await mUpdateTimerComponent.WaitAsync(200);
                            }
                            UIComponent.Instance.Remove(UIType.UIDialog);
                        }
                    }

#if UNITY_ANDROID
                    switch (UnityEngine.Application.internetReachability)
                    {
                    case NetworkReachability.ReachableViaCarrierDataNetwork:
                        // "当前为运行商网络(2g、3g、4g)"
                        string mTmpMsg      = string.Empty;
                        int    mTmpLanguage = PlayerPrefsMgr.mInstance.GetInt(PlayerPrefsKeys.KEY_LANGUAGE, 2);
                        switch (mTmpLanguage)
                        {
                        case 0:
                            mTmpMsg = $"当前使用移动数据,需要消耗{mInstallPacketDownloaderComponent.remoteInstallPacketConfig.ApkSize / (1024 * 1024)}M流量,是否继续下载?";
                            break;

                        case 1:
                            mTmpMsg = $"The current use of mobile data, need to consume {mInstallPacketDownloaderComponent.remoteInstallPacketConfig.ApkSize / (1024 * 1024)} M, whether to download?";
                            break;

                        case 2:
                            mTmpMsg = $"當前使用移動數據,需要消耗{mInstallPacketDownloaderComponent.remoteInstallPacketConfig.ApkSize / (1024 * 1024)}M流量,是否繼續下載?";
                            break;
                        }
                        Game.Scene.GetComponent <UIComponent>().Show(UIType.UICarrierDataNetwork, new UICarrierDataNetworkComponent.UICarrierDataNetworkData()
                        {
                            Msg      = mTmpMsg,
                            Callback = async() =>
                            {
                                await InstallPacketHelper.DownloadInstallPacket();
                                // Log.Debug($"下载完啦!");
                                // todo 调起安装
                                NativeManager.OpenApk(InstallPacketHelper.InstallPacketPath());
                            }
                        }, null, 0);
                        break;

                    case NetworkReachability.ReachableViaLocalAreaNetwork:
                        // wifi网络
                        await InstallPacketHelper.DownloadInstallPacket();

                        // todo 调起安装
                        NativeManager.OpenApk(InstallPacketHelper.InstallPacketPath());
                        return;
                    }
#elif UNITY_IOS
                    UnityEngine.Application.OpenURL(mInstallPacketDownloaderComponent.remoteInstallPacketConfig.IOSUrl);
                    Application.Quit();
#endif
                    return;
                }



                // 下载ab包
                await BundleHelper.CheckDownloadBundle();

                BundleHelper.IsDownloadBundleFinish = false;
                if (BundleHelper.NeedDownloadBundle())
                {
#if UNITY_IOS
                    bool AppStorePack = false;
#if APPStore
                    AppStorePack = true;
#endif
                    InstallPacketDownloaderComponent mInstallPacketDownloaderComponent = Game.Scene.GetComponent <InstallPacketDownloaderComponent>();
                    if (!mInstallPacketDownloaderComponent.remoteInstallPacketConfig.CheckRes && AppStorePack)
                    {
                        BundleHelper.DownloadBundleFinish();
                    }
                    else
#endif
                    {
                        BundleDownloaderComponent mBundleDownloaderComponent = Game.Scene.GetComponent <BundleDownloaderComponent>();
                        switch (UnityEngine.Application.internetReachability)
                        {
                        case NetworkReachability.ReachableViaCarrierDataNetwork:
                            // "当前为运行商网络(2g、3g、4g)"
                            string mTmpMsg      = string.Empty;
                            int    mTmpLanguage = PlayerPrefsMgr.mInstance.GetInt(PlayerPrefsKeys.KEY_LANGUAGE, 2);
                            switch (mTmpLanguage)
                            {
                            case 0:
                                mTmpMsg = $"当前使用移动数据,需要消耗{mBundleDownloaderComponent.TotalSize / (1024 * 1024)}M流量,是否继续下载?";
                                break;

                            case 1:
                                mTmpMsg = $"The current use of mobile data, need to consume {mBundleDownloaderComponent.TotalSize / (1024 * 1024)} M, whether to download?";
                                break;

                            case 2:
                                mTmpMsg = $"當前使用移動數據,需要消耗{mBundleDownloaderComponent.TotalSize / (1024 * 1024)}M流量,是否繼續下載?";
                                break;
                            }
                            Game.Scene.GetComponent <UIComponent>().ShowNoAnimation(UIType.UICarrierDataNetwork, new UICarrierDataNetworkComponent.UICarrierDataNetworkData()
                            {
                                Msg      = mTmpMsg,
                                Callback = async() =>
                                {
                                    await BundleHelper.DownloadBundle();
                                    BundleHelper.IsDownloadBundleFinish = true;
                                },
                            });
                            TimerComponent timerComponent = Game.Scene.GetComponent <TimerComponent>();
                            while (true)
                            {
                                await timerComponent.WaitAsync(1000);

                                if (BundleHelper.IsDownloadBundleFinish)
                                {
                                    break;
                                }
                            }
                            Game.Scene.GetComponent <UIComponent>().Remove(UIType.UICarrierDataNetwork);
                            break;

                        case NetworkReachability.ReachableViaLocalAreaNetwork:
                            // wifi网络
                            await BundleHelper.DownloadBundle();

                            break;
                        }
                    }
                }
                else
                {
                    BundleHelper.DownloadBundleFinish();
                }

                Game.Hotfix.LoadHotfixAssembly();

                // 加载配置
                Game.Scene.GetComponent <ResourcesComponent>().LoadBundle("config.unity3d");
                Game.Scene.AddComponent <ConfigComponent>();
                Game.Scene.GetComponent <ResourcesComponent>().UnloadBundle("config.unity3d");
                Game.Scene.AddComponent <OpcodeTypeComponent>();
                Game.Scene.AddComponent <MessageDispatcherComponent>();

                Game.Hotfix.GotoHotfix();

                // Game.EventSystem.Run(EventIdType.TestHotfixSubscribMonoEvent, "TestHotfixSubscribMonoEvent");
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }