Tags和Layers分别表示是Unity引擎里面的标签和层,他们都是用来对GameObject进行标识的属性,Tags常用于单个GameObject,Layers常用于一组的GameObject。添加Tags和Layers的操作如下:

"Edit" -> "Project Settings" -> "Tags and Layers"来打开设置面板。

tag可以理解为一类元素的标记,如hero、enemy、apple-tree等。通过设置tag,可以方便地通过GameObject.FindWithTag()来寻找对象。GameObject.FindWithTag()只返回一个对象,要想获取多个tag为某值的对象,GameObject.FindGameObjectsWithTag()。

每个GameObject的Inspector面板最上方都也有个Layer选项,就在Tag旁边,unity已经有了几个层,我们新建个层,也叫UI,点击Add Layer,可以看到从Layer0到Layer7都灰掉了,那是不能用的,从第八个起可以用,Layer和tag还有一个很大的区别就是layer最多只能有32个层。

在使用过程中,使用Culling Mask、Layer Mask的地方实际指的就是layer。下面我们来看一段使用layer来检测碰撞的代码:

  1. Ray ray1 = nguiCamera.camera.ScreenPointToRay(Input.mousePosition);
  2. RaycastHit hit1;
  3. LayerMask mask = << LayerMask.NameToLayer("UI");
  4. if (Physics.Raycast(ray1, out hit1, , mask.value))
  5. {
  6. return;
  7. }

LayerMask的NameToLayer是通过层的名称返回该层的索引,如果是8,然后1<<8换算成LayerMask值,再用LayerMask的value就可以了。注意也必须设置collider才能接收碰撞,这里才能判断到。LayerMask实际上是一个位码操作,在Unity中Layers一共有32层,这个是不能增加或者减少的:     1 << LayerMask.NameToLayer("UI") 这一句实际上表示射线查询只在UI所在这个层级查找是返回的该名字所定义的层的层索引,注意是从0开始。

