Unity 之 Mesh

Meshes

Unity 内置提供了 ProBuilder 插件可以在unity里面创建模型

Unity支持三角形或四边形多边形网格,因此您必须将Nurbs、Nurms和Subdiv曲面转换为多边形。

 

Material generation and assignment

Enable Import Materials on the Material tab of the Import Settings window

  打开Import Materials在导入模型的窗口下,选择Use Embedded(内含的) Materials Unity:

unity会根据模型的名字自动查找相应的贴图,如果没有的话,就创建一个

Colliders

Unity有两种类型的 collidersMesh Colliders 和 Primitive Colliders. Mesh Colliders 使用的是网格数据 and Unity 可以用它们进行环境检测 collision. 当你在Model tab打开 Generate Colliders 时, Unity 会自动添加一个Mesh Collider

如果你在移动游戏对象(例如汽车),你不能使用网格碰撞器,而是使用Primitive colliders. 所以这时候你应该关闭Generate Colliders setting.

Blend shapes

Hints

  • 尽可能多地合并网格。它们应该share Materials and Textures尽可能地共享材料和纹理。这具有巨大的性能优势。

  •  Unity Editor 显示的网格数要比3D软件中显示的要多一点,这是正确的。你看到的是实际发送到GPU渲染的顶点/三角形的数量.除了Material要求发送两次的情况, hard-normals and non-contiguous UVs 也增加了 vertex/triangle 的数量.

 

Mesh Filter

Mesh Filter 是把mesh传递给 Mesh Renderer,然后渲染出来

Properties

Property:Function:
MeshReference to a mesh that will be rendered. The Mesh is located inside your Assets Directory.

Details

在导入mesh时, unity会根据mesh的类型,添加相应的render,比如如果 mesh is skinned,添加 Skinned Mesh Renderer ,或者一个 Mesh Renderer, if it is not.
如果Mesh Renderer不存在,网格将仍然存在于你的场景(和电脑内存),但它不会被绘制

Mesh Renderer

The Mesh Renderer 把在Mesh Filter中的图形渲染到屏幕上

The Mesh Renderer component as it appears in the Inspector window with Receive Global Illumination set to Light Probes.

 

Exposing advanced settings

The Static checkbox on the GameObject.

  勾选Static 复选框,物体在运行时不能移动, 勾选Contribute GI Static checkbox, 或者在

 Lighting 处勾选Contribute Global Illumination复选框. 然后设置 Receive Global IlluminationLightmaps

 

Lighting

 Lighting 包含了 Mesh Renderer 和灯光交互的属性 Unity.

Property:Function:
Cast ShadowsSpecify if and how the Mesh casts shadows when a suitable Light shines on it.
    OnThe Mesh casts a shadow when a shadow-casting Light shines on it.
    OffThe Mesh does not cast shadows.
    Two Sided双面投影. Enlighten, Progressive Lightmapper 不支持
    Shadows Only只显示阴影,不显示网格本身
Receive ShadowsEnable this option to make the Mesh display any shadows that are cast upon it. This is only supported when using the Progressive Lightmapper.
Contribute Global IlluminationEnable this checkbox to include the GameObject in Global Illumination computations. When enabled, Unity also enables the Static checkbox at the top of the GameObject’s Inspector, with the property Contribute Global Illumination enabled.

Enable this property to make the Receive Global Illumination property appear.
Receive Global IlluminationIf you select Lightmaps, the Lightmapping properties appear. You can only configure this option if you’ve enabled Contribute Global Illumination above.

If you do not enable Contribute Global Illumination, the GameObject registers as non-static, the Static checkbox at the top of the inspector doesn’t have a mark, and Receive Global Illumination is grayed out. The GameObject then receives Global Illumination from Light Probes.
Prioritize illumination

Enable this property to always include this GameObject in lighting calculations. This is useful for affecting GameObjects that are far away from this emissive GameObject which, for performance reasons, wouldn’t normally be affected. By default this property is hidden. To expose it, enable the Stat

 

ic checkbox.

 

Skinned Mesh Renderer

