Unity扩展 自定义事件Send组件

在写项目的时候,我创建了一个方法里面需要一个int的参数.  我记得是UIEvent Trigger 不能直接传递一个数字,最多只能传递一个GameObject属性过去(=.=那个值不想再组件上定义)

UIButton Message组件不能传递参数.

UIEvent Trigger组件 传递GameObject某个组件的属性过去

感觉这两个都不是很合适就自己写了一个SendMessage组件

如图:

image

image

组件类:

using UnityEngine;
using System.Collections;

namespace PlateFace
{
    /// <summary>
    /// 消息Send组件
    /// </summary>
    public class SendMessageTo : MonoBehaviour {

        public enum MesType
        {
            @default,
            @int,
            @string,
            @object
        }

        public GameObject tager;
        public string functionName;

        public MesType MessageType = MesType.@default;
        public int @int;
        public string @string;
        public GameObject @object;


        void OnClick()
        {
            if (tager != null && functionName != "")
            {
                switch (MessageType)
                {
                    case MesType.@default:
                        tager.SendMessage(functionName);
                        break;
                    case MesType.@int:
                        tager.SendMessage(functionName, @int);
                        break;
                    case MesType.@string:
                        tager.SendMessage(functionName, @string);
                        break;
                    case MesType.@object:
                        tager.SendMessage(functionName, @object);
                        break;
                    default:
                        break;
                }
            }
        }
        
    }
}

InspectorIEdtor扩展

using UnityEngine;
using UnityEditor;
using System.Collections;

namespace PlateFace
{

    [CustomEditor(typeof(SendMessageTo))]
    public class SendMessageToEditor : Editor
    {   
        public override void OnInspectorGUI()
        {
            SendMessageTo item = target as SendMessageTo;
            serializedObject.Update();  // 序列化更新
            
            
            item.tager = EditorGUILayout.ObjectField("目标:", item.tager, typeof(GameObject)) as GameObject;
            item.functionName = EditorGUILayout.TextField("方法名:", item.functionName).ToString();
            EditorGUILayout.PropertyField(serializedObject.FindProperty("MessageType"));

            switch (item.MessageType)
            {
                case SendMessageTo.MesType.@default:

                    break;
                case SendMessageTo.MesType.@int:
                    item.@int = EditorGUILayout.IntField("参数(int)", item.@int);
                    break;
                case SendMessageTo.MesType.@string:
                    item.@string = EditorGUILayout.TextField("参数(string)", item.@string).ToString();
                    break;
                case SendMessageTo.MesType.@object:
                    item.@object = EditorGUILayout.ObjectField("参数(object)", item.@object, typeof(GameObject)) as GameObject;
                    break;
            }

            // 更新编辑后的数据。         
            serializedObject.ApplyModifiedProperties();
        }  
    }
}

三个脚本预览图:

image

posted @ 2015-03-10 16:17  盘子脸  阅读(445)  评论(0编辑  收藏  举报