示例#1
0
        /// <summary>
        /// Invokes all methods with the given name found in all the components of this GameObjects. Passes along the value
        /// as well if specified. It uses boxing to pass the value, so avoid using SendMessage with parameters in performance
        /// intese sections. You can specify if send message should throw an error when it doesn't find a mathing method.
        /// This is the default behaviour. Shorthand to the GameObject method.
        /// </summary>
        /// <param name="methodName">Name of the method to invoke</param>
        /// <param name="value">Value to be passed to the method</param>
        /// <param name="options">Specify wether an error should be thrown if no method could be found</param>
        public void SendMessage(string methodName, object[] args = null, SendMessageObtions options = SendMessageObtions.DONT_REQUIRE_RECEIVER)
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException("Component", "Called Update SendMessage on disposed object");
            }

            GameObject.SendMessage(methodName, args, options);
        }
示例#2
0
        /// <summary>
        /// Invokes all methods with the given name found in all the components of this GameObjects. Passes along the value
        /// as well if specified. It uses boxing to pass the value, so avoid using SendMessage with parameters in performance
        /// intese sections. You can specify if send message should throw an error when it doesn't find a mathing method.
        /// This is the default behaviour.
        /// </summary>
        /// <param name="methodName">Name of the method to invoke</param>
        /// <param name="value">Value to be passed to the method</param>
        /// <param name="options">Specify wether an error should be thrown if no method could be found</param>
        public void SendMessage(string methodName, object[] args = null, SendMessageObtions options = SendMessageObtions.DONT_REQUIRE_RECEIVER)
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException("GameObject", "Called SendMessage method on disposed object");
            }

            bool hasReceiver = false;

            _components.ForEach((component) =>
            {
                if (!component.IsActive)
                {
                    return;
                }

                MethodInfo method;
                StringBuilder keyBuilder = new StringBuilder(component.ToString());
                keyBuilder.Append(".");
                keyBuilder.Append(methodName);
                string key = keyBuilder.ToString();

                // First try to get the cached method.
                if (!_methodCache.TryGetValue(key, out method))
                {
                    // We haven't used this method before so search for it in the component
                    Type componentType = component.GetType();
                    method             = componentType.GetMethod(methodName, BindingFlags.Instance | BindingFlags.Public);

                    // Cache method info -> Cache null as well so we don't look up the component later if it simply doesn't contain the method
                    if (method != null)
                    {
                        _methodCache.Add(key, method);
                    }
                }

                // Invoke method with the given parameter
                if (method != null)
                {
                    method.Invoke(component, args);
                    hasReceiver = true;
                }
            });

            if (options == SendMessageObtions.REQUIRE_RECEIVER && !hasReceiver)
            {
                throw new NullReferenceException(string.Format("Couldn't find a method with the method name {0} in {1}", methodName, this.ToString()));
            }
        }