Unity 使用 Skinned Mesh Renderer 渲染 Bone animations, 网格根据动画发生形变

Bones 隐藏在蒙皮网格中的不可见对象,影响网格在动画中变形的方式. 骨头连接在一起形成一个层次分明的“骨架”, and 旋转骨骼的关节使其移动定义了动画. 每块骨头都连接到周围网格的一些顶点上.当你播放动画时,这些顶点会随着它们所连接的骨头移动, 所以“皮肤”跟随骨骼的运动. 在一个简单的关节处(例如肘部),网格的顶点受到在那里相遇的两个骨架的影响, 网格随着关节的弯曲而真实地拉伸和旋转. 在更复杂的情况下,超过两根骨头会影响网格的特定区域,导致更微妙的运动。

Unity automatically adds a Skinned Mesh Renderer component to any Mesh that needs it at import time.

经常用他来创建木偶效果,当一个角色被爆炸激中后四肢分开

Properties

Property:Function:
BlendShapesStores the blend shapes defined on this Mesh.存储此网格的混合类型
Rendering Layer Mask遮罩
Renderer Priority渲染的优先级,从低往高渲染
Quality每个顶点使用的骨头数
    Auto

使用 Blend Weights Quality setting.

 

    1 Bone仅仅使用一个骨头影响顶点
    2 BonesUse up to two bones per vertex. This is a common setting for games, because it is a good compromise between visual quality and performance.
    4 BonesUse up to four bones per vertex. If you need to allow more than four bones to influence a single vertex, use the Blend Weights Quality setting.
Update When Offscreen如果在物体在屏幕外,是否还更新其动画,默认是不更新的
Skinned Motion VectorsEnable this option to double-buffer the Mesh skinning
data, so Unity can interpolate the skinned motion and place it into the motion vector Texture. This uses more GPU memory, but leads to more correct motion vectors.
MeshSet the Mesh (skin) used by this Renderer.
Root Bone设置动画的“根”骨头(也就是所有其他移动的相对骨头)。
Bounds设置unity判断物体超出屏幕外的的边界,即:超出这个边界之后就算是不在屏幕内了bounding volume

Determining a GameObject’s visibility

Unity使用网格的边界来决定它是否可见. 也就是说,整个边界要在摄像机的视野之外,然而,一个动画网格的真实边界体积可以随着动画的播放而改变,例如,如果角色在空中举起手,包围体就会变高,Unity在计算最大边界体积时,会考虑所有附加的动画
但是在某些情况下,并不能考虑每个动作的情况。

下面的情况,就会造成计算边界和真实边界有偏差的情况:

  • 在运行时添加的动画
  • 附加的动画
  • 通过 script开始改变骨骼的位置
  • Using vertex Shaders which can push vertices outside the pre-calculated bounds
  • 使用布偶猫

在这些情况下,你可以尝试这些解决方案来解决问题:

  •  手动修改边界的大小,来适应潜在的变化.

  • 打开 Update When Offscreen

 

Text Mesh

 Text Mesh生成3D网格来显示字体

 Component > Mesh > Text Mesh.

Properties

Property:Function:
TextThe text that will be rendered
Offset ZHow far should the text be offset from the transform.position.z when drawing
Character SizeThe size of each character (This scales the whole text.)
Line SpacingHow much space will be in-between lines of text.
AnchorWhich point of the text shares the position of the Transform.
AlignmentHow lines of text are aligned (Left, Right, Center).
Tab SizeHow much space will be inserted for a tab ‘\t’ character. This is a multiplum of the ‘spacebar’ character offset.
Font SizeThe size of the font. This can override the size of a dynamic font.
Font StyleThe rendering
style of the font. The font needs to be marked as dynamic.
Rich TextWhen selected this will enable tag processing when the text is rendered.
FontThe TrueType Font to use when rendering the text.
ColorThe global color to use when rendering the text.

Details

Note: If you want to change the font for a Text Mesh, need to set the component’s font property and also set the texture of the font material to the correct font texture. This texture can be located using the font asset’s foldout. If you forget to set the texture then the text in the mesh will appear blocky and misaligned.

