像DoTween插件一样链式调用方法

请先看这是DoTween使用示例

AboutMeBack?.onClick.AddListener(() => {
            AboutCanvasGroup?.DOFade(0, 0.82F)
            .SetEase(Ease.OutCirc)
            .OnComplete(() => {
                AboutCanvasGroup.gameObject.SetActive(false);
            });
        });

下面是我们的简单实现方法,其实就是每次调用完一个方法后让他返回类的实例然后就可以接着继续调用

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

#region 佛祖保佑
/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            佛祖保佑       永无BUG
*/
#endregion

public class Q
{
    public Q DoSwim()
    {
        Debug.Log("Swimming"); 
        return this;
    }

    public Q DoSinging()
    {
        Debug.Log("Singing");
        return this;
    }

    public Q DoWork()
    {
        Debug.Log("Working");
        return this;
    }

    public Q DoCook()
    {
        Debug.Log("Cooking");
        return this;
    }
}

public class NewBehaviourScript : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Q q = new Q()
            .DoCook()
            .DoSinging()
            .DoSwim()
            .DoWork();
    }
}
posted @ 2021-07-23 12:03  yassine  阅读(185)  评论(0编辑  收藏  举报