【Unity3D】Tags和Layers
Tags和Layers分別表示是Unity引擎里面的標(biāo)簽和層,他們都是用來對GameObject進(jìn)行標(biāo)識的屬性,Tags常用于單個GameObject,Layers常用于一組的GameObject。添加Tags和Layers的操作如下:
"Edit" -> "Project Settings" -> "Tags and Layers"來打開設(shè)置面板。
?
??? tag可以理解為一類元素的標(biāo)記,如hero、enemy、apple-tree等。通過設(shè)置tag,可以方便地通過GameObject.FindWithTag()來尋找對象。GameObject.FindWithTag()只返回一個對象,要想獲取多個tag為某值的對象,GameObject.FindGameObjectsWithTag()。
??? 每個GameObject的Inspector面板最上方都也有個Layer選項(xiàng),就在Tag旁邊,unity已經(jīng)有了幾個層,我們新建個層,也叫UI,點(diǎn)擊Add Layer,可以看到從Layer0到Layer7都灰掉了,那是不能用的,從第八個起可以用,Layer和tag還有一個很大的區(qū)別就是layer最多只能有32個層。
??? 在使用過程中,使用Culling Mask、Layer Mask的地方實(shí)際指的就是layer。下面我們來看一段使用layer來檢測碰撞的代碼:
Ray ray1 = nguiCamera.camera.ScreenPointToRay(Input.mousePosition); RaycastHit hit1; LayerMask mask = 1 << LayerMask.NameToLayer("UI"); if (Physics.Raycast(ray1, out hit1, 600, mask.value)) { return; }? LayerMask的NameToLayer是通過層的名稱返回該層的索引,如果是8,然后1<<8換算成LayerMask值,再用LayerMask的value就可以了。注意也必須設(shè)置collider才能接收碰撞,這里才能判斷到。LayerMask實(shí)際上是一個位碼操作,在Unity中Layers一共有32層,這個是不能增加或者減少的: ??? 1 << LayerMask.NameToLayer("UI") 這一句實(shí)際上表示射線查詢只在UI所在這個層級查找是返回的該名字所定義的層的層索引,注意是從0開始。
下面我們來看一個玩家控制的腳本,利用?Physics2D.Linecast檢測物體是否碰撞到地面。
using UnityEngine; using System.Collections;public class PlayerControl : MonoBehaviour {[HideInInspector]public bool facingRight = true; // For determining which way the player is currently facing. [HideInInspector]public bool jump = false; // Condition for whether the player should jump.public float moveForce = 365f; // Amount of force added to move the player left and right.public float maxSpeed = 5f; // The fastest the player can travel in the x axis.public AudioClip[] jumpClips; // Array of clips for when the player jumps.public float jumpForce = 1000f; // Amount of force added when the player jumps.public AudioClip[] taunts; // Array of clips for when the player taunts.public float tauntProbability = 50f; // Chance of a taunt happening.public float tauntDelay = 1f; // Delay for when the taunt should happen.private int tauntIndex; // The index of the taunts array indicating the most recent taunt.private Transform groundCheck; // A position marking where to check if the player is grounded.private bool grounded = false; // Whether or not the player is grounded.private Animator anim; // Reference to the player's animator component.void Awake(){// Setting up references.// 在子對象里面找到groundCheckgroundCheck = transform.Find("groundCheck");// 獲取當(dāng)前的動畫控制器anim = GetComponent<Animator>();}void Update(){// 是為能隨時檢測到groundCheck這個物體,添加一個名叫Ground的Layer,然后把場景中的所有代表地面的物體的Layer設(shè)為Ground// 這里用到了2D射線檢測Physics2D.Linecast()// LayerMask實(shí)際上是一個位碼操作,在Unity3d中Layers一共有32層,這個是不能增加或者減少的:// 1 << LayerMask.NameToLayer("Ground") 這一句實(shí)際上表示射線查詢只在Ground所在這個層級查找 是返回的該名字所定義的層的層索引,注意是從0開始// 每個GameObject的Inspector面板最上方都也有個Layer選項(xiàng),就在Tag旁邊,unity3d已經(jīng)有了幾個層,我們新建個層,也叫UI,點(diǎn)擊Add Layer,可以看到從Layer0到Layer7都灰掉了,那是不能用的,從第八個起可以用,所以在第八個建個UI的層。// 一般情況下我們只用前兩個參數(shù),distance表示射線距離,默認(rèn)是無限遠(yuǎn),重點(diǎn)是最后一個參數(shù)layerMask,專門處理layer過濾的,是個整型,怎么用呢,是靠layer的二進(jìn)制位來操作的// LayerMask的NameToLayer是通過層的名稱返回該層的索引,這里是8,然后1<<8換算成LayerMask值,再用LayerMask的value就可以了。// 注意也必須設(shè)置collider才能接收碰撞,這里才能判斷到。grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground")); // If the jump button is pressed and the player is grounded then the player should jump.// 如果點(diǎn)擊了跳的按鍵,并且已經(jīng)著陸,那么就可以跳起來if(Input.GetButtonDown("Jump") && grounded)jump = true;}// 因?yàn)橹鹘怯螒驅(qū)ο笠褂玫絼傮w力,所以一定要寫在FixedUpdate里面,不能放在Update上void FixedUpdate (){// Cache the horizontal input.// 換取水平方向的移動距離float h = Input.GetAxis("Horizontal");// The Speed animator parameter is set to the absolute value of the horizontal input.// 設(shè)置動畫的速度變量anim.SetFloat("Speed", Mathf.Abs(h));// 給物體添加一個水平的力,讓它移動的時候會產(chǎn)生慣性的效果// If the player is changing direction (h has a different sign to velocity.x) or hasn't reached maxSpeed yet...// 如果速度小于最大的速度 if(h * rigidbody2D.velocity.x < maxSpeed)// ... add a force to the player.rigidbody2D.AddForce(Vector2.right * h * moveForce);// If the player's horizontal velocity is greater than the maxSpeed...if(Mathf.Abs(rigidbody2D.velocity.x) > maxSpeed)// ... set the player's velocity to the maxSpeed in the x axis.rigidbody2D.velocity = new Vector2(Mathf.Sign(rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y);// 轉(zhuǎn)身// If the input is moving the player right and the player is facing left...if(h > 0 && !facingRight)// ... flip the player. Flip();// Otherwise if the input is moving the player left and the player is facing right...else if(h < 0 && facingRight)// ... flip the player. Flip();// If the player should jump...if(jump){// Set the Jump animator trigger parameter.// 觸發(fā)跳的動畫anim.SetTrigger("Jump");// Play a random jump audio clip.int i = Random.Range(0, jumpClips.Length);AudioSource.PlayClipAtPoint(jumpClips[i], transform.position);// Add a vertical force to the player.// 添加一個垂直的力rigidbody2D.AddForce(new Vector2(0f, jumpForce));// Make sure the player can't jump again until the jump conditions from Update are satisfied.jump = false;}}// 轉(zhuǎn)身void Flip (){// Switch the way the player is labelled as facing.facingRight = !facingRight;// Multiply the player's x local scale by -1.Vector3 theScale = transform.localScale;theScale.x *= -1;transform.localScale = theScale;}public IEnumerator Taunt(){// Check the random chance of taunting.float tauntChance = Random.Range(0f, 100f);if(tauntChance > tauntProbability){// Wait for tauntDelay number of seconds.yield return new WaitForSeconds(tauntDelay);// If there is no clip currently playing.if(!audio.isPlaying){// Choose a random, but different taunt.tauntIndex = TauntRandom();// Play the new taunt.audio.clip = taunts[tauntIndex];audio.Play();}}}int TauntRandom(){// Choose a random index of the taunts array.int i = Random.Range(0, taunts.Length);// If it's the same as the previous taunt...if(i == tauntIndex)// ... try another random taunt.return TauntRandom();else// Otherwise return this index.return i;} }?
轉(zhuǎn)載于:https://www.cnblogs.com/qiuxiangmuyu/p/5826418.html
總結(jié)
以上是生活随笔為你收集整理的【Unity3D】Tags和Layers的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python数据库操作pymysql
- 下一篇: CentOS7静默安装oracle11g