【Unity3D基礎】讓物體動起來①--UGUI滑鼠點擊移動

來源:互聯網
上載者:User

標籤:cti   api   div   需求   files   記錄   dem   中控   sprite   

背景

首先還是先聲明自己是比較笨的一個人,總是找不到高效的學習方法,目前自己學習Unity3D的方式主要是兩種,一種是直接看高品質的源碼,另一種是光看不行還要自己動手,自己寫一些有代表性的小程式,這也是本文的由來。

誠然,現在已經不是Unity3D發展的黃金期了,很多知識點已經有很多朋友總結分享了,但由於Unity3D的客觀性就是依賴外掛程式很厲害,這樣就會照成外掛程式滿天飛,通用性不強,還有過時的風險,還有就是網上很多朋友發的文章很多並不是學習總結,只是對於Unity3D IDE的使用及一些API的羅列,或者是一些遊戲Demo的完整教程。自己一直想從另一個角度來學習一下,也就是單純從遊戲的角度(我們常說的商務邏輯的角度),其實以前很多VC遊戲教程都是這種思路,就是從一個很小的功能開始學,逐個知識點積累最後完成一個相對複雜完整的Demo程式。所以本文就從最小的讓物體動起來開始。

說明

文章的視角基本著眼的是從簡單到複雜功能的學習理解,所以是主要是基於2D的,也就是從Untiy2D開始,同時也會穿插一些3D部分,

所以最好使用具有Unity2D功能的Unity3D用戶端,好像是4.3以後吧

鎮樓

需求描述和分析

本章的需求很簡單,就是“通過在螢幕中點擊滑鼠,在2d視圖中控制物體進行移動”。在別的平台和引擎中簡直是Hello world層級的需求在Unity3d中卻變得有些棘手了,主要有幾個問題困擾著我,

1、IO輸入是怎麼樣的;

2、滑鼠事件是如何接收的;

3、移動動畫是如何?的;

“原理很簡單,現實很殘酷”,Unity3d的開放性造成了所謂的外掛程式和庫很多,顯得淩亂。這裡還是按照自己的慣性思維來了,按照別的平台的教程,

A、首先要有一個容器,

B、在容器上放置需要移動的精靈,

C、通過容器接收滑鼠事件,

D、事件驅動精靈移動

這裡UGui中採用容器的概念(或者我還不知道別的庫也有),所以先在UGui中實現,

第一步,加一個Panel對象

第二步,匯入一個精靈的圖片資源

第三步,加一個2d的Image對象,設定其sprite為剛才匯入的圖片資源

第四步,給Panel對象綁定一個指令碼,PanelController,

樹結構

加圖資源後

綁定代碼

 

具體代碼如下

 

using UnityEngine;using System.Collections;using UnityEngine.EventSystems;public class PanelController:  MonoBehaviour , IPointerClickHandler{    private Transform childPlayTransform;    private Transform parentCanvasTransform;    // Use this for initializationvoid Start (){       //獲得Image的Transform        childPlayTransform = transform.Find("Image");       parentCanvasTransform = transform.parent;}// Update is called once per framevoid Update () {}    public void OnPointerClick(PointerEventData eventData)    {        Debug.Log(string.Format("x:{0},y:{1}", eventData.position.x, eventData.position.y));        Debug.Log(string.Format("button x:{0},y:{1}", childPlayTransform.localPosition.x, childPlayTransform.localPosition.y));        Vector2 localPoint;        //在矩形範圍內檢測,全域滑鼠點擊點,到local點的轉換        RectTransformUtility.ScreenPointToLocalPointInRectangle(transform as RectTransform, eventData.position,            eventData.enterEventCamera, out localPoint);        childPlayTransform.localPosition = localPoint;    }}

  

代碼雖然少,但是核心的知識點就是全局座標、相機座標和局部座標的轉換也就是下面的這個函數

        //        // 摘要:         //     Does the RectTransform contain the screen point as seen from the given camera?        //        // 參數:         //   rect:        //     The RectTransform to test with.        //        //   screenPoint:        //     The screen point to test.        //        //   cam:        //     The camera from which the test is performed from.        //        // 返回結果:         //     True if the point is inside the rectangle.        public static bool RectangleContainsScreenPoint(RectTransform rect, Vector2 screenPoint, Camera cam);        public static bool ScreenPointToLocalPointInRectangle(RectTransform rect, Vector2 screenPoint, Camera cam, out Vector2 localPoint);

  

總結

雖然功能很少,但是也有幾個知識點,一個是Unity3d的座標系統,一個是座標轉換的基礎知識,一個是Unity3d Gui的事件處理方式,特此記錄。功能上有點小缺陷就是精靈的移動是瞬移的,也就是滑鼠指到哪裡移動到哪裡(確實我也沒有看出有什麼不平滑),留到下篇繼續吧。

源碼

【Unity3D基礎】讓物體動起來①--UGUI滑鼠點擊移動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.