//射线检测
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<Animator>();
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;
}
网友评论