工作细节笔记

[MarshalAs(UnmanagedType.I1)]

Indicates how to marshal the data between managed and unmanaged code.

[ExecuteAlways]

Makes instances of a script always execute, both as part of Play Mode and when editing.

TextAsset

Text Assets are a format for imported text files. When you drop a text file into your Project Folder, it will be converted to a Text Asset. The supported text formats are:

  • .txt

  • .html

  • .htm

  • .xml

  • .bytes

  • .json

  • .csv

  • .yaml

  • .fnt

XPath

XML Path Language

使用**tagName[@attributeName=’a’]**取特定子节点

使用”@”取属性节点

C# @string

逐字字符串(不进行转义),正则表达式中经常需要使用

XmlDocument.Save(XmlWriter)

保存Xml文件

TRANSFORM_TEX ()

简单来说,TRANSFORM_TEX主要作用是拿顶点的uv去和材质球的tiling和offset作运算, 确保材质球里的缩放和偏移设置是正确的。

下面这两个函数是等价的:

o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
o.uv = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;

_MainTex_ST.xy是tiling

_MainTex_ST.zw是offset

如果Tiling 和Offset你留的是默认值,即Tiling为(1,1) Offset为(0,0)

UnityCG.cginc

// Transforms 2D UV by scale/bias property
#define TRANSFORM_TEX(tex,name) (tex.xy * name##_ST.xy + name##_ST.zw)

预编译符号##和#

##:预编译时将符号直接拼接

#:预编译时将符号转为字符串

CustomEditor “C#ScriptName”

自定义shader材质编辑面板

Load (DirectX HLSL Texture Object)

顶点着色器中获取纹理

UnpackNormalmapRGorAG Function in UnityCG.cginc

// Unpack normal as DXT5nm (1, y, 1, x) or BC5 (x, y, 0, 1)
// Note neutral texture like "bump" is (0, 0, 1, 1) to work with both plain RGB normal and DXT5nm/BC5
fixed3 UnpackNormalmapRGorAG(fixed4 packednormal)
{
// This do the trick
packednormal.x *= packednormal.w;
fixed3 normal;
normal.xy = packednormal.xy * 2 - 1;
normal.z = sqrt(1 - saturate(dot(normal.xy, normal.xy)));
return normal;
}