下面我们来看一个玩家控制的脚本,利用 Physics2D.Linecast检测物体是否碰撞到地面。

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PlayerControl : MonoBehaviour
  5. {
  6. [HideInInspector]
  7. public bool facingRight = true; // For determining which way the player is currently facing.
  8. [HideInInspector]
  9. public bool jump = false; // Condition for whether the player should jump.
  10.  
  11. public float moveForce = 365f; // Amount of force added to move the player left and right.
  12. public float maxSpeed = 5f; // The fastest the player can travel in the x axis.
  13. public AudioClip[] jumpClips; // Array of clips for when the player jumps.
  14. public float jumpForce = 1000f; // Amount of force added when the player jumps.
  15. public AudioClip[] taunts; // Array of clips for when the player taunts.
  16. public float tauntProbability = 50f; // Chance of a taunt happening.
  17. public float tauntDelay = 1f; // Delay for when the taunt should happen.
  18.  
  19. private int tauntIndex; // The index of the taunts array indicating the most recent taunt.
  20. private Transform groundCheck; // A position marking where to check if the player is grounded.
  21. private bool grounded = false; // Whether or not the player is grounded.
  22. private Animator anim; // Reference to the player's animator component.
  23.  
  24. void Awake()
  25. {
  26. // Setting up references.
  27. // 在子对象里面找到groundCheck
  28. groundCheck = transform.Find("groundCheck");
  29. // 获取当前的动画控制器
  30. anim = GetComponent<Animator>();
  31. }
  32.  
  33. void Update()
  34. {
  35. // 是为能随时检测到groundCheck这个物体,添加一个名叫Ground的Layer,然后把场景中的所有代表地面的物体的Layer设为Ground
  36. // 这里用到了2D射线检测Physics2D.Linecast()
  37. // LayerMask实际上是一个位码操作,在Unity3d中Layers一共有32层,这个是不能增加或者减少的:
  38. // 1 << LayerMask.NameToLayer("Ground") 这一句实际上表示射线查询只在Ground所在这个层级查找 是返回的该名字所定义的层的层索引,注意是从0开始
  39. // 每个GameObject的Inspector面板最上方都也有个Layer选项,就在Tag旁边,unity3d已经有了几个层,我们新建个层,也叫UI,点击Add Layer,可以看到从Layer0到Layer7都灰掉了,那是不能用的,从第八个起可以用,所以在第八个建个UI的层。
  40. // 一般情况下我们只用前两个参数,distance表示射线距离,默认是无限远,重点是最后一个参数layerMask,专门处理layer过滤的,是个整型,怎么用呢,是靠layer的二进制位来操作的
  41. // LayerMask的NameToLayer是通过层的名称返回该层的索引,这里是8,然后1<<8换算成LayerMask值,再用LayerMask的value就可以了。
  42. // 注意也必须设置collider才能接收碰撞,这里才能判断到。
  43. grounded = Physics2D.Linecast(transform.position, groundCheck.position, << LayerMask.NameToLayer("Ground"));
  44.  
  45. // If the jump button is pressed and the player is grounded then the player should jump.
  46. // 如果点击了跳的按键,并且已经着陆,那么就可以跳起来
  47. if(Input.GetButtonDown("Jump") && grounded)
  48. jump = true;
  49. }
  50.  
  51. // 因为主角游戏对象要使用到刚体力,所以一定要写在FixedUpdate里面,不能放在Update上
  52. void FixedUpdate ()
  53. {
  54. // Cache the horizontal input.
  55. // 换取水平方向的移动距离
  56. float h = Input.GetAxis("Horizontal");
  57.  
  58. // The Speed animator parameter is set to the absolute value of the horizontal input.
  59. // 设置动画的速度变量
  60. anim.SetFloat("Speed", Mathf.Abs(h));
  61.  
  62. // 给物体添加一个水平的力,让它移动的时候会产生惯性的效果
  63. // If the player is changing direction (h has a different sign to velocity.x) or hasn't reached maxSpeed yet...
  64. // 如果速度小于最大的速度
  65. if(h * rigidbody2D.velocity.x < maxSpeed)
  66. // ... add a force to the player.
  67. rigidbody2D.AddForce(Vector2.right * h * moveForce);
  68.  
  69. // If the player's horizontal velocity is greater than the maxSpeed...
  70. if(Mathf.Abs(rigidbody2D.velocity.x) > maxSpeed)
  71. // ... set the player's velocity to the maxSpeed in the x axis.
  72. rigidbody2D.velocity = new Vector2(Mathf.Sign(rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y);
  73.  
  74. // 转身
  75. // If the input is moving the player right and the player is facing left...
  76. if(h > && !facingRight)
  77. // ... flip the player.
  78. Flip();
  79. // Otherwise if the input is moving the player left and the player is facing right...
  80. else if(h < && facingRight)
  81. // ... flip the player.
  82. Flip();
  83.  
  84. // If the player should jump...
  85. if(jump)
  86. {
  87. // Set the Jump animator trigger parameter.
  88. // 触发跳的动画
  89. anim.SetTrigger("Jump");
  90.  
  91. // Play a random jump audio clip.
  92. int i = Random.Range(, jumpClips.Length);
  93. AudioSource.PlayClipAtPoint(jumpClips[i], transform.position);
  94.  
  95. // Add a vertical force to the player.
  96. // 添加一个垂直的力
  97. rigidbody2D.AddForce(new Vector2(0f, jumpForce));
  98.  
  99. // Make sure the player can't jump again until the jump conditions from Update are satisfied.
  100. jump = false;
  101. }
  102. }
  103.  
  104. // 转身
  105. void Flip ()
  106. {
  107. // Switch the way the player is labelled as facing.
  108. facingRight = !facingRight;
  109.  
  110. // Multiply the player's x local scale by -1.
  111. Vector3 theScale = transform.localScale;
  112. theScale.x *= -;
  113. transform.localScale = theScale;
  114. }
  115.  
  116. public IEnumerator Taunt()
  117. {
  118. // Check the random chance of taunting.
  119. float tauntChance = Random.Range(0f, 100f);
  120. if(tauntChance > tauntProbability)
  121. {
  122. // Wait for tauntDelay number of seconds.
  123. yield return new WaitForSeconds(tauntDelay);
  124.  
  125. // If there is no clip currently playing.
  126. if(!audio.isPlaying)
  127. {
  128. // Choose a random, but different taunt.
  129. tauntIndex = TauntRandom();
  130.  
  131. // Play the new taunt.
  132. audio.clip = taunts[tauntIndex];
  133. audio.Play();
  134. }
  135. }
  136. }
  137.  
  138. int TauntRandom()
  139. {
  140. // Choose a random index of the taunts array.
  141. int i = Random.Range(, taunts.Length);
  142.  
  143. // If it's the same as the previous taunt...
  144. if(i == tauntIndex)
  145. // ... try another random taunt.
  146. return TauntRandom();
  147. else
  148. // Otherwise return this index.
  149. return i;
  150. }
  151. }

[Unity2D]Tags和Layers的更多相关文章

  1. 【Unity3D游戏开发】基础知识之Tags和Layers (三二)[转]

    Tags和Layers分别表示是Unity引擎里面的标签和层,他们都是用来对GameObject进行标识的属性,Tags常用于单个GameObject,Layers常用于一组的GameObject.添 ...

  2. Tags and Layers

    [Tags and Layers] 1.tags and layers 配置面板."Edit" -> "Project Settings" -> & ...

  3. 【Unity3D】Tags和Layers

    Tags和Layers分别表示是Unity引擎里面的标签和层,他们都是用来对GameObject进行标识的属性,Tags常用于单个GameObject,Layers常用于一组的GameObject.添 ...

  4. Unity3D-第一视角射击游戏

    一.新建关卡 File,Save Scene,File,New Scene,File,Save Scene as... ,Level02.unity 1.建立场景 从Assets中拖放场景模型到Hie ...

  5. Unity3D Layer要点

    简介         Layer可以用于光照的分层和物理碰撞的分层,这样可以很好地进行性能优化 数据结构         Layer在Unity中有3中呈现方式:1.string名字,2.int层索引 ...

  6. 在Unity中实现小地图(Minimap)

    小地图的基本概念众所周知,小地图(或雷达)是用于显示周围环境信息的.首先,小地图是以主角为中心的.其次,小地图上应该用图标来代替真实的人物模型,因为小地图通常很小,玩家可能无法看清真实的模型.大多数小 ...

  7. Unity 2D游戏开发教程之游戏中精灵的跳跃状态

    Unity 2D游戏开发教程之游戏中精灵的跳跃状态 精灵的跳跃状态 为了让游戏中的精灵有更大的活动范围,上一节为游戏场景添加了多个地面,于是精灵可以从高的地面移动到低的地面处,如图2-14所示.但是却 ...

  8. UNITY VR 视频/图片 开发心得(二)

    上回说到了普通的全景图片,这回讲真正的VR. 由于这种图片分为两部分,所以我们需要两个Camera对象以及两个球体.首先新建一个Camera对象,并将其命名为RightEye(其它名字也无妨,只要你自 ...

  9. Unity射击游戏实例—物理碰撞的实现

    前言: 这一篇章实现物理碰撞,就是游戏体碰撞减装甲,这几天想要试着做出兼具装甲与血量的模式,可自动回复的装甲与永久损伤的血量,在一些平台上找到了不少有意思的模型,有兴趣的可以自己找找模型替换一下. 射 ...

随机推荐

  1. 三、Java基础---------关于继承、构造函数、静态代码块执行顺序示例讲解

    在上节博客中曾提到过类的继承,这篇文章主要是介绍类的继承.构造函数以及静态代码块的执行顺序. 首先接着分析在黑马基础测试中的一个关于继承的题目,题目描述如下: 声明类Person,包含2个成员变量:n ...

  2. 设置Tab键为四个空格

    https://my.oschina.net/xunxun10/blog/110074

  3. MongoDB C# / .NET Driver

    MongoDB C# Driver是官方提供的.NET C#驱动. Getting Started with the C# Driver C# Driver Tutorial C# Driver LI ...

  4. Linux下创建与解压tar, tar.gz和tar.bz2文件及压缩率对比 | 沉思小屋

    刚 在qq群里面一位仁兄问到文件压缩的命令,平时工作中大多用解压缩命令,要是遇到压缩就现查(这不是一个好习惯),于是整理下Linux下创建与解压 zip.tar.tar.gz和tar.bz2文件及他们 ...

  5. PHP将XML数据转换为数组

    <?php $s=join(,file('httpapi.elong.comxmlv2.0hotelcn0132701501.xml')); $result = xml_to_array($s) ...

  6. html+css复习之第2篇 | javascript

    1. java 中定义数组和对象: 数组(Array)字面量 定义一个数组: [40, 100, 1, 5, 25, 10] 对象(Object)字面量 定义一个对象: {firstName:&quo ...

  7. 新学习的语言Groovy

    什么是 Groovy? Groovy 是 JVM 的一个替代语言 —替代 是指可以用 Groovy 在 Java 平台上进行 Java 编程,使用方式基本与使用 Java 代码的方式相同.在编写新应用 ...

  8. [xcode]Xcode查找函数(方法)调用及被调用

    参考资料:http://stackoverflow.com/questions/7145045/find-method-references-in-xcode 这个功能有的说是 Find Caller ...

  9. SIM卡应用-OPN,PLMN,SPN

    SIM卡应用 移动运营商已经将SIM卡用於很多不同的应用,下面列出了其中最主要的应 用∶ ·漫游应用∶确保手机可以在漫游之後选择缺省的运营商网络.一个SIM应用是可以在手机漫游到某个合作夥伴运营商网络 ...

  10. JVM的classloader(转)

    Java中一共有四个类加载器,之所以叫类加载器,是程序要用到某个类的时候,要用类加载器载入内存.    这四个类加载器分别为:Bootstrap ClassLoader.Extension Class ...