Unity3D跨平台时partial分部方法的使用

最近看到项目中插件的一部分逻辑,发现问题多多,可读性很差,并且容易出错,于是随手整理了下逻
辑。
Unity3D的插件逻辑,因为要考虑到针对各平台的移植,因此会大片的出现#if/#endif等条件编译,当
然,为了简化逻辑,我们可以使用Conditional属性,但当需要处理的逻辑规模(函数个数规模、函数逻
辑规模、针对的平台规模等等)达到一定程度时,仍然会显得繁琐,这时候可能就需要寻找新的处理方法
:
可以使用partial分部类/方法的方式解决问题。我们先来看一下修改前的逻辑:
//Sample.cs
public class Sample
{
#if UNITY_ANDROID
  private AndroidJavaClass _agent = null;
  private AndroidJavaObject _content = null;
  
  public AndroidJavaClass Agent
  {
    get { return _agent; }
  }
  public AndroidJavaObject Content
  {
    get { return _content; }
  }
#elif UNITY_IPHONE

#endif

#region singleton...
  private Sample()
  {
#if UNITY_EDITOR
    Debug.Log("Editor: Sample--------------------");
#elif UNITY_ANDROID
    try
    {
      _agent = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
      _content = _agent.GetStatic<AndroidJavaObject>("currentActivity");
    }
    catch (System.Exception ex)
    {
      Debug.LogError(ex != null ? ex.Message : "<null>");
    }
#elif UNITY_IPHONE

#endif
  }
#endregion

  public void Func1(string _param1, string _param2)
  {
#if UNITY_ANDROID
    if (Content != null)
    {
      Content.Call(......);
    }
#elif UNITY_IPHONE
    ......
#endif
  }


  public void Func2()
  {
#if UNITY_ANDROID
    if (Content != null)
    {
      Content.Call("Func2");
    }
#elif UNITY_IPHONE

#endif
  }

  public string Func3()
  {
#if UNITY_ANDROID
    if (Content != null)
      return Content.Call<string>("Func3");
    else
    {
      return "12345678";
    }
#elif UNITY_IOS
    return "23456789";
#endif
    return "34567890";
  }

  public int Func4()
  {
#if UNITY_ANDROID
    if (Content != null)
    {
      return Content.Call<int>("Func4");
    } //此处隐藏无返回的情况
#elif UNITY_IPHONE
    return 100;
#endif
    return 100; //编译时的警告:执行不到的情况等等
  }
  ......
  ......
}
对比修改之后的内容:
//Sample.cs
public partial class Sample
{
  partial void init(); //针对构造函数

  partial void func1(string _param1, string _param2);
  partial void func2();
  partial void func3(ref string _param1);
  partial void func4(ref int _param1);
}
public partial class Sample
{
#region singleton...
  private Sample()
  {
    init();
  }
#endregion

  public void Func1(string _param1, string _param2)
  {
    func1(_param1, _param2);
  }


  public void Func2()
  {
    func2();
  }

  public string Func3()
  {
    string tResult = "";
    func3(ref tResult);
    return tResult;
  }

  public int Func4()
  {
    int tResult = "";
    func4(ref tResult);
    return tResult;
  }
}
以下针对平台可以分别放在不同文件:
//Sample4Android.cs
#if !UNITY_EDITOR && UNITY_ANDROID
public partial class Sample
{
  private AndroidJavaClass _agent = null;
  private AndroidJavaObject _content = null;

  public AndroidJavaClass Agent
  {
    get { return _agent; }
  }
  public AndroidJavaObject Content
  {
    get { return _content; }
  }

  partial void init()
  {
    try
    {
      _agent = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
      _content = _agent.GetStatic<AndroidJavaObject>("currentActivity");
    }
    catch (System.Exception ex)
    {
      Debug.LogError(ex != null ? ex.Message : "<null>");
    }
  }

  partial void func1(string _param1, string _param2)
  {
    if (Content != null)
    {
      Content.Call(......);
    }
  }
  partial void func2()
  {
    if (Content != null)
    {
      Content.Call("Func2");
    }
  }
  partial void func3(ref string _param1)
  {
    _param1 = Content != null ? Content.Call<string>("Func3") : "12345678";
  }
  partial void func4(ref int _param1)
  {
    _param1 = Content != null ? Content.Call<int>("Func4") : 100;
  }
}
#endif

//Sample4Editor.cs
#if UNITY_EDITOR
public partial class Sample
{

}
#endif

//Sample4IPhone.cs
#if !UNITY_EDITOR && (UNITY_IPHONE || UNITY_IOS)
public partial class Sample
{

}
#endif
我们可以看到,修改之后可读性大大提高,而且出错的概率也会下降很多,同时对于多平台的同时开发移植都很友好。

原文地址:https://www.cnblogs.com/wayland/p/5011172.html