unity開發經驗(不定期更新)

希望我的開發日志記錄的經驗越來越多,給以后得自己留個記錄和參考,同時也能為廣大的同樣是想做unity獨立開發者的朋友一些經驗和參考,互相學習,互相進步!如果想合作的可以加我微信:wqm994,一起探討unity引擎,在獨立開發的道路上齊頭并進!!

廢話不多說,開始正題:

如何實現點擊按鈕彈出菜單欄?

實現方法:

再建一個Canvas,在這個Canvas上加入所有你想加的東西

然后加代碼:

public Canvas canvas;

void Start()

{

canvas.enabled=false;

}

public void Click()

{

canvas.enabled=true;

}

將這個代碼加到任意游戲物體上,然后就和普通的創建按鈕點擊事件操作一樣了。

注意:回到unity后把Canvas關聯上。

unity5.6版本的video組件,在通過代碼控制它的時候,必須引用命名空間,即必須進行程序集引用:

using UnityEngine .Video ;(這是必須的)

控制視頻的播放與暫停

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine .Video ;

public class bofang : MonoBehaviour {

public VideoPlayer vp;

// Use this for initialization

void Start () {

vp.Stop ();

}

// Update is called once per frame

void OnMouseDown () {

vp.Play ();

}

}

如何實例化發射炮彈?(變種:如何發射類似于英雄聯盟中極地大亂斗中的雪球?)

public Rigidbody paodan;

public float speed=20f;

void Update()

{

if(Input.GetButtonDown(KeyCode.F)){

Rigidbody newpandan=Instantiate(pandan,transform.position,transform.rotation) as Rigidbody;

newpandan.velocity=transform.TransformDirection(new Vector3(0,0,speed));

}

}

如何每隔一段時間克隆一個呢?

public GameObject monster;//要克隆的游戲物體(必須要制作成預制)

public float jdtime = 5.0f;//每隔5秒克隆一個

private float nextgs = 0.0f;//游戲時長

void Start () {

}

void Update () {

if(Time .time >nextgs )//判斷實際游戲時長是否大于游戲時長

{

nextgs = Time.time + jdtime;

GameObject newgs=Instantiate(monster,transform.position,transform.rotation) as GameObject ;

}

}

}

改變鼠標樣式:

public Textrue2D mousetextrue;

void OnMouseOver()

{

Cursor.SetCursor(mousetextrue,Vector2.zero,CursorMode.Auto);

}

void OnMouseExit()

{

Cursor.SetCursor(null,Vector2.zero,CursorMode.Auto);

}

在腳本組件小齒輪圖標下自定義菜單項:

[ContextMenu("XXXX")] //XXXX為菜單名字

void XXXX(){

}

如何制作光線投射,即發射射線來檢測是否碰撞到物體?

void Update(){

RaycastHit hit;設置一個RaycastHit類型的變量。

if (Physics.Raycast(transform.position,transform.forword,hit,3)){//transform.position表示從此腳本所依附的游戲物體的位置發射射線;transform.forword表示射線的朝向和所依附的游戲物體一致

這里面即可寫一些做什么事的代碼

}

}

在當前物體的位置繪制圖像等元素的函數:

void OnDrawGizmos()

{

XXXXXX

}

如何建立路點?在場景中擺好各個路點,并把“parent”和“Next”設置好。然后為每個路點(空物體)添加以下腳本:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class PathNode : MonoBehaviour {

public PathNode m_parent;//路點

public PathNode m_next;

public void SetNext(PathNode node){

if (m_next != null) {

m_next.m_parent = null;

}

m_next = node;

node.m_parent = this;

}

void OnDrawGizmos(){

Gizmos .DrawIcon (this .transform .position ,"timg.jpg");

}

}

