/// <summary> /// creates a game object over the network /// </summary> /// <param name="t_prefab">the name of the prefab used to create the object</param> /// <param name="t_position">the spawn position of the object</param> /// <param name="t_rotation">the spawn rotation of the object</param> /// <param name="t_data">a serializable object to pass other creation data with</param> /// <param name="t_connectionOrigin">the origin of the create object message, defaults to -1 for local</param> /// <returns>the unique netutil name of the object</returns> public string CreateObject(string t_prefab, Vector3 t_position, Quaternion t_rotation, object t_data, string t_name = "", int t_connectionOrigin = -1) { // ensure the prefab exists if (!m_prefabs.ContainsKey(t_prefab)) { throw new ArgumentException("\"" + t_prefab + "\" is a prefab not registered in netuitl"); } // ensure a dupplicate is not being made if (m_gameObjects.ContainsKey(t_name)) { throw new ArgumentException("\"" + t_name + "\" is already the name of a network gameobject"); } // ensure the data object is valid if (!t_data.GetType().IsSerializable) { throw new ArgumentException("the type \"" + t_data.GetType().FullName + "\" is not serializable and can not be properly sent over network"); } // if no name for the object is given, generate one if (t_name == "") { t_name = genId(); } NetUtilObjectCreator creator = new NetUtilObjectCreator(); creator.name = t_name; creator.prefab = t_prefab; creator.position = new NetUtilVector3(t_position); creator.rotation = new NetUtilQuaternion(t_rotation); creator.data = t_data; if (m_isHost) { m_activeObjects[t_name] = creator; } SendMessage(NetUtilMessageType.CREATE_OBJECT, creator, t_connectionOrigin); CreateLocalObject(t_prefab, t_name, t_position, t_rotation, t_data); if (t_connectionOrigin == -1) { m_localObjects.Add(t_name); } return(t_name); }
/// <summary> /// handles incoming create object messages /// </summary> private void HandleCreateObject(int connectionId, byte[] buffer, int size) { NetUtilObjectCreator message = ConstructType <NetUtilObjectCreator>(buffer, 1, size - 1); CreateObject(message.prefab, message.position.toVector3(), message.rotation.toQuaternion(), message.data, message.name, connectionId); }