unity制作软件试用版功能

1、软件试用基本上没有什么难点,就是联网获取时间,然后与规定试用时间对比,超过了就强行退出App,没有超过就不做操作,话不多说,上代码:

//当前年、月、日、时、分、秒
    float curYear, curMonth, curDay, curHours, curMin, curSec;
    //截至XX年
    public const float year = 2023;
    //截至XX月
    public const float month = 10;
    //截至XX日
    public const float day = 31;
    //截至XX时
    public const float hours = 00;
    //截至XX分
    public const float min = 00;
    //截至XX秒
    public const float sec = 0;
    async void Awake()
    {
        await timerAsync();
    }
    async Task timerAsync()
    {
        char[] zifi = new char[] { '-', ':', ';', };
        var json = await new HttpClient().GetStringAsync($"https://apps.game.qq.com/CommArticle/app/reg/gdate.php");
        string shijian = json.Remove(0, 20);
        var fengge = shijian.Split(zifi);
        curYear = float.Parse(shijian.Substring(0, 4));
        Debug.Log($"{shijian}");
        curMonth = float.Parse(fengge[0]);

        curDay = float.Parse(fengge[2].Substring(0, 2));
        curHours = float.Parse(fengge[2].Substring(2, 3));
        curMin = float.Parse(fengge[3]);
        curSec = float.Parse(fengge[4].Substring(0, 2));
        Debug.Log($"原格式=>{shijian}");
        Debug.Log($"转换后格式=>{curYear}{curMonth}{curDay}{curHours}{curMin}{curSec}");
    }
    private void FixedUpdate()
    {
        curYear = System.DateTime.Now.Year;
        curMonth = System.DateTime.Now.Month;
        curDay = System.DateTime.Now.Day;
        curHours = System.DateTime.Now.Hour;
        curMin = System.DateTime.Now.Minute;
        curSec = System.DateTime.Now.Second;
        if (curYear > year)
        {
#if UNITY_EDITOR
            UnityEditor.EditorApplication.isPlaying = false;
#endif
#if UNITY_STANDALONE_WIN
            Application.Quit();
#endif
#if UNITY_ANDROID
                    Application.Quit();
#endif
        }
        else if (curYear < year)
        {
            return;
        }
        else if (curYear == year)
        {
            if (curMonth > month)
            {
#if UNITY_EDITOR
                UnityEditor.EditorApplication.isPlaying = false;
#endif
#if UNITY_STANDALONE_WIN
                Application.Quit();
#endif
#if UNITY_ANDROID
                    Application.Quit();
#endif
            }
            else if (curMonth < month)
            {
                return;
            }
            else if (curMonth == month)
            {
                if (curDay > day)
                {
#if UNITY_EDITOR
                    UnityEditor.EditorApplication.isPlaying = false;
#endif
#if UNITY_STANDALONE_WIN
                    Application.Quit();
#endif
#if UNITY_ANDROID
                    Application.Quit();
#endif
                }
                else if (curDay < day)
                {
                    return;
                }
                else if (curDay == day)
                {
                    if (curHours > hours)
                    {
#if UNITY_EDITOR
                        UnityEditor.EditorApplication.isPlaying = false;
#endif
#if UNITY_STANDALONE_WIN
                        Application.Quit();
#endif
#if UNITY_ANDROID
                    Application.Quit();
#endif
                    }
                    else if (curHours < hours)
                    {
                        return;
                    }
                    else if (curHours == hours)
                    {
                        if (curMin > min)
                        {
#if UNITY_EDITOR
                            UnityEditor.EditorApplication.isPlaying = false;
#endif
#if UNITY_STANDALONE_WIN
                            Application.Quit();
#endif
#if UNITY_ANDROID
                    Application.Quit();
#endif
                        }
                        else if (curMin < min)
                        {
                            return;
                        }
                        else if (curMin == min)
                        {
                            if (curSec > sec)
                            {
#if UNITY_EDITOR
                                UnityEditor.EditorApplication.isPlaying = false;
#endif
#if UNITY_STANDALONE_WIN
                                Application.Quit();
#endif
#if UNITY_ANDROID
                    Application.Quit();
#endif
                            }
                            else if (curSec <= sec)
                            {
                                return;
                            }
                        }
                    }
                }
            }
        }
    }
    private void OnGUI()
    {
        GUIStyle gs = new GUIStyle();
        gs.fontSize = 20;
        gs.normal.textColor = Color.white;
        GUI.Label(new Rect(750, 30, 200, 60), "体验版:截止日期为" + year + "年" + month + "月" + day + "日" + min + "分" + sec + "秒", gs);
    }

我也是网上看的,但是忘记原作者了,我自己简略修改了部分东西,原作者如果看到了可以联系我删除

2、当我们试用版有二级应用时,肯定不希望启动器关了,但是程序还在运行,那我们可以像steam一样,我们在游戏时关闭steam,游戏也会一同关闭,这段代码写在”OnDestroy“当脚本被回收,也就是应用被关闭时启用(也可以这么理解)OnDestroy最好写在总控脚本

processName是进程名字,当我们运行程序时可以打开任务管理器看进程名,然后获取现在所有进程名,遍历对比,当有进程名和processName相同时,杀死进程

这就是原理,还是挺简单的

void OnDestroy()
    {
        processName = "3D";
        System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcesses();
        foreach (System.Diagnostics.Process process in processes)
        {
            try
            {
                if (!process.HasExited && process.ProcessName == processName)
                    process.Kill();
            }
            catch (InvalidOperationException ex)
            {
                Debug.Log(ex);
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/k253316/article/details/128567241