close

最近有人看了我的這篇教學

並私訊我停頓的解決方法

在這就順便做一個簡單的解決方法


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

public class Test : MonoBehaviour 
{
  // 當前選擇的目標
  Transform currentTrans;
  // 是按下滑鼠
  bool isDown;

  void Update()
  {
    // 當按下時
    if(isDown)
    {
      // 當前目標獲得滑鼠當下的位置
      currentTrans.position = Input.mousePosition;
      // 當滑鼠放手時
      if (Input.GetMouseButtonUp(0))
      {
        // 放手
        isDown = false;
      }
    }
    else
    {
      // 當滑鼠按下
      if (Input.GetMouseButtonDown(0))
      {
        PointerEventData pointer = new PointerEventData(EventSystem.current);
        pointer.position = Input.mousePosition;
        // 射線到的目標
        List<RaycastResult> raycastResults = new List<RaycastResult>();
        EventSystem.current.RaycastAll(pointer, raycastResults);
        // 目標數量
        if (raycastResults.Count > 0)
        {
          isDown = true;
          // 取得第一個目標
          currentTrans = raycastResults[0].gameObject.transform;
        }
      }
    }
  }
}


 

也順便打上一些註解,希望剛入門的人可以看得懂

如果有不明白的或是有其他功能不知如何解決也歡迎留言或私訊

也非常感謝到我的FB粉絲專頁按讚

arrow
arrow
    全站熱搜

    Weight 發表在 痞客邦 留言(0) 人氣()