Unity 白猫操作小实例

最近师兄找我说白猫的操作如何做,  0.0 结果白猫没有android的客户端玩不了,看了下视频介绍就简单做了下

效果图:

1

 

核心代码:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

public class Test : MonoBehaviour {

    private Test() { }

    private Vector3 startMouseDown;
    private Vector3 lastMouseDown;
    private float pressTimer;
    private bool isCounter;             //开始计数
    private bool isDrag;                //开始拖动
    private bool isLasting;             //开始持久点击


    public float pressTime;             //单击
    public float pressLastingTime;      //持久点击
    public float dragDistance;          //拖动大于多少才开始生效

    #region 事件
    public static Action<Vector3> StartPressEvent;
    public static Action<Vector3> EndPressEvent;

    public static Action<Vector3> StartDragEvent;
    public static Action<Vector3> EndDragEvent;

    public static Action<Vector3> StartLastingEvent;
    public static Action<Vector3> EndLastingEvent;
    #endregion

    #region 测试方法
    void Awake()
    {
        StartPressEvent += StartPress;
        EndPressEvent += EndPress;

        StartDragEvent += StartDrag;
        EndDragEvent += EndDrag;

        StartLastingEvent += StartLasting;
        EndLastingEvent += EndLasting;
    }

    private void StartPress(Vector3 v)
    {
        Debug.Log("开始单击事件");
    }

    private void EndPress(Vector3 v)
    {
        Debug.Log("结束单击事件");
    }

    private void StartDrag(Vector3 v)
    {
        Debug.Log("开始拖动事件");
    }

    private void EndDrag(Vector3 v)
    {
        Debug.Log("结束拖动事件");
    }

    private void StartLasting(Vector3 v)
    {
        Debug.Log("开始持续点击事件");
    }

    private void EndLasting(Vector3 v)
    {
        Debug.Log("结束持续点击事件");
    }
    #endregion

    // Update is called once per frame
    void Update () {

        if (Input.GetMouseButtonDown(0))
        {
            isCounter = true;
            startMouseDown = Input.mousePosition;
        }

        if (Input.GetMouseButtonUp(0))
        {
            lastMouseDown = Input.mousePosition;
            isCounter = false;

            if (isDrag) 
            {
                //拖动
                if (EndDragEvent != null) EndDragEvent(Input.mousePosition);
                isDrag = false;
            }
            else if (isLasting) 
            {
                //持久点击
                if (EndLastingEvent != null) EndLastingEvent(Input.mousePosition);
                isLasting = false;
            }
            else 
            {
                //单击
                if (EndPressEvent != null) EndPressEvent(Input.mousePosition);
            }

        }

        if (isCounter)
        {
            //开始计数
            pressTimer += Time.deltaTime;
        }
        else 
        {
            if (pressTimer > 0 && pressTimer < pressTime)
            {
                Debug.Log("单击");
                if (StartPressEvent != null) StartPressEvent(Input.mousePosition);

            }

            pressTimer = 0f;
        }

        if (isCounter && Mathf.Abs(Vector3.Distance(startMouseDown, Input.mousePosition)) > dragDistance && isLasting == false)
        {
            Debug.Log("正在拖动");
            isDrag = true;

            if (StartDragEvent != null) StartDragEvent(Input.mousePosition);
           
            //让人物跟谁手指的方向移动
            return;
        }

        if (isCounter && pressTimer > pressLastingTime && isDrag == false)
        {
            Debug.Log("持久点击");
            isLasting = true;

            if (StartLastingEvent != null) StartLastingEvent(Input.mousePosition);
            
            //出现技能图标,然后滑动到技能哪里就可以触发技能
            
            return;
        }


    }



}

 

Unity5 + UGUI制作

完整的demo: http://yunpan.cn/cjHbIaXvzemax  访问密码 7607

posted @ 2015-05-03 15:30  盘子脸  阅读(727)  评论(0编辑  收藏  举报