예제 #1
0
        protected override IEnumerator SerializeBin()
        {
            Debug.Log($"Waiting city to be deserialized in order to serialize to '{CityBINPath}'...");
            yield return(new WaitUntil(() => isCityReady));

            Debug.Log($"Serializing '{CityBINPath}'!");

            // ReSharper disable once InvokeAsExtensionMethod
            File.WriteAllBytes(CityBINPath, F.Serialize(CityModel, null));
        }
예제 #2
0
        public override void InvokeAtAwake()
        {
            base.InvokeAtAwake();

            // TODO: Comment this line when you want to see city
            if (!IS_DEMO && !forceDemo)
#pragma warning disable 162
            {
                return;
            }
#pragma warning restore 162

            if (singlePlaneSize == -1)
            {
                singlePlaneSize = 12.2f;
            }

            // The first thing we will do, is fill the fields of city class (to allow it to have what it need)
            if (SConv == null)
            {
                SConv = new SceneConversion(realScaleZoom, singlePlaneSize, conversionFactor, new Vector2(mapWidth, mapHeight));
            }

            string path;

#if !UNITY_WEBGL
            path = CityJSONPath;
#else
            path = CityBINPath;
#endif

            DoesCityFileExists = File.Exists(path);

            if (DoesCityFileExists)
            {
                //ThreadedDebug.Log("Starting loading city from file!");
                Debug.Log("Starting loading city from file!");

                //if (playerObject != null)
                HoldPosition = FirstPersonController.Pos;
                //holdPosition.y = 100;
                //characterController.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionY;

                // TODO: Unfreeze player
                //OnGenerationFinishedEvent += UnfreezePlayer;

                // playerObject.transform.position;

#if !UNITY_WEBGL
                //StartCoroutine(AsyncReadAllFileText(s =>
                //{
                //    city = s.Deserialize<City>();
                //    CityModel = city;
                //    Debug.Log($"Deserialized city with {city.BuildingCount} buildings!");

                //    isCityReady = true;
                //}));
#else
                var url = WebRequestUtils.MakeInitialUrl(path);
                Debug.Log($"City: '{path}' -> '{url}'");
                url.ReadDataFromWebAsync(result =>
                {
                    Func <City> cityAsync = () => F.Deserialize <City>(result, evnt =>
                    {
                        Progress = evnt.Progress;
                    });
                    AsyncHelper.RunAsync(cityAsync, cityResult =>
                    {
                        city      = cityResult;
                        CityModel = city;
                        Debug.Log($"Deserialized city with {city.BuildingCount} buildings!");
                        isCityReady = true;
                    });
                });

                //StartCoroutine(F.AsyncReadFileWithWWW<byte[]>(path, s =>
                //{
                //    city = F.Deserialize<City>(s);
                //    Debug.Log($"Deserialized city with {city.BuildingCount} buildings!");

                //    isCityReady = true;
                //}));
#endif
            }
            else
            {
                if (characterController != null)
                {
                    characterController.enabled = false;
                }

                IsCityLoaded(false);
            }

#if UNITY_WEBGL
            //Debug.Log(CityBINPath);
            //Debug.Log($"Exists: {File.Exists(CityBINPath)}\n" +
            //          $"City exists: {DoesCityFileExists}\n" +
            //          $"if: {!File.Exists(CityBINPath) && DoesCityFileExists}");
            if (!File.Exists(CityBINPath) && DoesCityFileExists)
            {
                Debug.Log("Started coroutine!");
                StartCoroutine(SerializeBin());
            }
#endif
        }