Hints

  • 可以从 1001freefonts.com 网站下载免费的字体(download the Windows fonts since they contain TrueType Fonts).
  • 通过 “\n” 换行
  • Text meshes 可以使用简单的标记样式. See the Styled Text page for more details.
  • Fonts in Unity 首先被渲染到纹理贴图上. 果字体大小设置得太小,这些字体纹理将显示为块状. 因为 TextMesh资源使用 quads渲染

 

Text Asset

Text Assets 是用于导入文本文件的格式.支持的文本格式有:

  • .txt
  • .html
  • .htm
  • .xml
  • .bytes
  • .json
  • .csv
  • .yaml
  • .fnt

Note 注意:脚本问价也是text 资源,也可以使用 AssetDatabase.FindAssets function方法查找,它们的标签是“t:TextAsset” .

The Text Asset Inspector

The Text Asset Inspector

Properties

Property:Function:
TextThe full text of the asset as a single string.

Details

 Text Asset:当你正在发布建设它的的时候可以从不同的text文件中获得信息,您可以编写一个简单的.txt文件,并将文本非常容易地带入您的游戏。它不打算在运行时生成文本文件。

看一下下面的方法:

你正在制作一个传统的文本为主的冒险游戏. 为了简化制作,您希望将游戏中的所有文本分解到不同的房间中.在这种情况下,您将创建一个文本文件,其中包含将在一个房间中使用的所有文本. 从那里可以很容易地引用 正确的 Text Asset 根据你进入的房间. 然后使用一些定制的解析逻辑,您可以非常轻松地管理大量文本。

Binary data

文本资产的一个特殊特性是它可以用来存储二进制数据.  .bytes后缀的文件可以被当作 a text asset加载,里面的数据可以通过bytes 属性访问

例如,将一个jpeg文件放入Resources文件夹并将扩展名更改为.bytes:

//Load texture from disk
TextAsset bindata= Resources.Load("Texture") as TextAsset;
Texture2D tex = new Texture2D(1,1);
tex.LoadImage(bindata.bytes); 

请注意,扩展名为.txt和.bytes的文件将被视为文本文件和二进制文件, 不要尝试使用.txt扩展名存储二进制文件,因为这将在尝试从其中读取数据时创建意外行为。

Hints

  • Text Assets 在发布时向其他的资源一样被序列化. .
  • Text Assets 不打算用于在运行时生成文本文件.

 

Font

Fonts 可以创建或导入用于GUIText或TextMesh组件

Importing Font files

unity支持的文字格式(.ttf files) and OpenType Fonts (.otf files).

.

Import Settings for a font

Import Settings for a font

Property:Function:
Font Size字体大小
Rendering mode字体渲染模式,告诉Unity如何应用平滑到字形。
Character要导入到字体纹理中的字体的字符集

Import Settings specific to dynamic fonts

Property:Function:
Include Font Data在Character为dynamic时,勾选这个可以被打包到发布文件当中,如果不勾选,默认玩家已经安装了此字体.
Font Names当字体或字符不可用时要使用的后备字体列表(参见下面)。

导入字体后,会生成两种资源, “font material” and “font texture”.Unity中的字体被转换为纹理,使用 quads渲染出来
.调整字体的大小,就是在调整它所占用的像素. Text Mesh 会自动生成字体贴图. 您将需要改变字体的大小,以使这些资产看起来更清晰。TextMesh里面的font size越大字体越清晰,然后用charactor整体缩放字体的大小

Dynamic fonts

设置为 Dynamic: Unity不会预先生成一个所有字体字符的纹理. 它将使用FreeType字体渲染引擎来动态创建纹理. 这样做的好处是可以节省下载大小和纹理内存, 特别是当您使用的字体通常包括在用户系统中,所以你不需要包括字体数据,或者当你需要支持亚洲语言或大字体尺寸(这将使字体纹理非常大)。

