unity 3D 筆記

來源:互聯網
上載者:User

當js指令碼裡有#pragma strict語句時,unity就會禁用動態類型,若程式裡有var  test=GetComponent(Test);//此為動態語句,unity編譯時間就會報錯。

聲明靜態變數語句: private var  xxx:Transform;然後在Awake()函數裡給變數賦值  代碼如下:function  Awake(){xxx=transform;}

內建數組代碼如下:;

private var   xxx:Vector3[];

function Awake(){

xxx=new  Vector3[100];//建立一個向量

for(var i=0;i<100;i++)

{xxx[i]=Vector3.zero;//為每一個向量賦值

}}

想在一個JS指令碼中引用一個C#指令碼,可以將C# 指令碼放到“standard   Assert”檔案夾下,然後將JS指令碼放在此檔案夾之外,JS指令碼便可以直接引用C# 指令碼。在“WebPlay  Templates”檔案下的指令碼不會被編譯。

泛化方法:在方法名字後面加T或者.<T>。這種方法可以指定參數的類型或者傳回值的類型,在JS中,此方法可以用於動態類型的局限性。

if(GUI.Button(Rect(Screen.width/2,Screen.height-30,50,30),aTexture,MyStyle))代碼的意思是 如果點擊在 螢幕寬度1/2 高度-30 處,寬為50高為30的按鈕(按鈕的紋理為 aTexture)

執行個體:編寫一個aaa.cs 指令碼,將此指令碼掛載到攝像機上,從而實現GUI組件的應用,主要代碼如下:

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {

 // Use this for initialization
    public Texture qdTexture;//qd按鈕紋理圖引用
   public Texture tcTexture;//tc按鈕紋理圖引用
   public Texture albtTexture;//albt按鈕紋理圖引用
   public GUIStyle MySttyle;//定義自己的GUIStyle
   public static bool isTouched = false;//是否有cube被觸摸
   public static string touchedCube = null;//被觸摸Cube的名字
 void Start () {
        PlayerPrefs.SetString("Text", "Text1");//預設儲存第一個Cube的紋理圖,PlayerPrefs.SetString()方法擷取紋理圖
 }
 
 // Update is called once per frame
 void Update () {
 foreach (Touch touch in Input.touches)//對當前觸控事件迴圈

{

Ray ray=Camera.main.ScreenPointToRay(touch.position);//聲明一條由螢幕觸控點發出垂直於螢幕的射線

RaycastHit  hit;//聲明一個RaycastHit 型變數hit

if(Physics.Raycast(ray,out hit))//判斷此物理事件

{isTouched=true;//當射線碰撞到Cube時將isTouched設為true

//獲得射線碰撞Cube的名稱

touchedCube=hit.transform.root.transform.name;

SetText(touchedCube);//調用SetText函數

}}

if(Application.platform==RuntimePlatform.Android)//判斷運行平台是否是Android平台

{if(Input.GetKeyUp(KeyCode.Home))//判斷當前按鍵是否為HOME鍵

{Application.Quit();//退出項目}}

}

void  OnGUI(){

float ratioScaleTempH=Screen.height/960.0f;//為了進行螢幕自適應而聲明的縱向縮放比變數

float ratioScaleTemp=Screen.width/540.0f;//為了進行螢幕自適應而聲明的橫向縮放比變數

//在自訂的螢幕位置繪製案例標題的紋理圖片

GUI.DrawTexture(new Rect(45*ratioScaleTemp,50*ratioScaleTempH,450*ratioScaleTemp,100*ratioScaleTempH),albtTexture,ScaleMode.ScaleToFit,true,450.0f/100.0f)

if(GUI.Button(new Rect(70*ratioScaleTemp,800*ratioScaleTempH,150*ratioScaleTemp,80*ratioScaleTempH),qdTexture,MyStyle))//在自訂的螢幕位置繪製一個確定按鈕,並判斷是否被按下

{

Application.LoadLevel("StartBall");//若按下,則跳轉到Start情境

Destoy(this);//銷毀這個按鈕

}

}

}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.