void InitEnd(AfterInitCallback callback)
        {
            initialized = true;

            if (applicationIsPlaying)
            {
                GC.Collect();
            }

            if (input != null)
            {
                input.gameObject.SetActive(true);
                input.Init();
            }

            ComputeFirstReusableChunk();

            if (applicationIsPlaying)
            {
                if (OnInitialized != null)
                {
                    OnInitialized();
                }
            }

#if UNITY_EDITOR
            EditorApplication.update -= UpdateInEditor;
            if (renderInEditor && !applicationIsPlaying)
            {
                EditorApplication.update += UpdateInEditor;
            }
#endif

            if (callback != null)
            {
                callback();
            }

            if (initialWaitTime > 0 && applicationIsPlaying)
            {
                input.enabled = false;
                StartCoroutine(DoWaitTime());
            }
            else
            {
                EndWaitTime();
            }
        }
Example #2
0
        void WarmChunksEditor(AfterInitCallback callback)
        {
            int required = 1000;

            if (world != null)
            {
                while (chunksPoolLoadIndex < maxChunks)
                {
                    for (int k = 0; k < 100; k++)
                    {
                        ReserveChunkMemory();
                    }
                    if (chunksPoolLoadIndex > required)
                    {
                        break;
                    }
                }
            }
            InitEnd(callback);
        }
Example #3
0
        IEnumerator WarmChunks(AfterInitCallback callback)
        {
            WaitForEndOfFrame w = new WaitForEndOfFrame();
            int required        = maxChunks;

#if UNITY_EDITOR
            required = prewarmChunksInEditor;
#endif

            if (world != null)
            {
                while (chunksPoolLoadIndex < maxChunks)
                {
                    try {
                        for (int k = 0; k < 100; k++)
                        {
                            ReserveChunkMemory();
                        }
                    } catch (Exception ex) {
                        ShowExceptionMessage(ex);
                        break;
                    }
                    if (enableLoadingPanel)
                    {
                        float progress = (float)(chunksPoolLoadIndex + 1) / required;
                        VoxelPlayUI.instance.ToggleInitializationPanel(true, loadingText, progress);
                    }
                    yield return(w);

                    if (chunksPoolLoadIndex > required)
                    {
                        break;
                    }
                }
            }

            InitEnd(callback);
        }
