提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言
接口介绍
一、 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);