茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1671308 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_60D_00186.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

Game Play Maker : [轉貼]Unity 常用腳本 – 滑鼠、手機螢幕觸摸 控制畫面

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]Unity 常用腳本 – 滑鼠、手機螢幕觸摸 控制畫面
Unity 常用腳本 (1) – 滑鼠、手機螢幕觸摸 控制畫面

▌首先建立一個腳本,加入下列程式碼
using UnityEngine;
using System.Collections;

//增加觸碰操作功能,控制攝影機運動
public class Touch : MonoBehaviour
{
    //紀錄手指觸碰位置
    Vector2 m_screenPos = new Vector2 ();
    void Start ()
    {
        //允許多點觸碰
        Input.multiTouchEnabled = true;
    }
    void Update ()
    {
        //判斷平台
        #if !UNITY_EDITOR && (UNITY_IOS || UNITY_ANDROID)
        MobileInput ();
        #else
        DeskopInput();
        #endif
    }
}

我們在 Update 中判斷平台是在"電腦"還是"手機",平台相關可以參考這個 官方Documentation
分別加入MobileInput 和 DeskopInput 兩個方法處理畫面控制

▌在DeskopInput 增加程式碼
void DeskopInput ()
{
    //紀錄滑鼠左鍵的移動距離
    float mx = Input.GetAxis ("Mouse X");
    float my = Input.GetAxis ("Mouse Y");
    float speed=6.0f;

    if (mx != 0 || my != 0)
    {
        //滑鼠左鍵
        if (Input.GetMouseButton (0))
        {
                //移動攝影機位置
            Camera.main.transform.Translate (new Vector3 (-mx * Time.deltaTime*speed, -my * Time.deltaTime*speed, 0));
        }
    }
}

獲得X軸與Y軸的位置,並控制攝影機位置

▌在MobileInput 增加程式碼
void MobileInput ()
{
    if (Input.touchCount <= 0)
        return;
    //1個手指觸碰螢幕
    if (Input.touchCount == 1)
    {
       //開始觸碰
       if (Input.touches [0].phase == TouchPhase.Began)
       {
          //紀錄觸碰位置
          m_screenPos = Input.touches [0].position;
          //手指移動
       }
       else if (Input.touches [0].phase == TouchPhase.Moved)
       {
            //移動攝影機
            Camera.main.transform.Translate (new Vector3 (-Input.touches [0].deltaPosition.x * Time.deltaTime, -Input.touches [0].deltaPosition.y * Time.deltaTime, 0));
       }
        //手指離開螢幕
        if (Input.touches [0].phase == TouchPhase.Ended && Input.touches [0].phase == TouchPhase.Canceled)
        {
            Vector2 pos = Input.touches [0].position;
            //手指水平移動
            if (Mathf.Abs (m_screenPos.x - pos.x) > Mathf.Abs (m_screenPos.y - pos.y))
            {
               if (m_screenPos.x > pos.x)
               {
                  //手指向左滑動
               }
               else
               {
                   //手指向右滑動
               }
            }
            else
            {
                if (m_screenPos.y > pos.y)
                {
                   //手指向下滑動
                }
                else
                {
                    //手指向上滑動
                }
            }
        }

    }
    //攝影機縮放,如果1個手指以上觸碰螢幕
    else if (Input.touchCount > 1)
    {
        //記錄兩個手指位置
        Vector2 finger1 = new Vector2 ();
        Vector2 finger2 = new Vector2 ();

        //記錄兩個手指移動距離
        Vector2 move1 = new Vector2 ();
        Vector2 move2 = new Vector2 ();

        //是否是小於2點觸碰
        for (int i=0; i<2; i++)
        {
            UnityEngine.Touch touch = UnityEngine.Input.touches [i];

            if (touch.phase == TouchPhase.Ended)
                break;

            if (touch.phase == TouchPhase.Moved)
            {
                //每次都重置
                float move = 0;

                //觸碰一點
                if (i == 0)
                {
                    finger1 = touch.position;
                    move1 = touch.deltaPosition;

                }
                else      //另一點
                {
                    finger2 = touch.position;
                    move2 = touch.deltaPosition;

                    //取最大X
                    if (finger1.x > finger2.x)
                    {
                        move = move1.x;
                    }
                    else
                    {
                        move = move2.x;
                    }

                    //取最大Y,並與取出的X累加
                    if (finger1.y > finger2.y)
                    {
                        move += move1.y;
                    }
                    else
                    {
                        move += move2.y;
                    }

                    //當兩指距離越遠,Z位置加的越多,相反之
                    Camera.main.transform.Translate (0, 0, move * Time.deltaTime);
                }
            }
        }//end for
    }//end else if
}//end void


▌所有資源作為教學用,無任何商業用途!!
歡迎轉載,但務必註明出處!!


原文出處:Unity 常用腳本 (1) – 滑鼠、手機螢幕觸摸 控制畫面 @ Game & Web Design :: 痞客邦 PIXNET ::
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]Unity3D 手機中 Input 類 touch 詳解 Unity觸屏事件解析

Unity3D 手機中 Input 類 touch 詳解 Unity觸屏事件解析

首先貼一下Unity支持的模型文件類型,以前沒有收集過。

Unity支持兩種類型的 3D文件格式:

1.  通用的“出口型” 3D文件

.fbx.dae.3ds.dxf.obj等文件格式。

2.  3D軟件專用的 3D文件格式

Max, Maya, Blender,Cinema4D, Modo, Lightwave & Cheetah3D 等軟件所支持的格式 ,.MAX, .MB, .MA等等。

 

 

Unity3D手機中Input類touch詳解:

1.Input.touchCount 觸摸隨之增長,一秒50次增量。

