/// <summary> /// 远端关联事件 /// </summary> /// <param name="name"></param> /// <param name="bindingInfo"></param> protected virtual void BindingEventInvocations(string name, Type componentType, BindingEvent bindingInfo) { var invocations = PropBindingsNode.Body.Descendants.OfType <InvocationExpression>(); var arg0_name = "m_" + name + "." + bindingInfo.bindingTarget; //绑定目标 var arg1_name = GenCodeUtil.GetSourceKeyWord(bindingInfo.bindingSource); //绑定源 var may_invocations = invocations.Where( x => x.Target.ToString().Contains("Binder") && x.Arguments.Count > 0 && x.Arguments.First().ToString() == arg0_name && x.Arguments.ToArray().Length > 1 && x.Arguments.ToArray()[1].ToString() == arg1_name); InvocationExpression invocation = null; if (may_invocations != null) { //两个参数 if (bindingInfo.type == BindingType.Normal) { invocation = may_invocations.Where(x => x.Arguments.Count() == 2).FirstOrDefault(); } //三个参数 else if (bindingInfo.type == BindingType.WithTarget) { invocation = may_invocations.Where(x => x.Arguments.ToArray().Length > 2 && x.Arguments.ToArray()[2].ToString() == "m_" + name).FirstOrDefault(); } } if (invocation == null) { var methodName = "RegistEvent"; UnityEngine.Debug.Assert(bindingInfo.bindingTargetType.type != null, bindingInfo.bindingSource + ";" + bindingInfo.bindingTarget + " type Null"); ///从UnityEvent和UnityEvent<T>派生 if (bindingInfo.bindingTargetType.type.BaseType.IsGenericType) { var arguments = bindingInfo.bindingTargetType.type.BaseType.GetGenericArguments().Select(x => x.FullName).ToList(); if (bindingInfo.type == BindingType.WithTarget) { arguments.Add(componentType.FullName); } methodName = "RegistEvent<" + string.Join(",", arguments.ToArray()) + ">"; } if (!string.IsNullOrEmpty(methodName)) { invocation = new InvocationExpression(); invocation.Target = new MemberReferenceExpression(new IdentifierExpression("Binder"), methodName, new AstType[0]); invocation.Arguments.Add(new IdentifierExpression(arg0_name)); invocation.Arguments.Add(new IdentifierExpression(GenCodeUtil.GetSourceKeyWord(bindingInfo.bindingSource))); if (bindingInfo.type == BindingType.WithTarget) { invocation.Arguments.Add(new IdentifierExpression("m_" + name)); } PropBindingsNode.Body.Add(invocation); } } }
/// <summary> /// 远端member关联 /// </summary> protected virtual void BindingMemberInvocations(string name, BindingShow bindingInfo) { var invocations = PropBindingsNode.Body.Descendants.OfType <InvocationExpression>(); var arg0_name = "m_" + name + "." + bindingInfo.bindingTarget; var arg0 = arg0_name; //UnityEngine.Debug.Log(bindingInfo.bindingTargetType.type); //UnityEngine.Debug.Log(bindingInfo.isMethod); if (!bindingInfo.isMethod) { arg0 = string.Format("x=>{0}=x", arg0_name); } //UnityEngine.Debug.Log(bindingInfo.bindingTarget); //UnityEngine.Debug.Log(bindingInfo.bindingTargetType.type); var arg1 = GenCodeUtil.GetSourceKeyWord(bindingInfo.bindingSource); var invocation = invocations.Where( x => x.Target.ToString().Contains("Binder") && x.Arguments.Count > 0 && x.Arguments.First().ToString().Replace(" ", "") == arg0 && x.Arguments.ToArray()[1].ToString() == arg1).FirstOrDefault(); if (invocation == null) { string methodName = ""; UnityEngine.Debug.Assert(bindingInfo.bindingTargetType.type != null, name + ":" + bindingInfo.bindingSource + " type Null!"); if (!bindingInfo.bindingTargetType.type.IsGenericType) { var typeName = bindingInfo.bindingTargetType.typeName; methodName = string.Format("RegistValueChange<{0}>", typeName); } else { var type = bindingInfo.bindingTargetType.type; var baseName = type.Name.Remove(type.Name.IndexOf("`")); var arguments = type.GetGenericArguments(); baseName += "<"; baseName += string.Join(",", Array.ConvertAll <Type, string>(arguments, x => x.FullName)); baseName += ">"; methodName = string.Format("RegistValueChange<{0}>", baseName); } if (!string.IsNullOrEmpty(methodName)) { invocation = new InvocationExpression(); invocation.Target = new MemberReferenceExpression(new IdentifierExpression("Binder"), methodName, new AstType[0]); invocation.Arguments.Add(new IdentifierExpression(arg0)); invocation.Arguments.Add(new IdentifierExpression(GenCodeUtil.GetSourceKeyWord(bindingInfo.bindingSource))); PropBindingsNode.Body.Add(invocation); } } }
/// <summary> /// 本地事件关联 /// </summary> protected virtual void LocalEventInvocations(string name, BindingEvent bindingInfo, bool bindingAble) { var node = bindingAble ? InitComponentsNode : AwakeNode; var invocations = node.Body.Descendants.OfType <InvocationExpression>(); var targetName = "m_" + name; var invocation = invocations.Where(x => x.Target.ToString().Contains(targetName) && x.Arguments.FirstOrDefault().ToString() == GenCodeUtil.GetSourceKeyWord(bindingInfo.bindingSource)).FirstOrDefault(); var eventName = bindingInfo.bindingTarget;//如onClick if (invocation == null && !string.IsNullOrEmpty(eventName) && !string.IsNullOrEmpty(bindingInfo.bindingSource)) { invocation = new InvocationExpression(); invocation.Target = new MemberReferenceExpression(new MemberReferenceExpression(new IdentifierExpression("m_" + name), eventName, new AstType[0]), "AddListener", new AstType[0]); invocation.Arguments.Add(new IdentifierExpression(bindingInfo.bindingSource)); node.Body.Add(invocation); CompleteMethod(bindingInfo); } }