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
我们可以看到,修改之后可读性大大提高,而且出错的概率也会下降很多,同时对于多平台的同时开发移植都很友好。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity3D使用C#计算偏导数可以通过数值方法或者符号方法来实现。 数值方法是一种逼近计算的方式,通过在变量的微小变化范围内进行计算来估计偏导数。这种方法比较简单,但精度较低。 符号方法是通过对函数进行符号化的处理来计算偏导数。这种方法可以得到精确的结果,但实现起来比较复杂,需要对函数进行符号化处理。 下面是一个使用数值方法计算二维函数偏导数的示例代码: ```csharp using UnityEngine; public class DerivativeExample : MonoBehaviour { public float Delta = 0.001f; private float Function(float x, float y) { // 二维函数示例:f(x, y) = x^2 + 2*y return x * x + 2 * y; } private float PartialDerivativeX(float x, float y) { float fx1 = Function(x + Delta, y); float fx2 = Function(x - Delta, y); return (fx1 - fx2) / (2 * Delta); } private float PartialDerivativeY(float x, float y) { float fy1 = Function(x, y + Delta); float fy2 = Function(x, y - Delta); return (fy1 - fy2) / (2 * Delta); } private void Start() { float x = 1f; float y = 2f; float dx = PartialDerivativeX(x, y); float dy = PartialDerivativeY(x, y); Debug.Log("Partial derivative with respect to x: " + dx); Debug.Log("Partial derivative with respect to y: " + dy); } } ``` 请注意,这只是一个简单的示例,可以根据需要进行调整和扩展。使用数值方法计算偏导数,需要选择适当的微小变化范围(Delta),以便在精度和计算效率之间进行平衡。 希望以上信息能对你有所帮助!如有其他问题,请随提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值