快速連結

2016年8月8日

Unity內的Delegate.CreateDelegate

真心大崩潰的Delegate.CreateDelegate流程。
筆記一下來紀念我花了一小時發現他給的"ArgumentException" Error意涵(哭奔)


  private Dictionary> m_List = new Dictionary>();
  void Start()
  {
        Init(new string[]{"Item1","Item2"});
  }
  private void Init(string[] _Names)
  {
        int _Count = _Names.Length;
        for (int i = 1; i <  _Count; i++)
        {
            string _Name = _Names[i];
            string _Callback = "On" + _Name;
            MethodInfo _MethodInfo = GetType().GetMethod(_Callback, BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { typeof(string), typeof(string)}, null);
            if (_MethodInfo == null)
            {
                Debug.LogError("[Skill System] Method " + _Callback + " Is Not Exist!");
                continue;
            }

            var _Method = (Action)Delegate.CreateDelegate(typeof(Action), this, _MethodInfo, false);
            m_List.Add(_Name, _Method);
        }
  }


  private void OnItem1(string ItemName, string PlayerID)
  {
  }


  private void OnItem2(string ItemName, string PlayerID)
  {
  }

作為函式儲存資料的m_List之後會在適當的時機點,呼叫對應key的Method。
但主要會筆記這個項目的原因,是因為不知道為何Unity的Delegate.CreateDelegate只能使用Delegate.CreateDelegate(Type type, object target, MethodInfo method, bool throwOnBindFailure)才不會報錯或是無法使用><!不知道是不是因為我寫法出問題Q___Q!!

以上作筆記之。

1 則留言:

歡迎大家留言提問,我會答的都會盡力回答!
如果太久沒出現回應就是我又忘記回來看留言了TAT