private void Awake()
{
bool callInitialize = false;
if (instance != null)
{
instance.dontDestroyOnLoad = false;
rpcStack = instance.rpcStack;
NetWorker currentSocket = ControllingSocket;
Destroy(instance.gameObject);
ResetForScene(new List<SimpleNetworkedMonoBehavior>(new SimpleNetworkedMonoBehavior[] { this }));
callInitialize = true;
ControllingSocket = currentSocket;
}
else
Unity.UnityEventObject.onDestroy += SkipResetOnDestroy;
List<SimpleNetworkedMonoBehavior> allCurrentNetworkBehaviors = new List<SimpleNetworkedMonoBehavior>();
if (startNetworkedSceneBehaviors != null)
allCurrentNetworkBehaviors.AddRange(startNetworkedSceneBehaviors);
SimpleNetworkedMonoBehavior[] behaviors = FindObjectsOfType<SimpleNetworkedMonoBehavior>().Union(allCurrentNetworkBehaviors).ToArray();
foreach (SimpleNetworkedMonoBehavior behavior in behaviors)
{
foreach (SimpleNetworkedMonoBehavior childBehavior in GetAllSimpleMonoBehaviors(behavior.gameObject))
if (!allCurrentNetworkBehaviors.Contains(childBehavior))
allCurrentNetworkBehaviors.Add(childBehavior);
}
startNetworkedSceneBehaviors = allCurrentNetworkBehaviors.ToArray();
instance = this;
DontDestroyOnLoad(gameObject);
dontDestroyOnLoad = true;
CreateUnityEventObject();
if (networkInstantiates != null)
{
foreach (GameObject obj in networkInstantiates)
{
foreach (GameObject obj2 in networkInstantiates)
{
if (obj != obj2 && obj.name == obj2.name)
Debug.LogError("You have two or more objects in the Network Instantiate array with the name " + obj.name + ", these should be unique");
}
}
}
foreach (GameObject obj in networkInstantiates)
{
if (behaviorsAndRefCount.ContainsKey(obj.name))
continue;
behaviorsAndRefCount.Add(obj.name, GetAllSimpleMonoBehaviors(obj).Length);
if (behaviorsAndRefCount[obj.name] == 0)
throw new NetworkException("The object " + obj.name + " in the prefabs list requires to have at least 1 SimpleNetworkedMonoBehavior attached to it or a child");
}
if (!string.IsNullOrEmpty(resourcesDirectory))
{
foreach (GameObject obj in Resources.LoadAll<GameObject>(resourcesDirectory))
{
if (behaviorsAndRefCount.ContainsKey(obj.name))
continue;
behaviorsAndRefCount.Add(obj.name, GetAllSimpleMonoBehaviors(obj).Length);
if (behaviorsAndRefCount[obj.name] == 0)
throw new NetworkException("The object " + obj.name + " in the resources directory requires to have at least 1 SimpleNetworkedMonoBehavior attached to it or a child");
}
}
if (callInitialize)
Initialize(ControllingSocket);
}