//射线检测
RaycastHit hit;
void Update () {
if (Input.GetMouseButtonDown(0)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit)) {
if (hit.transform.tag.Equals("Player")) {
Animator ani = hit.transform.GetComponent
if (ani.GetCurrentAnimatorStateInfo(0).IsName("Idle")) {
ani.SetTrigger("ToClick");
}
}
}
}
}
双击功能
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DoubleClick : MonoBehaviour
{
//双击功能的实现
RaycastHit hit;
Ray ray;
float clickTimer1, clickTimer2;
//实现双击功能的物体
public Transform clickTrans;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.name == clickTrans.name)
{
clickTimer2 = Time.realtimeSinceStartup;
if (clickTimer2 - clickTimer1 < 0.2f)
{
print("double click ");
}
clickTimer1 = clickTimer2;
}
}
}
}
}
获取当前时间
void Start () {
string nowTime = DateTime.Now.ToString("yyyy-MM-dd HH: mm: ss");
string nowTime1 = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
print(nowTime);
print(nowTime1);
}
public Image image;
public ScrollRect scrollRect;
void Awake() {
//不规则按钮点击
image.alphaHitTestMinimumThreshold = 0.1f;
//alpha阈值指定一个像素必须具有的最小alpha,以便事件被视为图像上的“命中”。
//当alpha小于赋值的值0.1的的时候,图片无法进行点击检测
实现点击不规则按钮功能时(image.alphaHitTestMinimumThreshold = 0.1f)需要将图片设置如下:(在Advanced 下勾选 Read/Write Enabled )
//ScrollView 界面初始化
scrollRect.verticalNormalizedPosition = 1;
//垂直滚动位置,值介于0和1之间,0位于底部。
//scrollView 的垂直滚动,可用于初始化scrollView的垂直展示
}
Texture2D旋转
Texture2D RotateTexture(Texture2D texture, float eulerAngles)
{
int x;
int y;
int i;
int j;
float phi = eulerAngles / (180 / Mathf.PI);
float sn = Mathf.Sin(phi);
float cs = Mathf.Cos(phi);
Color32[] arr = texture.GetPixels32();
Color32[] arr2 = new Color32[arr.Length];
int W = texture.width;
int H = texture.height; int xc = W / 2;
int yc = H / 2;
for (j = 0; j < H; j++)
{
for (i = 0; i < W; i++)
{
arr2[j * W + i] = new Color32(0, 0, 0, 0);
x = (int)(cs * (i - xc) + sn * (j - yc) + xc);
y = (int)(-sn * (i - xc) + cs * (j - yc) + yc);
if ((x > -1) && (x < W) && (y > -1) && (y < H))
{
arr2[j * W + i] = arr[y * W + x];
}
}
}
Texture2D newImg = new Texture2D(W, H);
newImg.SetPixels32(arr2);
newImg.Apply();
return newImg;
}
//是否隐藏鼠标
//Cursor.visible = bool.Parse(Configs.instance.LoadText("隐藏鼠标", "false/true"))
Cursor.visible = false;
判断字符串是否由字母数字组成,并且长度在8-16个字符
using UnityEngine;
using System.Text.RegularExpressions;
public class Test : MonoBehaviour
{
Regex regex;
private void Awake()
{
//正则表达式
/*
^ 匹配一行的开头位置
(?![0-9]+$) 预测该位置后面不全是数字
(?![a-zA-Z]+$) 预测该位置后面不全是字母
[0-9A-Za-z] {8,16} 由8-16位数字或这字母组成
$ 匹配行结尾位置
注:(?!xxxx) 是正则表达式的负向零宽断言一种形式,标识预该位置后不是xxxx字符。
*/
regex = new Regex("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$");
}
private void Start()
{
Debug.Log(validatePassWord("1234567890a"));
}
///
/// 判断输入的字符串是否包含数字和字母,且长度在8-16之间
///
///
///
public bool validatePassWord(string newPass) {
bool istrue = !string.IsNullOrEmpty(newPass) && regex.IsMatch(newPass);
return istrue;
}
}