在写项目的时候,我创建了一个方法里面需要一个int的参数. 我记得是UIEvent Trigger 不能直接传递一个数字,最多只能传递一个GameObject属性过去(=.=那个值不想再组件上定义)
UIButton Message组件不能传递参数.
UIEvent Trigger组件 传递GameObject某个组件的属性过去
感觉这两个都不是很合适就自己写了一个SendMessage组件
如图:
组件类:
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 = [email protected]; public int @int; public string @string; public GameObject @object; void OnClick() { if (tager != null && functionName != "") { switch (MessageType) { case [email protected]: tager.SendMessage(functionName); break; case [email protected]: tager.SendMessage(functionName, @int); break; case [email protected]: tager.SendMessage(functionName, @string); break; case [email protected]: 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 [email protected]: break; case [email protected]: [email protected] = EditorGUILayout.IntField("参数(int)", [email protected]); break; case [email protected]: [email protected] = EditorGUILayout.TextField("参数(string)", [email protected]).ToString(); break; case [email protected]: [email protected] = EditorGUILayout.ObjectField("参数(object)", [email protected], typeof(GameObject)) as GameObject; break; } // 更新编辑后的数据。 serializedObject.ApplyModifiedProperties(); } } }
三个脚本预览图:
时间: 2024-11-03 09:25:33