This is a singleton manager for all of the networked behaviors
Inheritance: SimpleNetworkedMonoBehavior
示例#1
0
        /// <summary>
        /// Check the argument update with the stream and start index
        /// </summary>
        /// <param name="stream">Stream to be updated</param>
        /// <param name="start">Start index</param>
        public ulong SetupInstantiateId(NetworkingStream stream, int start)
        {
            ulong id = 0;

            if (MethodName == INSTANTIATE_METHOD_NAME)
            {
                string objName = stream.Bytes.GetString(start + 16);

                if (!NetworkingManager.TryPullIdFromObject(objName, ref id))
                {
                    throw new NetworkException("Invalid object being instantiated");
                }

                if (id == 0)
                {
                    throw new NetworkException("Invalid object being instantiated");
                }

                idReplacer = BitConverter.GetBytes(id);

                for (int i = 0; i < idReplacer.Length; i++)
                {
                    stream.Bytes.byteArr[start + sizeof(ulong) + i] = idReplacer[i];
                }
            }

            return(id);
        }
示例#2
0
        public override void Disconnect()
        {
            base.Disconnect();

            ControllingSocket = null;

            Unity.UnityEventObject.onDestroy -= SkipResetOnDestroy;

            if (Threading.ThreadManagement.IsMainThread)
            {
                Destroy(gameObject);
            }
            else
            {
                Unity.MainThreadManager.Run(() => { Destroy(gameObject); });                 // JM: make sure this is run on main thread
            }
            instance = null;
        }
        /// <summary>
        /// Check the argument update with the stream and start index
        /// </summary>
        /// <param name="stream">Stream to be updated</param>
        /// <param name="start">Start index</param>
        public ulong SetupInstantiateId(NetworkingStream stream, int start)
        {
            ulong id = 0;

            if (MethodName == INSTANTIATE_METHOD_NAME)
            {
                // TODO:  Debug log if start != 30 what it is equal to
#if UNITY_EDITOR
                if (start != 30)
                {
                    UnityEngine.Debug.LogError("The start value is not 30 it is " + start);
                }
#endif

                // We add 16 because it is the size of 2 ulongs since the first 2 args of the instantiate are 2 ulongs
                string objName = stream.Bytes.GetString(30 + 16);

                if (!NetworkingManager.TryPullIdFromObject(objName, ref id))
                {
                    throw new NetworkException("Invalid object being instantiated");
                }

                if (id == 0)
                {
                    throw new NetworkException("Invalid object being instantiated");
                }

                idReplacer = BitConverter.GetBytes(id);

                for (int i = 0; i < idReplacer.Length; i++)
                {
                    stream.Bytes.byteArr[start + sizeof(ulong) + i] = idReplacer[i];
                }
            }

            return(id);
        }
示例#4
0
        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);
            }
        }
		public override void Disconnect()
		{
			base.Disconnect();

			ControllingSocket = null;

			Unity.UnityEventObject.onDestroy -= SkipResetOnDestroy;

			if (Threading.ThreadManagement.IsMainThread)
				Destroy(gameObject);
			else
				Unity.MainThreadManager.Run(() => { Destroy(gameObject); }); // JM: make sure this is run on main thread

			instance = null;
		}
		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);
		}
		protected override void Awake()
		{
			instance = this;

			base.Awake();
		}