88using Network . LobbyServices ;
99using Network . NetworkRunnerProvider ;
1010using UI . LobbyPanel . Views ;
11+ using Unity . VisualScripting ;
1112using UnityEngine ;
1213using UnityEngine . AddressableAssets ;
1314using UnityEngine . AddressableAssets . ResourceLocators ;
@@ -115,68 +116,49 @@ public void Dispose() =>
115116 //
116117 private async void OnLobbyCreated ( )
117118 {
118- // 1) Создаём/хостим
119119 try
120120 {
121- await _lobbyService . CreateAndHostAsync ( ) ; // внутри должен успешно стартовать Runner.StartGame(...)
121+ await _lobbyService . CreateAndHostAsync ( ) ;
122122 }
123- catch ( System . Exception e )
123+ catch ( Exception e )
124124 {
125125 Debug . LogError ( $ "Error creating lobby: { e } ") ;
126126 return ;
127127 }
128-
129- // 2) Убеждаемся, что у Runner есть SceneManager
130- var runner = _networkRunnerProvider . Runner ;
131- if ( ! runner )
128+
129+ if ( _networkRunnerProvider . Runner . IsSceneAuthority == false )
132130 {
133- Debug . LogError ( "Runner is null ") ;
131+ Debug . LogWarning ( "This peer is not scene authority; skip LoadScene ") ;
134132 return ;
135133 }
136134
137- if ( runner . SceneManager == null )
138- runner . gameObject . AddComponent < NetworkSceneManagerDefault > ( ) ;
135+ const string SceneAddress = "Room" ;
136+ IList < IResourceLocation > resourceLocations = new List < IResourceLocation > ( ) ;
139137
140- if ( runner . IsSceneAuthority == false )
138+ try
141139 {
142- Debug . LogWarning ( "This peer is not scene authority; skip LoadScene" ) ;
143- return ;
140+ resourceLocations = await Addressables . LoadResourceLocationsAsync ( SceneAddress , typeof ( SceneInstance ) ) . Task . AsUniTask ( ) ;
144141 }
145-
146- // 3) Адрес сцены (Addressable Address). НЕ InternalId!
147- const string sceneAddress = "Room" ; // или "Assets/Scenes/Room.unity"
148-
149- // (Необязательно, но можно проверить, что адрес существует)
150- AsyncOperationHandle < IList < UnityEngine . ResourceManagement . ResourceLocations . IResourceLocation > > handle =
151- Addressables . LoadResourceLocationsAsync ( sceneAddress , typeof ( SceneInstance ) ) ;
152- await handle . Task ;
153-
154- if ( handle . Status != AsyncOperationStatus . Succeeded || handle . Result . Count == 0 )
142+ catch ( Exception e )
155143 {
156- Debug . LogError ( $ "Scene address '{ sceneAddress } ' not found in Addressables.") ;
157- Addressables . Release ( handle ) ;
158- return ;
144+ throw new Exception ( $ "Failed to load scene '{ SceneAddress } ' . Exception: { e } ") ;
159145 }
160146
161- var loc = handle . Result [ 0 ] ;
162- Debug . Log ( $ "Found scene: address={ loc . PrimaryKey } | internal={ loc . InternalId } ") ;
163-
164- // 4) Собираем SceneRef ИМЕННО из Address (PrimaryKey)
165- var sceneRef = SceneRef . FromPath ( loc . PrimaryKey ) ;
147+ if ( resourceLocations . Count == 0 )
148+ throw new Exception ( $ "Not find scene '{ SceneAddress } '") ;
166149
167- Addressables . Release ( handle ) ;
150+ SceneRef sceneRef = SceneRef . FromPath ( resourceLocations [ 0 ] . InternalId ) ;
168151
169- // 5) Грузим через Fusion (Host триггерит, все клиенты подтянутся)
170152 try
171153 {
172- // В Fusion 2 это Task → ToUniTask норм
173- await runner . LoadScene ( sceneRef , LoadSceneMode . Single ) . ToUniTask ( ) ;
174- _stateMachine . ChangeState < RoomState > ( ) ;
154+ await _networkRunnerProvider . Runner . LoadScene ( sceneRef , LoadSceneMode . Single ) . ToUniTask ( ) ;
175155 }
176- catch ( System . Exception e )
156+ catch ( Exception e )
177157 {
178158 Debug . LogError ( $ "Error loading scene via Fusion: { e } ") ;
179159 }
160+
161+ _stateMachine . ChangeState < RoomState > ( ) ;
180162 }
181163 }
182164}
0 commit comments