Beispiel #1
0
        /// <summary>
        /// Invoke the specified function. It's unlikely that you will need to call this function yourself.
        /// </summary>

        static public void FindAndExecute(int channelID, uint objID, string funcName, params object[] parameters)
        {
            TNObject obj = TNObject.Find(channelID, objID);

            if (obj != null)
            {
                if (obj.Execute(funcName, parameters))
                {
                    return;
                }
#if UNITY_EDITOR
                Debug.LogError("[TNet] Unable to execute function '" + funcName + "'. Did you forget an [RFC] prefix, perhaps?\n" +
                               "GameObject: " + GetHierarchy(obj.gameObject), obj.gameObject);
#endif
            }
#if UNITY_EDITOR
            else if (TNManager.isJoiningChannel)
            {
                Debug.Log("[TNet] Trying to execute a function '" + funcName + "' on TNObject #" + objID +
                          " before it has been created.");
            }
            else
            {
                Debug.LogWarning("[TNet] Trying to execute a function '" + funcName + "' on TNObject #" + objID +
                                 " before it has been created.");
            }
#endif
        }
Beispiel #2
0
        /// <summary>
        /// Make sure that this object's ID is actually unique.
        /// </summary>

        void UniqueCheck()
        {
            if (id < 0)
            {
                id = -id;
            }

            if (id == 0)
            {
                uid = GetUniqueID();
            }
            else
            {
                TNObject tobj = Find(channelID, uid);

                if (tobj != null && tobj != this)
                {
                    if (Application.isPlaying)
                    {
                        if (tobj != null)
                        {
                            Debug.LogError("Network ID " + id + " is already in use by " +
                                           GetHierarchy(tobj.gameObject) +
                                           ".\nPlease make sure that the network IDs are unique.", this);
                        }
                        else
                        {
                            Debug.LogError("Network ID of 0 is used by " + GetHierarchy(gameObject) +
                                           "\nPlease make sure that a unique non-zero ID is given to all objects.", this);
                        }
                    }
                    uid = GetUniqueID();
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Invoke the specified function. It's unlikely that you will need to call this function yourself.
        /// </summary>

        static public void FindAndExecute(int channelID, uint objID, byte funcID, params object[] parameters)
        {
            TNObject obj = TNObject.Find(channelID, objID);

            if (obj != null)
            {
                if (obj.Execute(funcID, parameters))
                {
                    return;
                }
#if UNITY_EDITOR
                Debug.LogError("[TNet] Unable to execute function with ID of '" + funcID + "'. Make sure there is a script that can receive this call.\n" +
                               "GameObject: " + GetHierarchy(obj.gameObject), obj.gameObject);
#endif
            }
#if UNITY_EDITOR
            else if (TNManager.isJoiningChannel)
            {
                Debug.Log("[TNet] Trying to execute RFC #" + funcID + " on TNObject #" + objID + " before it has been created.");
            }
            else
            {
                Debug.LogWarning("[TNet] Trying to execute RFC #" + funcID + " on TNObject #" + objID + " before it has been created.");
            }
#endif
        }
Beispiel #4
0
        /// <summary>
        /// Create the TNObject.
        /// </summary>

        protected void CreateTNObject()
        {
            mTNO = GetComponentInParent <TNObject>();

#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                return;
            }
#endif
            if (mTNO != null)
            {
                mTNO.rebuildMethodList = true;
            }
            else if (!ignoreMissingTNO)
            {
                if (!isActiveAndEnabled)
                {
                    return;
                }

                Debug.LogWarning("Your game object is missing a TNObject script needed for network communication.\n" +
                                 "Simply attach a TNObject script to this game object to fix this problem. If instantiating a prefab, " +
                                 "attach it to your prefab instead.", this);

                // Add a TNObject manually to make scripts work properly. Doing so won't make network communication
                // work properly however, so beware! Make sure that a TNObject is present on the same object or any
                // parent of an object containing your TNBehaviour-derived scripts.
                mTNO = gameObject.AddComponent <TNObject>();
                mTNO.rebuildMethodList = true;
            }
        }
Beispiel #5
0
        /// <summary>
        /// Cache the TNObject if it exists.
        /// </summary>

        protected virtual void Awake()
        {
            mTNO = GetComponentInParent <TNObject>();
            if (mTNO != null)
            {
                mTNO.rebuildMethodList = true;
            }
            TNUpdater.AddStart(this);
        }
Beispiel #6
0
        /// <summary>
        /// Finds the specified component on the game object or one of its parents.
        /// </summary>

        static TNObject FindParent(Transform t)
        {
            while (t != null)
            {
                TNObject tno = t.gameObject.GetComponent <TNObject>();
                if (tno != null)
                {
                    return(tno);
                }
                t = t.parent;
            }
            return(null);
        }
Beispiel #7
0
        /// <summary>
        /// Get a new unique object identifier.
        /// </summary>

        static internal uint GetUniqueID()
        {
            foreach (KeyValuePair <int, TNet.List <TNObject> > pair in mList)
            {
                TNet.List <TNObject> list = pair.Value;

                for (int i = 0; i < list.size; ++i)
                {
                    TNObject ts = list[i];
                    if (ts != null && ts.uid > mLastID && ts.uid < 32768)
                    {
                        mLastID = ts.uid;
                    }
                }
            }
            return(++mLastID);
        }
Beispiel #8
0
        void AddTNO()
        {
            mWaitingOnStart = false;

            if (tno == null)
            {
                Debug.LogWarning("Your game object is missing a TNObject script needed for network communication.\n" +
                                 "Simply attach a TNObject script to this game object to fix this problem. If instantiating a prefab, attach it to your prefab instead.", this);

                // Add a TNObject manually to make scripts work properly. Doing so won't make network communication
                // work properly however, so beware! Make sure that a TNObject is present on the same object or any
                // parent of an object containing your TNBehaviour-derived scripts.
                mTNO = gameObject.AddComponent <TNObject>();
                if (Application.isPlaying)
                {
                    mTNO.rebuildMethodList = true;
                }
            }
        }
Beispiel #9
0
        /// <summary>
        /// Register the object with the lists.
        /// </summary>

        void Start()
        {
            if (id == 0)
            {
                mParent = FindParent(transform.parent);
                if (!TNManager.isConnected)
                {
                    return;
                }

                if (mParent == null && Application.isPlaying)
                {
                    Debug.LogError("Objects that are not instantiated via TNManager.Create must have a non-zero ID.", this);
                    return;
                }
            }
            else
            {
                Register();
            }
        }
Beispiel #10
0
        /// <summary>
        /// Retrieve the Tasharen Network Object by ID.
        /// </summary>

        static public TNObject Find(int channelID, uint tnID)
        {
            if (mDictionary == null)
            {
                return(null);
            }
            TNObject tno = null;

            if (channelID == 0)
            {
                // Broadcasts are sent with the channel ID of '0'
                foreach (KeyValuePair <int, TNet.List <TNObject> > pair in mList)
                {
                    TNet.List <TNObject> list = pair.Value;

                    for (int i = 0; i < list.size; ++i)
                    {
                        TNObject ts = list[i];
                        if (ts.id == tnID)
                        {
                            return(ts);
                        }
                    }
                }
            }
            else
            {
                Dictionary <uint, TNObject> dict;
                if (!mDictionary.TryGetValue(channelID, out dict))
                {
                    return(null);
                }
                if (!dict.TryGetValue(tnID, out tno))
                {
                    return(null);
                }
            }
            return(tno);
        }