post请求和时间戳转化

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

接口介绍

一、 ICanvasRaycastFilter 接口

示例:ICanvasRaycastFilter
该元素可以过滤射线投射。如果顶级元素被命中,它还能进一步“检查”该位置是否有效。

1.可以用来实现新手引导镂空效果

实现 接口的函数即可

2.示例代码

下面只是过滤射线投射的示例代码,不是新手引导镂空效果的代码,新手引导镂空效果的代码,请查看其它文档。
代码如下(示例):

    public bool IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera)
    {
        if (!enabled)
            return true;

        Sprite sprite = image.overrideSprite;
        if (sprite == null)
            return true;

        Vector2 local;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(image.rectTransform, screenPoint, eventCamera, out local);

        Rect rect = image.rectTransform.rect;

        // Convert to have lower left corner as reference point.
        local.x += image.rectTransform.pivot.x * rect.width;
        local.y += image.rectTransform.pivot.y * rect.height;

        float u = local.x / rect.width;
        float v = local.y / rect.height;

        try
        {
            if (!reversed)
                return sprite.texture.GetPixelBilinear(u, v).a != 0;
            else
                return sprite.texture.GetPixelBilinear(u, v).a == 0;
        }
        catch (UnityException e)
        {
            Debug.LogException(e);
        }

        return true;
    }

二、 JsonUtility序列和反序列时,所需的json数据读取的格式

json数据格式范例如下:
注意:stepList 是list类型的字段,JsonUtility 无法

{
  "stepList": [
    {
      "id": 21,
      "name": "哈哈哈哈哈",
      "describe": "哈哈哈哈哈哈哈哈哈哈哈哈哈哈",
    },
    {
      "id": 22,
      "name": "哈哈哈哈哈哈哈哈哈哈哈哈哈哈",
      "describe": "哈哈哈哈哈哈哈哈哈哈哈哈哈哈",
    },
    {
      "id": 23,
      "name": "哈哈哈哈哈哈哈哈哈哈哈哈哈哈",
      "describe": "哈哈哈哈哈哈哈哈哈哈哈哈哈哈",
    }
  ]
}

三、 UnityWebRequest 用post向服务器申请数据

范例如下:
注意:
需要网址正确,
两对请求头的键值对(key,value)需要加上,必要时,还会有token请求头键值对。
post申请需要严格对应的格式转成的字符串。例如里面的dataAll,就是自定义类。

        //StartCoroutine("SendPost");
    private IEnumerator SendPost()
    {
        string uri1 = "http://www.my-server.com";

        string key = "你的请求头key1";
        string value = "你的请求头value1";

        string key01 = "Content-Type";//你的请求头key2
        string value1 = "application/json";//你的请求头value2

        dataAll = new DataAll();
        string temp02 = JsonUtility.ToJson(dataAll);

        #region UnitySkill
        using (UnityWebRequest request = UnityWebRequest.Post(uri1,temp02))
        {
            request.SetRequestHeader(key, value);
            request.SetRequestHeader(key01, value1);
            yield return request.SendWebRequest();

            if (request.isNetworkError || request.isHttpError)
                Debug.Log(request.error);
            else
            {
                if (request.isDone)
                {
                    print(request.downloadHandler.text);
                    dataAll = JsonUtility.FromJson<DataAll>(request.downloadHandler.text);
                }
            }
        }
        yield return new WaitForSeconds(5);
        #endregion
    }

四、 用摄像头截图

            var cam=n.GetComponentInChildren<Camera>();
            Texture2D texture = new Texture2D(256, 256);
            RenderTexture currentRT = RenderTexture.active;
            RenderTexture renderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 32);
            Graphics.Blit(texture, renderTexture);

            RenderTexture.active = renderTexture;
            texture.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
            texture.Apply();

            RenderTexture.active = currentRT;
            RenderTexture.ReleaseTemporary(renderTexture);

五、时间戳和时间相互转化

1.10位的时间戳是秒,13位的时间戳是毫秒

        TimeSpan st = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0);
        Debug.Log("日期转为时间戳:" + st.TotalSeconds);

        DateTime startTime = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), TimeZoneInfo.Local);
        DateTime dt = startTime.AddSeconds(st.TotalSeconds);
        //DateTime dt = startTime.AddMilliseconds(long.Parse("1708314398000"));第二种方法,使用时间戳的字符串转时间
        string t = dt.ToString("yy/MM/dd HH:mm:ss");//转化为日期时间
        print(t);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱岗酸豆角吖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值