Example #4
0
        public void Init(AfterInitCallback callback = null)
        {
            initialized          = false;
            sceneCam             = null;
            applicationIsPlaying = Application.isPlaying;
            tempChunks           = new List <VoxelChunk> ();
            tempColliders        = new Collider[1];
            InitMainThreading();

#if UNITY_ANDROID || UNITY_IOS
            isMobilePlatform = true;
#elif UNITY_WEBGL
            isMobilePlatform = false;
                        #if UNITY_EDITOR
            if (PlayerSettings.WebGL.memorySize < 2000)
            {
                PlayerSettings.WebGL.memorySize = 2000;
            }
                        #endif
#else
            isMobilePlatform = Application.isMobilePlatform;
                        #endif

#if UNITY_WEBGL
            effectiveMultithreadGeneration = false;
#else
            effectiveMultithreadGeneration = multiThreadGeneration;
#endif

            // Init camera and Sun references
            if (cameraMain == null)
            {
                cameraMain = Camera.main;
                if (cameraMain == null)
                {
                    cameraMain = FindObjectOfType <Camera> ();
                }
                if (cameraMain == null)
                {
                    Debug.LogError("Voxel Play: No camera found!");
                    return;
                }
            }

            // Cache player collider
            if (characterController == null)
            {
                characterController = FindObjectOfType <VoxelPlayCharacterControllerBase> ();
            }
            if (characterController != null)
            {
                characterControllerCollider = characterController.GetComponent <CharacterController> ();
                if (characterControllerCollider == null)
                {
                    characterControllerCollider = characterController.GetComponent <Collider> ();
                }
            }

                        #if UNITY_EDITOR
            if (cameraMain.actualRenderingPath != RenderingPath.Forward)
            {
                Debug.LogWarning("Voxel Play works better with Forward Rendering path.");
            }
            if (!isMobilePlatform && QualitySettings.antiAliasing < 2)
            {
                Debug.LogWarning("Voxel Play looks better with MSAA enabled (x2 minimum to enable crosshair).");
            }
                        #endif

            if (isMobilePlatform && Application.isPlaying)
            {
                cameraMain.farClipPlane = Mathf.Min(400, _visibleChunksDistance * 16);
            }

            if (UICanvasPrefab == null)
            {
                UICanvasPrefab = Resources.Load <GameObject> ("VoxelPlay/UI/Voxel Play UI Canvas");
            }
            if (crosshairPrefab == null)
            {
                crosshairPrefab = Resources.Load <GameObject> ("VoxelPlay/UI/crosshair");
            }
            if (crosshairTexture == null)
            {
                crosshairTexture = Resources.Load <Texture2D> ("VoxelPlay/UI/crosshairTexture");
            }

                        #if UNITY_EDITOR
            input = new KeyboardMouseController();
                        #else
            if (isMobilePlatform)
            {
                input = new DualTouchController();
            }
            else
            {
                input = new KeyboardMouseController();
            }
                        #endif

            stopWatch = new System.Diagnostics.Stopwatch();

                        #if UNITY_EDITOR
            lastInspectorUpdateTime = 0;
            lastCameraMoveTime      = 0;
                        #endif

            if (!enableBuildMode)
            {
                buildMode = false;
            }

            if (applicationIsPlaying || (!applicationIsPlaying && renderInEditor))
            {
                stopWatch.Start();
                if (cachedChunks == null || chunksPool == null)
                {
                    LoadWorldInt();
                    if (applicationIsPlaying)
                    {
                        StartCoroutine(WarmChunks(callback));
                    }
                    else
                    {
                        WarmChunksEditor(callback);
                    }
                }
            }
            else
            {
                UpdateAmbientProperties();
            }
        }
        public void Init(AfterInitCallback callback = null)
        {
            initialized          = false;
            sceneCam             = null;
            applicationIsPlaying = Application.isPlaying;
            tempChunks           = new List <VoxelChunk> ();
            tempColliders        = new Collider [1];
            InitMainThreading();

#if UNITY_ANDROID || UNITY_IOS
            isMobilePlatform = true;
#elif UNITY_WEBGL
            isMobilePlatform = false;
#if UNITY_EDITOR
            if (PlayerSettings.WebGL.memorySize < 2000)
            {
                PlayerSettings.WebGL.memorySize = 2000;
            }
#endif
#else
            isMobilePlatform = Application.isMobilePlatform;
#endif

#if UNITY_WEBGL
            effectiveMultithreadGeneration = false;
#else
            effectiveMultithreadGeneration = multiThreadGeneration;
#endif

#if !UNITY_EDITOR
            if (isMobilePlatform)
            {
                Application.lowMemory += Application_lowMemory;
            }
#endif

            // Init camera and Sun references
            if (cameraMain == null)
            {
                cameraMain = Camera.main;
                if (cameraMain == null)
                {
                    cameraMain = FindObjectOfType <Camera> ();
                }
                if (cameraMain == null)
                {
                    Debug.LogError("Voxel Play: No camera found!");
                    return;
                }
            }

            // Default distance anchor
            if (distanceAnchor == null && cameraMain != null)
            {
                distanceAnchor = cameraMain.transform;
            }

            // Cache player collider
            if (characterController == null)
            {
                characterController = FindObjectOfType <VoxelPlayCharacterControllerBase> ();
            }
            if (characterController != null)
            {
                characterControllerCollider = characterController.GetComponent <CharacterController> ();
                if (characterControllerCollider == null)
                {
                    characterControllerCollider = characterController.GetComponent <Collider> ();
                }
            }

#if UNITY_EDITOR
            if (cameraMain.actualRenderingPath != RenderingPath.Forward)
            {
                Debug.LogWarning("Voxel Play works better with Forward Rendering path.");
            }
            if (!isMobilePlatform && QualitySettings.antiAliasing < 2)
            {
                Debug.LogWarning("Voxel Play looks better with MSAA enabled (x2 minimum to enable crosshair).");
            }
#endif

            if (isMobilePlatform && applicationIsPlaying && adjustCameraFarClip)
            {
                cameraMain.farClipPlane = Mathf.Min(400, _visibleChunksDistance * CHUNK_SIZE);
            }
            VoxelPlayUI oldUISystem = GetComponent <VoxelPlayUI> ();
            if (oldUISystem != null)
            {
                DestroyImmediate(oldUISystem);
                oldUISystem = null;
            }

            // Default values
            if (crosshairPrefab == null)
            {
                crosshairPrefab = Resources.Load <GameObject> ("VoxelPlay/UI/crosshair");
            }
            if (crosshairTexture == null)
            {
                crosshairTexture = Resources.Load <Texture2D> ("VoxelPlay/UI/crosshairTexture");
            }


            // Input
            if (inputControllerPCPrefab == null)
            {
                inputControllerPCPrefab = Resources.Load <GameObject> ("VoxelPlay/InputControllers/PC/Voxel Play PC Input Controller");
            }
            if (inputControllerMobilePrefab == null)
            {
                inputControllerMobilePrefab = Resources.Load <GameObject> ("VoxelPlay/InputControllers/Mobile/Voxel Play Mobile Input Controller");
            }

            if (applicationIsPlaying)
            {
                // UI
                VoxelPlayUI.Init();
                // Init Input

                GameObject inputPrefab = null;
#if UNITY_EDITOR
                if (isMobilePlatform && previewTouchUIinEditor)
                {
                    inputPrefab = inputControllerMobilePrefab;
                }
                else
                {
                    inputPrefab = inputControllerPCPrefab;
                }
#else
                if (isMobilePlatform)
                {
                    inputPrefab = inputControllerMobilePrefab;
                }
                else
                {
                    inputPrefab = inputControllerPCPrefab;
                }
#endif

                if (inputPrefab != null)
                {
                    GameObject inputGO = Instantiate <GameObject> (inputPrefab);
                    inputGO.name      = inputPrefab.name;
                    inputGO.hideFlags = HideFlags.DontSave;
                    inputGO.SetActive(false);
                    input = inputGO.GetComponent <VoxelPlayInputController> ();
                }
            }

            stopWatch = new System.Diagnostics.Stopwatch();

#if UNITY_EDITOR
            lastInspectorUpdateTime = 0;
            lastCameraMoveTime      = 0;
#endif

            if (!enableBuildMode)
            {
                buildMode = false;
            }

            if (applicationIsPlaying || (!applicationIsPlaying && renderInEditor))
            {
                stopWatch.Start();
                if (cachedChunks == null || chunksPool == null)
                {
                    LoadWorldInt();
                    if (applicationIsPlaying)
                    {
                        StartCoroutine(WarmChunks(callback));
                    }
                    else
                    {
                        WarmChunksEditor(callback);
                    }
                }
            }
            else
            {
                UpdateAmbientProperties();
            }
        }