Beispiel #1
0
    /// <summary>
    /// Create a packet that will send a custom object creation call.
    /// It is expected that the first byte that follows will identify which function will be parsing this packet later.
    /// </summary>

    static public void CreateEx(int rccID, bool persistent, GameObject go, params object[] objs)
    {
        if (go != null)
        {
            int index = IndexOf(go);

            if (isConnected)
            {
                if (index != -1)
                {
                    BinaryWriter writer = mInstance.mClient.BeginSend(Packet.RequestCreate);
                    writer.Write((ushort)index);
                    writer.Write(GetFlag(go, persistent));
                    writer.Write((byte)rccID);
                    writer.WriteArray(objs);
                    EndSend();
                    return;
                }
                else
                {
                    Debug.LogError("\"" + go.name + "\" has not been added to TNManager's list of objects, so it cannot be instantiated.\n" +
                                   "Consider placing it into the Resources folder and passing its name instead.", go);
                }
            }

            objs = BinaryExtensions.CombineArrays(go, objs);
            UnityTools.ExecuteAll(GetRCCs(), (byte)rccID, objs);
            UnityTools.Clear(objs);
        }
    }
Beispiel #2
0
    /// <summary>
    /// Create a packet that will send a custom object creation call.
    /// It is expected that the first byte that follows will identify which function will be parsing this packet later.
    /// </summary>

    static public void CreateEx(int rccID, bool persistent, string path, params object[] objs)
    {
        GameObject go = LoadGameObject(path);

        if (go != null)
        {
            if (isConnected)
            {
                if (mInstance != null && mInstance.mClient.isSwitchingScenes)
                {
                    Debug.LogWarning("Trying to create an object while switching scenes. Call will be ignored.");
                }

                BinaryWriter writer = mInstance.mClient.BeginSend(Packet.RequestCreate);
                byte         flag   = GetFlag(go, persistent);
                writer.Write((ushort)65535);
                writer.Write(flag);
                writer.Write(path);
                writer.Write((byte)rccID);
                writer.WriteArray(objs);
                EndSend();
                return;
            }

            objs = BinaryExtensions.CombineArrays(go, objs);
            UnityTools.ExecuteAll(GetRCCs(), (byte)rccID, objs);
            UnityTools.Clear(objs);
        }
        else
        {
            Debug.LogError("Unable to load " + path);
        }
    }
Beispiel #3
0
    /// <summary>
    /// Create a new game object.
    /// </summary>

    static GameObject CreateGameObject(GameObject prefab, BinaryReader reader)
    {
        if (prefab != null)
        {
            // The first byte is always the type that identifies what kind of data will follow
            byte type = reader.ReadByte();

            if (type == 0)
            {
                // Just a plain game object
                return(Instantiate(prefab) as GameObject);
            }
            else
            {
                // Custom creation function
                object[] objs = reader.ReadArray(prefab);
                object   retVal;

                if (!UnityTools.ExecuteFirst(GetRCCs(), type, out retVal, objs))
                {
                    Debug.LogError("[TNet] Failed to call RCC #" + type + ".\nDid you forget to register it in Awake() via TNManager.AddRCCs?");
                    UnityTools.Clear(objs);
                    return(null);
                }

                UnityTools.Clear(objs);

                if (retVal == null)
                {
                    Debug.LogError("[TNet] Instantiating \"" + prefab.name + "\" via RCC #" + type + " returned null.\nDid you forget to return the game object from your RCC?");
                }
                return(retVal as GameObject);
            }
        }
        return(null);
    }