2.Input.GetTouch(0).phase==TouchPhase.Moved 手指滑動中最後一幀滑動的狀態是運動的。

3.TouchPhase  觸摸的幾個狀態。

4.Touch.deltaPosition 增量位置(Input.GetTouch(0).deltaPosition)最後一幀滑動的值,只返回xy軸坐標,也可用vector3(z軸為0),所以一般用vector2接收。


 1 static var aa:int;
2 function Update () {
3 if(Input.touchCount>0)
4 {
5 print(Input.touchCount);
6 }
7 }
8 function OnGUI()
9 {
10 GUI.Label(Rect(34,34,34,34),"sdff");
11 }

touchCount指的是觸摸幀的數量。要注意的是:touch事件 只能在模擬器或者真機上運行(已測試通過),大約一秒鐘touch不放。touchCount+50次左右。2.Input.touches 觸摸列表。


// Prints number of fingers touching the screen
//輸出觸摸在屏幕上的手指數量
function Update () {
var fingerCount = 0;
for (var touch : Touch in Input.touches) {
if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
fingerCount
++;
}
if (fingerCount > 0)
print (
"User has " + fingerCount + " finger(s) touching the screen");
}

3.讓cube隨著touch 移動代碼:


static var count:int; //定義touchCount數
var particle_:GameObject;//定義存放cube對像
var touchposition:Vector3; //存儲移動三維坐標值
function Update () {
if(Input.touchCount>0)
{
count
+=Input.touchCount;}
if((Input.touchCount>0&&Input.GetTouch(0).phase==TouchPhase.Moved)) //如果點擊手指touch了 並且手指touch的狀態為移動的
{
touchposition
=Input.GetTouch(0).deltaPosition; //獲取手指touch最後一幀移動的xy軸距離
particle_.transform.Translate(touchposition.x*0.01,touchposition.y*0.01,0);//移動這個距離
}}
function OnGUI()
{
GUI.Label(Rect(
10,10,100,30),"cishu:"+count.ToString());
GUI.Label(Rect(
10,50,100,30),touchposition.ToString());
}

 

  移動物體:


using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public float speed = 0.1F;
void Update() {
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
Vector2 touchDeltaPosition
= Input.GetTouch(0).deltaPosition;
transform.Translate(
-touchDeltaPosition.x * speed, -touchDeltaPosition.y * speed, 0);
}
}
}

點擊碰撞克隆:


using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public GameObject projectile;
void Update() {
int i = 0;
while (i < Input.touchCount) {
if (Input.GetTouch(i).phase == TouchPhase.Began)
clone
= Instantiate(projectile, transform.position, transform.rotation) as GameObject;
++i;
}
}
}

點擊屏幕,射線法發射一個粒子


using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public GameObject particle;
void Update() {
int i = 0;
while (i < Input.touchCount) {
if (Input.GetTouch(i).phase == TouchPhase.Began) {
Ray ray
= Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
if (Physics.Raycast(ray))
Instantiate(particle, transform.position, transform.rotation)
as GameObject;
}
++i;
}
}
}


原文出處:25.Unity3D手机中Input类touch详解-Unity触屏事件解析到底(Twisted Fate) - Twisted Fate Gargamel - 博客园
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]資策會Unity課程「Unity進階」Day1~2

資策會Unity課程「Unity進階」Day1~2

冰劍士-冰雪飛│2014-11-20 23:45:01
這篇拖了好久才開始撰寫......這是10/29日下午&11/5日上午的課程

這堂課的內容實在不好寫,要花好些時間才能消化

由於第一天的課程大致上是重複的內容,所以本篇集中講第二天的實作內容
=====================================================================

這次的腳本範例都是使用3D的,事前要準備好內建的GameObject
GameObject>CreateOther>Plane(內建地板模組)
GameObject>CreateOther>Cube(內建方塊模組)
GameObject>CreateOther>Directional light(內建定向光)
=====================================================================
點擊物件

這個範例腳本放在有Camera的GameObject內,範例內容為
滑鼠點擊畫面,假如點到實體物件,此物件會移除

Input. mousePosition
:滑鼠當下的座標位置,範例中說是 點擊時的座標是因為 if 條件關係

Ray類別:存取射線資訊用,有2D版本

ScreenPointToRay(Vector3):畫面釋出射線,為Camera類別內的方法

RaycastHit類別:存取射線擊中物件的資訊, 物件必須要有Collider才能起作用

Physics類別:物理運算類別,有2D版本

Raycast(Ray , out RaycastHit , float 距離):接收 射線擊中物件後反射資訊

=====================================================================
我們為何要撰寫點擊物件的腳本?
有沒有注意到現在流行的DOTA遊戲的移動方式,是屬「滑鼠點擊座標移動」
假設我們在 上個範例點擊「Plane地面」時不是刪除物件,改成「Cube方塊」移動到該點
這樣一來我們就完成了最簡單的「滑鼠點擊物件移動」

這個腳本本身沒太大的錯誤,不過我測試好像還有點問題,需要有人來debug

hitInfo. point:射線擊中物件時的座標點, 使用 Vector3 存取

nDeltaVector變數是存取 「射線擊中座標」和「準備移動物件座標」的差距

Vector3. Normalize():正規化, 保持原有的方向,但長度為1.0

CharacterController. SimpleMove(): 移動腳色的位移速度
=====================================================================
接下去關於這堂課的文章大概都會像這樣,撰寫解釋課堂範例的內容

程式腳本可能會出錯,請小心使用,能幫忙debug就幫忙吧

原文出處: 資策會Unity課程「Unity進階」Day1~2 - a1455785的創作 - 巴哈姆特
前一個主題 | 下一個主題 | 頁首 | | |



Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|