Skip to content

Commit 59f8208

Browse files
update
Adjustments based on earlier discussion.
1 parent 2916b67 commit 59f8208

1 file changed

Lines changed: 53 additions & 63 deletions

File tree

com.unity.netcode.gameobjects/Tests/Runtime/TestHelpers/NetcodeIntegrationTest.cs

Lines changed: 53 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4-
using System.IO;
54
using System.Linq;
65
using System.Reflection;
76
using System.Runtime.CompilerServices;
87
using System.Text;
98
using NUnit.Framework;
10-
using Unity.Entities;
119
using Unity.NetCode;
1210
using Unity.Netcode.RuntimeTests;
1311
using Unity.Netcode.Transports.UTP;
14-
using UnityEditor;
1512
using UnityEngine;
1613
using UnityEngine.SceneManagement;
1714
using UnityEngine.TestTools;
@@ -2210,75 +2207,68 @@ protected GameObject CreateNetworkObjectPrefab(string baseName)
22102207
}
22112208

22122209
#if UNIFIED_NETCODE
2213-
22142210
protected GameObject CreateHybridPrefab(string baseName, bool moveToDDOL = true)
22152211
{
22162212
// Prevent from trying to register/spawn when creating this hybrid prefab
2217-
GhostBehaviour.IsCreatingPrefab = true;
22182213
var gameObject = new GameObject
22192214
{
22202215
name = baseName
22212216
};
22222217

2223-
// Wrap the rest in case a bug (or the like) is introduced and an exception is thrown
2224-
// so we can assure to set IsCreatingPrefab back to false.
2225-
try
2226-
{
2227-
// TODO: We might think if there is a better way to handle this, but GhostBehaviour is dependent
2228-
// upon GhostObject having run through its Awake prior to it and NetworkObject needs to access
2229-
// the NetowrkObjectId GhostField which depends upon GhostBehaviour.
2230-
2231-
// Order of operations in how these execute is actually important.
2232-
// GhostObject should execute 1st.
2233-
// NetworkObjectBridge 2nd.
2234-
// NetworkObject 3rd.
2235-
// NetworkBehaviours will execute in the order they are arranged unless otherwise specified.
2236-
var adapter = gameObject.AddComponent<GhostAdapter>();
2237-
var bridge = gameObject.AddComponent<NetworkObjectBridge>();
2238-
var no = gameObject.AddComponent<NetworkObject>();
2239-
2240-
// Ghost specific settings
2241-
no.HasGhost = true;
2242-
no.GhostAdapter = adapter;
2243-
no.HadBridge = true;
2244-
no.NetworkObjectBridge = bridge;
2245-
2246-
// This gets automatically disabled when NetworkObject is validated.
2247-
no.SynchronizeTransform = false;
2248-
no.SceneMigrationSynchronization = false;
2249-
2250-
// Make sure this is not a scene object
2251-
no.IsSceneObject = false;
2252-
2253-
// TODO: This might be part of the CreateHybridPrefab parameters
2254-
// For now, just use normal interpolation until we get integration
2255-
// tests running.
2256-
// Once we have validated prediction works and have a working manual
2257-
// test, we can circle back to this (possibly make that a sub-task
2258-
// with the dependency to prediction manual test).
2259-
adapter.SupportedGhostModes = GhostModeMask.Interpolated;
2260-
2261-
// Mark the reference as post processing to avoid registering this instance automatically
2262-
GhostPrefabReference.s_IsPostProcessing = true;
2263-
adapter.prefabReference = ScriptableObject.CreateInstance<GhostPrefabReference>();
2264-
adapter.prefabReference.name = "GhostPrefabReference";
2265-
adapter.prefabReference.Prefab = gameObject;
2266-
adapter.prefabReference.Ghost = adapter;
2267-
GhostPrefabReference.s_IsPostProcessing = false;
2268-
2269-
// Turn it into a test prefab
2270-
NetcodeIntegrationTestHelpers.MakeNetworkObjectTestPrefab(no);
2271-
if (moveToDDOL)
2272-
{
2273-
Object.DontDestroyOnLoad(gameObject);
2274-
}
2275-
}
2276-
catch (Exception ex)
2277-
{
2278-
Debug.LogException(ex);
2218+
// Order of operations in how these execute is actually important.
2219+
// GhostObject should execute 1st.
2220+
// NetworkObjectBridge 2nd.
2221+
// NetworkObject 3rd.
2222+
// NetworkBehaviours will execute in the order they are arranged unless otherwise specified.
2223+
2224+
// When adding a Hybrid/Ghost prefab:
2225+
// - We disabled the GameObject prior to adding the GhostPrefabReference (so IsPrefab() == true).
2226+
// - Add the GhostAdapter and GhostPrefabReference
2227+
// - Then set it back to active.
2228+
gameObject.SetActive(false);
2229+
var adapter = gameObject.AddComponent<GhostAdapter>();
2230+
// Mark the reference as post processing to avoid registering this instance automatically.
2231+
GhostPrefabReference.s_IsPostProcessing = true;
2232+
adapter.prefabReference = ScriptableObject.CreateInstance<GhostPrefabReference>();
2233+
adapter.prefabReference.name = "GhostPrefabReference";
2234+
adapter.prefabReference.Prefab = gameObject;
2235+
adapter.prefabReference.Ghost = adapter;
2236+
GhostPrefabReference.s_IsPostProcessing = false;
2237+
2238+
// TODO: This might be part of the CreateHybridPrefab parameters
2239+
// For now, just use normal interpolation until we get integration
2240+
// tests running.
2241+
// Once we have validated prediction works and have a working manual
2242+
// test, we can circle back to this (possibly make that a sub-task
2243+
// with the dependency to prediction manual test).
2244+
adapter.SupportedGhostModes = GhostModeMask.Interpolated;
2245+
2246+
// Once done with setting up the GhostAdapter, we can set it back to active in the hierarchy
2247+
gameObject.SetActive(true);
2248+
2249+
// GhostBehaviours that are part of a prefab will not invoke Ghost.InternalAcquireEntityReference
2250+
// Add the bridge
2251+
var bridge = gameObject.AddComponent<NetworkObjectBridge>();
2252+
2253+
// Now add NGO components
2254+
var no = gameObject.AddComponent<NetworkObject>();
2255+
2256+
// NetworkObject Ghost specific settings
2257+
no.HasGhost = true;
2258+
no.GhostAdapter = adapter;
2259+
no.HadBridge = true;
2260+
no.NetworkObjectBridge = bridge;
2261+
2262+
// Disable transform synchronization for NetworkObject serialization
2263+
// since that is handled by the GhostAdapter.
2264+
no.SynchronizeTransform = false;
2265+
2266+
// Turn it into a test prefab
2267+
NetcodeIntegrationTestHelpers.MakeNetworkObjectTestPrefab(no);
2268+
if (moveToDDOL)
2269+
{
2270+
Object.DontDestroyOnLoad(gameObject);
22792271
}
2280-
// Always reset
2281-
GhostBehaviour.IsCreatingPrefab = false;
22822272
return gameObject;
22832273
}
22842274
#endif

0 commit comments

Comments
 (0)