当Unity试图使用动态字体呈现文本时, 但是它找不到字体 (因为 Include Font Data 没有被勾选, 而且字体没有安装在用户机器上), 或者字体不包含请求的字形 (比如在东亚脚本中使用拉丁字体呈现文本, 或者当使用粗体/斜体文本样式时), 然后它将尝试 Font Names 列出的字体,看这些字体是否存在可用 ,如果也没有,unity会使用当前设备上有的一种字体

一些平台(WebGL, some consoles) 没有 OSunity可以访问的默认字体. 这些平台上 Include Font Data 将被忽略, 字体数据将始终包括在内. 所有作为回退的字体都必须包含在项目中,因此如果您需要呈现国际文本或字体的粗体/斜体版本, 您需要将具有所需字符的字体文件添加到项目中, 把他们添加到 Font Names 中,作为后备字体.

Default font asset

默认使用动态字体 Arial. 如果你计算机上没有改字体,会使用 Liberation Sans.

Liberation Sans不包含粗体和斜体

Custom fonts

创建自己的字体‘Create->custom font’ from the project window.

 Ascii Start Offset 字段是一个十进制数,它定义了您想要开始Character Rects索引的Ascii索引,例如,如果您的Ascii开始偏移量设置为0,那么大写字母A将在索引65处,但是如果Ascii开始偏移量设置为65,那么字母A将在索引0处. 您可以在这里查阅Ascii表,但是您应该记住,自定义字体使用的是十进制Ascii编码系统。

Tracking 每个字之间的间距and Line spacing 行距.

创建一个 font material 你需要把字体作为texture导入,

The Character Rects section is where each character of your font is defined.

The Size field is for defining how many characters are in your font.

Within each Element there is an index field for the ascii index of the character. This will be an integer that represents the character in this element.

To work out the UV values you need to figure out how your characters are positioned on a scale of 0 to 1. You divide 1 by the number of characters on a dimension. For example if you have a font and the image dimensions on it are 256x128, 4 characters across, 2 down (so 64x64), then UV width will be 0.25 and UV height will be 0.5.

For UV X and Y, it’s just a matter of deciding which character you want and multiplying the width or height value times the column/row of the letter.

Vert size is based on the pixel size of the characters e.g. your characters are each 128x128, putting 128 and –128 into the Vert Width and Height will give properly proportioned letters. Vert Y must be negative.

Advance will be the desired horizontal distance from the origin of this character to the origin of the next character in pixels. It is multiplied by Tracking when calculating the actual distance.

Example of custom font inspector with values

Example of custom font inspector with values

Unicode support

Unity has full unicode support. Unicode text allows you to display German, French, Danish or Japanese characters that are usually not supported in an ASCII character set. You can also enter a lot of different special purpose characters like arrow signs or the option key sign, if your font supports it.

To use unicode characters, choose either Unicode or Dynamic from the Characters drop-down in the Import Settings. You can now display unicode characters with this font. If you are using a GUIText or Text Mesh
, you can enter unicode characters into the Component’s Text
field in the Inspector.

You can also use unicode characters if you want to set the displayed text from scripting. The C# compiler fully supports Unicode based scripts
. You have to save your scripts with UTF–16 encoding. Now you can add unicode characters to a string in your script and they will display as expected in UnityGUI, a GUIText, or a Text Mesh.

Note that surrogate pairs are not supported.

Changing Font Color

There are different ways to change the color of your displayed font, depending on how the font is used.

GUIText and Text Mesh

If you are using a GUIText or a Text Mesh, you can change its color by using a custom Material
for the font. In the Project View, click on Create > Material, and select and set up the newly created Material in the Inspector. Make sure you assign the texture from the font asset to the material. If you use the built-in GUI/Text Shader shader for the font material, you can choose the color in the Text Color property of the material.

UnityGUI

If you are using UnityGUI scripting to display your font, you have much more control over the font’s color under different circumstances. To change the font’s color, you create a GUISkin from Assets > Create > GUI Skin, and define the color for the specific control state, e.g. Label > Normal > Text Color. For more details, please read the GUI Skin page.

Hints

  • To display an imported font select the font and choose GameObject > Create Other > 3D Text.
  • You can reduce the generated texture size for fonts by using only lower or upper case characters.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TO_ZRG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值