Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Load/unload scene

Discussion in 'Project Tiny' started by Car3man, Sep 6, 2019.

  1. Car3man

    Car3man

    Joined:
    Mar 3, 2017
    Posts:
    4
    unity 2019.2/project tiny 0.16.1

    im use SceneService.LoadSceneAsync(request.TargetScene); to load scene
    and SceneService.UnloadAllSceneInstances(request.SceneToUnload);

    but if i unload scene on button click, app stuck (not crash, log empty after click)
    app stuck only if button by which im click locate in same scene which im unload
     
  2. Car3man

    Car3man

    Joined:
    Mar 3, 2017
    Posts:
    4
    I found solution, here is my code scene manager

    Code (CSharp):
    1.  
    2. using Unity.Entities;
    3. using Unity.Tiny.Core;
    4. using Unity.Tiny.Scenes;
    5.  
    6. namespace TemplateTinyProject
    7. {
    8.     public class LoadSceneTransitionSystem : ComponentSystem
    9.     {
    10.         private Entity requestEntity;
    11.         private LoadSceneTransitionRequest request;
    12.         private TinyEnvironment environment;
    13.  
    14.         protected override void OnCreate()
    15.         {
    16.             environment = World.TinyEnvironment();
    17.             base.OnCreate();
    18.         }
    19.  
    20.         protected override void OnUpdate()
    21.         {
    22.             requestEntity = Entity.Null;
    23.             request = LoadSceneTransitionRequest.Null;
    24.  
    25.             CurrentLoadedScene currentLoadedScene = environment.GetConfigData<CurrentLoadedScene>();
    26.             Entities.ForEach((Entity entity, ref LoadSceneTransitionRequest request) => {
    27.                 if (this.request.Equals(LoadSceneTransitionRequest.Null) && !request.Equals(LoadSceneTransitionRequest.Null)) {
    28.                     this.request = request;
    29.                     this.requestEntity = entity;
    30.                 }
    31.             });
    32.  
    33.             if (!request.Equals(LoadSceneTransitionRequest.Null)) {
    34.                 //if current scene is null or current scene not qual target scene we load new scene
    35.                 if (currentLoadedScene.SceneEntity.Equals(Entity.Null) || (!currentLoadedScene.SceneEntity.Equals(Entity.Null) && !currentLoadedScene.SceneReference.Equals(request.TargetScene))) {
    36.                     Entity newScene = SceneService.LoadSceneAsync(request.TargetScene);
    37.  
    38.                     if (currentLoadedScene.SceneEntity != Entity.Null) {
    39.                         SceneService.UnloadAllSceneInstances(currentLoadedScene.SceneEntity);
    40.                     }
    41.  
    42.                     environment.SetConfigData(new CurrentLoadedScene() {
    43.                         SceneEntity = newScene,
    44.                         SceneReference = request.TargetScene
    45.                     });
    46.                 }
    47.             }
    48.  
    49.             if (requestEntity != Entity.Null && EntityManager.Exists(requestEntity)) {
    50.                 EntityManager.DestroyEntity(requestEntity);
    51.             }
    52.         }
    53.     }
    54. }
    55.  
    Code (CSharp):
    1.  
    2. using System;
    3. using System.Collections.Generic;
    4. using Unity.Entities;
    5. using Unity.Tiny.Scenes;
    6.  
    7. namespace TemplateTinyProject
    8. {
    9.     public struct LoadSceneTransitionRequest : IComponentData, IEquatable<LoadSceneTransitionRequest>
    10.     {
    11.         public SceneReference TargetScene;
    12.  
    13.         private static LoadSceneTransitionRequest @null = new LoadSceneTransitionRequest() {
    14.             TargetScene = SceneReference.Null
    15.         };
    16.         public static LoadSceneTransitionRequest Null {
    17.             get {
    18.                 return @null;
    19.             }
    20.         }
    21.  
    22.         public override bool Equals(object obj)
    23.         {
    24.             return Equals((LoadSceneTransitionRequest)obj);
    25.         }
    26.  
    27.         public bool Equals(LoadSceneTransitionRequest other)
    28.         {
    29.             return TargetScene.SceneGuid.Equals(other.TargetScene.SceneGuid);
    30.         }
    31.  
    32.         public override int GetHashCode()
    33.         {
    34.             var hashCode = 710859412;
    35.             hashCode = hashCode * -1521134295 + EqualityComparer<SceneReference>.Default.GetHashCode(TargetScene);
    36.             return hashCode;
    37.         }
    38.     }
    39. }
    40.  
     
unityunity