如何讓人物或者你需要的游戲物體沿著你架設的路點移動?

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Enemy : MonoBehaviour {

public PathNode m_currentNode;

public float E_life = 15.0f;

public float max_life = 15.0f;

public float speed = 12.0f;

//public Enemy inst;

//void Start () {

//inst = this;

//}

void Update () {

RotateTo ();

MoveTo ();

}

public void RotateTo()

{

float current = transform.eulerAngles.y;

this.transform.LookAt (m_currentNode.transform);

Vector3 target = this.transform.eulerAngles;

float next = Mathf.MoveTowardsAngle (current, target.y, 120 * Time.deltaTime);

this.transform.eulerAngles = new Vector3 (0, next, 0);

}

public void MoveTo()

{

Vector3 pos1 = transform.position;

Vector3 pos2 = m_currentNode.transform.position;

float dist = Vector2.Distance (new Vector2 (pos1.x, pos1.z), new Vector2 (pos2.x, pos2.z));

if (dist < 1.0f) {

if (m_currentNode.m_next == null) {

Destroy (this.gameObject);

}else {m_currentNode = m_currentNode.m_next;}

}

transform.Translate (new Vector3 (0, 0 ,speed * Time.deltaTime));

}

}

如果好幾個物體都需要用同一個代碼,則可以使用下面的技巧(如,控制玩家的代碼,但由于分敵我情況,但腳本都是一樣的)

那么則可以這樣做:

[Range(1, 2)]

這里則申請一個int型的變量,如

public int playernum=1;//初始時是玩家1

這樣則可以在檢視面板出現一個滑塊,滑塊的數字的多少由上面的中括號里的數字來定。

(注:[Range(1, 2)]只能用來控制它下面的一個變量,所以我們要把需要控制的變量寫到下面,并且保證變量類型和上面的一致)

接著就可以在代碼里面這樣來做:

if(playernum==1)

{

//任何你想做的事

}

if(playernum==2)

{

//任何你想做的事

}

....

如何在安卓設備中播放視頻:

using UnityEngine;

using System.Collections;

public class Test : MonoBehaviour {

//以下都為全屏播放

//必須將視頻轉換成MP4格式并且放到Assets/StreamingAssets文件夾下

public GUIStyle buttontype;

public Texture buttonzhanshi;

void OnGUI()

{

//if (GUI.Button(new Rect(40, 600, 68, 76), "播放"))//觸屏后會取消播放

//{

// Handheld.PlayFullScreenMovie("GS.mp4", Color.black, FullScreenMovieControlMode.CancelOnInput);

//}

//if (GUI.Button(new Rect(20, 290, 200, 200), "全屏播放"))

//{

// Handheld.PlayFullScreenMovie("test.mp4", Color.black, FullScreenMovieControlMode.Full);

//}

//if (GUI.Button(new Rect(20, 500, 200, 200), "隱藏按鈕控制器"))//隱藏控制器

//{

// Handheld.PlayFullScreenMovie("test.mp4", Color.black, FullScreenMovieControlMode.Hidden);

//}

if (GUI.Button(new Rect(30, 600, 100, 150), buttonzhanshi,buttontype ))

{

Handheld.PlayFullScreenMovie("GS.mp4", Color.black, FullScreenMovieControlMode.Minimal);

}

}

}

將腳本直接放到攝像機上即可。


怎么在Unity中實現劃線效果呢?即通過鼠標來劃線。代碼如下:

public class DrawLineTest : MonoBehaviour {

private GameObject clone;

private LineRenderer line;

private int i;

public GameObject tf;

void Start () {

}

// Update is called once per frame

void Update () {

if (Input.GetMouseButtonDown(0))

{

clone = (GameObject)Instantiate(tf, tf.transform.position, transform.rotation);//克隆一個帶有LineRender的物體

//clone.gameObject.GetComponent().enabled=false;

//clone.GetComponent().enabled=true;

line = clone.GetComponent();//獲得該物體上的LineRender組件

line.SetColors(Color.blue, Color.red);//設置顏色

line.SetWidth(0.2f, 0.1f);//設置寬度

i = 0;

}

if (Input.GetMouseButton(0))

{

i++;

line.SetVertexCount(i);//設置頂點數

line.SetPosition(i - 1, Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 15)));//設置頂點位置

}

if (Input.GetMouseButtonUp (0)) {

line.enabled=false;

Destroy (clone, 1);

}

}

}

將此腳本掛載到攝像機上?并在工程面板里將tf游戲物體關聯上,運行,就可以劃線了!

未完待續.....

?

?

?

?

?

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容