iLMS知識社群歷程檔登入
位置: 黃國哲 > Unity
1091226 垂直或水平旋轉畫面控制器
by 黃國哲 2020-12-26 16:00:18, 回應(0), 人氣(656)
參考BLOG


//////旋轉

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.EventSystems;

public class 下轉腳本 : MonoBehaviour, IPointerDownHandler, IPointerUpHandler

{
  public GameObject controller3 ; //宣告要被遙控的對象
  public Text counttext1 ; //宣告要用來辨識方向的運算子
  public float PressDownTimer; //按下幾秒觸發
  private bool PressDown; //按下
  public UnityEvent onLongClick; //開啟Inspector觸發事件
  [SerializeField]
  public float HoldTime;

  //按下按鈕
  public void OnPointerDown(PointerEventData eventData){
    PressDown = true;
    Debug.Log("PressDown");
    counttext1.text= "8";
  }
  //按鈕彈起
  public void OnPointerUp(PointerEventData eventData){
    Reset();
    Debug.Log("PressUp");
    counttext1.text= "0";
  }
  //當按下按鈕 PressDown = true 時計時
  void Update(){
    if (PressDown == true){
      PressDownTimer += Time.deltaTime;
      if (PressDownTimer >= HoldTime){
        if (onLongClick != null){
          onLongClick.Invoke();
        }
        Reset();
      }
    }

    if(counttext1.text == "8")
controller3.transform.Rotate(0.1f,0,0);

   // if(counttext1.text == "0")
//print(" No Data") ;

  }
  //當PressUp的時候重製計算時間
  private void Reset(){
    PressDown = false;
    PressDownTimer = 0;
  }
  //觸發後執行的功能
  public void LongPressFuntion() { 
    Debug.Log("觸發後執行的功能");
  }
}



/////水平

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.EventSystems;

public class 右轉腳本 : MonoBehaviour, IPointerDownHandler, IPointerUpHandler

{
  public GameObject controller2 ; //宣告要被遙控的對象
  public Text counttext1 ; //宣告要用來辨識方向的運算子
  public float PressDownTimer; //按下幾秒觸發
  private bool PressDown; //按下
  public UnityEvent onLongClick; //開啟Inspector觸發事件
  [SerializeField]
  public float HoldTime;

  //按下按鈕
  public void OnPointerDown(PointerEventData eventData){
    PressDown = true;
    Debug.Log("PressDown");
    counttext1.text= "5";
  }
  //按鈕彈起
  public void OnPointerUp(PointerEventData eventData){
    Reset();
    Debug.Log("PressUp");
    counttext1.text= "0";
  }
  //當按下按鈕 PressDown = true 時計時
  void Update(){
    if (PressDown == true){
      PressDownTimer += Time.deltaTime;
      if (PressDownTimer >= HoldTime){
        if (onLongClick != null){
          onLongClick.Invoke();
        }
        Reset();
      }
    }

    if(counttext1.text == "5")
controller2.transform.Rotate(0,0.1f,0);

   // if(counttext1.text == "0")
//print(" No Data") ;

  }
  //當PressUp的時候重製計算時間
  private void Reset(){
    PressDown = false;
    PressDownTimer = 0;
  }
  //觸發後執行的功能
  public void LongPressFuntion() { 
    Debug.Log("觸發後執行的功能");
  }
}
回應