Event监听
控制脚本里声明event
public event Action OnAutoClickValuesChanged;
同时在需要监测的地方对这个event进行监听
private float timeToAutoClick = 1;
public float TimeToAutoClick
{
set
{
timeToAutoClick = value;
OnAutoClickValuesChanged?.Invoke();
}
get
{
return timeToAutoClick;
}
}
再在UI脚本里用这个event去改变UI显示数值
public class ClicksUI : MonoBehaviour
{
[SerializeField]
TMPro.TMP_Text clickText;
private ClickManager clickManager;
private void Start()
{
clickManager = FindObjectOfType<ClickManager>();
clickManager.OnClicksChanged += UpdateClickText;
UpdateClickText();
}
private void UpdateClickText()
{
int WholeNumber=(int)clickManager.Clicks;
clickText.text = WholeNumber.ToString();
}
}
输入
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InputController : MonoBehaviour
{
public event Action OnSwipeUp, OnSwipeDown, OnSwipeLeft, OnSwipeRight;
public event Action<Vector2> OnSwipe;
private const float MAX_SWIPE_TIME = 0.5f;
private const float MIN_SWIPE_DISTANCE = 0.17f;
Vector2 startPosTouch;
Vector2 startPosMouse;
float startTime;
float screenRatioDivider;
public Vector3 Acceleration { get; private set; }
private void Start()
{
screenRatioDivider = (float)Screen.width;
}
private void Update()
{
Acceleration = Input.acceleration;
if (Input.touchCount > 0)
{
Touch t = Input.GetTouch(0);
if (t.phase == TouchPhase.Began)
{
startPosTouch = new Vector2(t.position.x / screenRatioDivider, t.position.y / screenRatioDivider);
startTime = Time.time;
}
if (t.phase == TouchPhase.Ended)
{
if (Time.time - startTime > MAX_SWIPE_TIME)
return;
Vector2 endPos = new Vector2(t.position.x / screenRatioDivider, t.position.y / screenRatioDivider);
Vector2 swipe = new Vector2(endPos.x - startPosTouch.x, endPos.y - startPosTouch.y);
CalculateSwipe(swipe);
}
}
if (!(Application.isEditor && UnityEditor.EditorApplication.isRemoteConnected))
{
if (Input.GetMouseButtonDown(0))
{
startPosMouse = new Vector2(Input.mousePosition.x / screenRatioDivider, Input.mousePosition.y / screenRatioDivider);
startTime = Time.time;
}
if (Input.GetMouseButtonUp(0))
{
if (Time.time - startTime > MAX_SWIPE_TIME)
return;
Vector2 endPos = new Vector2(Input.mousePosition.x / screenRatioDivider, Input.mousePosition.y / screenRatioDivider);
Vector2 swipe = new Vector2(endPos.x - startPosMouse.x, endPos.y - startPosMouse.y);
CalculateSwipe(swipe);
}
}
}
private void CalculateSwipe(Vector2 swipe)
{
if (swipe.magnitude < MIN_SWIPE_DISTANCE)
return;
if (Mathf.Abs(swipe.x) > Mathf.Abs(swipe.y))
{
// Horizontal swipe
if (swipe.x > 0)
{
OnSwipeRight?.Invoke();
}
else
{
OnSwipeLeft?.Invoke();
}
}
else
{
// Vertical swipe
if (swipe.y > 0)
{
OnSwipeUp?.Invoke();
}
else
{
OnSwipeDown?.Invoke();
}
}
OnSwipe?.Invoke(swipe);
}
}
用于检测触摸屏的滑动和手机加速度仪的数值(可用于模拟物理效果等)
大小检测
- 物体大小检测
float fishWdth = GetComponentInChildren<Renderer>().bounds.size.x;
[SerializeField]
GameObject GameContainer;
private float leftbound, rightbound, topbound, bottombound;
public float LeftBound { get { return leftbound; } }
public float RightBound { get { return rightbound; } }
public float TopBound { get { return topbound; } }
public float BottomBound { get { return bottombound; }
float gamecontainerZ = GameContainer.transform.position.z;
leftbound = MainCamera.ScreenToWorldPoint(new Vector3(0, 0, gamecontainerZ)).x;
rightbound = MainCamera.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, gamecontainerZ)).x;
bottombound = MainCamera.ScreenToWorldPoint(new Vector3(0, 0, gamecontainerZ)).y;
topbound = MainCamera.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, gamecontainerZ)).y;
- 屏幕大小检测
Vector3 ScreenTop = new Vector3(Screen.width, Screen.height, 20);
ScreenTop = MainCamera.ScreenToWorldPoint(ScreenTop);