Unity手机游戏制作的一些相关知识

Unity手机游戏制作的一些相关知识

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);
    }
}

用于检测触摸屏的滑动和手机加速度仪的数值(可用于模拟物理效果等)

大小检测

  1. 物体大小检测
 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;
  1. 屏幕大小检测
Vector3 ScreenTop = new Vector3(Screen.width, Screen.height, 20);
ScreenTop = MainCamera.ScreenToWorldPoint(ScreenTop);
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值