U3D笔记11:47 2016/11/30-15:15 2016/12/19

时间:2021-01-28 14:24:30

11:47 2016/11/30
Before you can load a level you have to add it to the list of levels used in the game. Use File->Build Settings... in Unity and add the levels you need to the level list there. MonoBehaviour.OnLevelWasLoaded is called on all active game objects after the level has been loaded
在加载任何场景之前,必须将场景添加到build settings列表

Assets/code/Engine/Common/Config.cs(51,49): error CS0117: `System.IO.File' does not contain a definition for `ReadAllText',平台切换的问题,默认是webplayer,应该切换到windows
14:53 2016/12/3
这个问题是由于U3D editor设置platform为 web player
15:35 2016/12/3

17:49 2016/11/30
loadScene(index or name)可用路径或场景名,如果用场景名有重名是只加载第一个

11:31 2016/12/9
U3D里直接实现IPointerUpHandler接口后发现OnPointerUp没有调用,必须IPointerDownHandler, IPointerUpHandler一起实现,然后OnPointerDown,OnPointerUp就都能收到消息了

15:38 2016/12/9
调用顺序:awake, onlevelwasloaded, start
注意,onlevelwasloaded 在 start之前。
awake是一个物体实例化完成之后立即调用,场景中所有物体都实例化完成,所有资源都加载完后,调用onlevelwasloaded,然后紧接着调用各个物体的start()
18:15 2016/12/11
Expanding invalid MinMaxAABB 这个诡异的错误原因是:对某物体的父接点设置了setactive(false),然后又去企图设置它的属性。
public void Init ()
{
cellsDict = new Dictionary<int, GameObject> ();
cellPrefab = Resources.Load ("blockCell") as GameObject;
lineAux = new GameObject ("lineAux");
lineAux.SetActive (true);//这里如果不设置,则lineAux可能是隐藏的,下面对材质的设置将会触发以上错误。

LineRenderer lrder = lineAux.AddComponent<LineRenderer> ();

lrder.SetColors (Color.green, Color.green);
lrder.SetWidth (0.1f, 0.1f);
lrder.SetVertexCount (5);
lrder.material = new Material (Shader.Find ("Qx/Unity/Legacy/Legacy-Diffuse"));
lrder.material.color = Color.green;
}
////////////////////////////////////////////////////////////原因似乎不是如上//////////////////////////////////////////////
UnityEngine.DockArea()......错误信息中有此串,似乎是UNITY编辑器自身引起的,错误是偶现的。
IsFinite(outDistanceForSort)
UnityEditor.DockArea:OnGUI()
10:56 2016/12/12
=============================上面问题原因是:SetVertexCount(5),设置了五个顶点,又没有去给顶点赋值,默认值又比较大导致了计算数据出现无穷大的现象================
从上面也可看出,linerender居然和GUI有关

12:12 2016/12/12
U3D打可执行包时,shader除了在edit-projectsettings-graphic中设置的,其它都不会打进EXE中,也不会assetbundle,除非设置了打包标识 assetbundle的tag
13:20 2016/12/12
GameObject[] auxLines = new GameObject[4]; 这时只是声明了一个数组,数组的每个成员还没赋值,还不能使用。
这时auxLines [0] auxLines [1],auxLines [2],auxLines [3]全为null
还不能使用。要想使用,必须:
for(int i=0; i<4; ++i)
auxLines [i] = new GameObject()
14:05 2016/12/12
对于每帧getcomponent的东西,消耗有点大,最好做预处理。
14:24 2016/12/12
放在Editor下的脚本挂到游戏场景的物体上是无效的,提示:没有继承自Monobehavivor

11:38 2016/12/15
将一张带有透明像素的texture转换为sprite时,U3D会根据透明像素的范围自动将图片切分为许多的三角形,增加了许多顶点。这样做是为了尽可能少的绘制透明像素。
以减少ALPHA测试与ALPHA混合。以增加三角形为代价换取透明像素的绘制。
10:14 2016/12/16
UI Prefab不同于模型prefab,例如制作了一个UIprefab A,上面有一个Image, button。A 的大小为8K, 再制作一个类似A的 prefab B, 二者总大小将为 A+B = 16K,
也就是说UI的prefab是实际容量的增加,而不是对UI元素的引用。比如A和B都有BUTTON,实际是增加BUTTON的大小而不是对它的引用。
11:41 2016/12/16
BuildPipeline.BuildPlayer (scenes, target_dir, build_target, build_options);是打包可执行程序。
BuildPipeline.BuildAssetBundles 是打包资源
18:21 2016/12/16
当发现SVN无法cleanup时,将清理选项全部勾选,再清理就可以了。
8:47 2016/12/19
原来TAG也可以在打包版本中使用。之前应该是其它问题导致的。
11:19 2016/12/19
GameObject.Destroy(obj.transform:GetChild(i))会发生什么? 提示:不能被销毁,XX组件还在依赖它。
这是因为obj.transform:GetChild(i)获取的是一个transform,而不是一个Gameobject,单纯销毁GO的Transform组件是不允许的,因为此GO的其它组件还在。
正确写法:GameObject.Destroy(obj.transform:GetChild(i).gameobject)

14:41 2016/12/19
The GC Alloc column shows how much memory has been allocated in the current frame, which will be collected later by the garbage collector. By designing your code to keep this value at zero, you can avoid the Garbage Collector from causing hiccups in your framerate.
15:15 2016/12/19
IPointerDownHandler, IPointerUpHandler, IDragHandler这些接口实现了后,如果是UI,比如BUTTON,当用户点击了此按钮,此按钮的OnPointerDown等就会被调用。
如果是场景中的3D物体实现了以上接口,比如一个CUBE,点击CUBE,此时CUBE的OnPointerDown并不会被调用。还必须给主相机增加一个physics raycaster并且指定CUBE所在的LAYER在EVENTMASK列表中。
--------------
但如果不给相机增加physics raycaster,也可以通过 Physics.RayCast(ray, dist, out hitInfo, layermask)接口来判断有没有物体被点中,且返回点中的坐标等信息。
layermask传递希望进行测试的LAYER。
15:37 2016/12/19
相机的culling mask,在其列表中打勾的都是被此相机绘制的,不打勾的则不会绘制。这与setactive有什么区别?
这里仅是绘制与否,并不是隐藏与显示。当物体被隐藏了就无法点击它,无法与它交互。而不绘制的物体还可以被点击。还会被UPDATE等。而一个物体被setactive(false)后就不能被点击 了。

10:39 2016/12/20
addcomponent后就会调用 awake, start等函数。
12:04 2016/12/22
U3D不支持DDS格式,支持的格式:PSD, TIFF, JPG, TGA, PNG, GIF, BMP, IFF, PICT

18:03 2016/12/26
组件是一个对象,而不是一个类。
18:45 2016/12/28
When you set the Characters drop-down in the Import Settings to Dynamic, Unity will not pre-generate a texture with all font characters. Instead, it will use the FreeType font rendering engine to create the texture on the fly. This has the advantage that it can save in download size and texture memory, especially when you are using a font which is commonly included in user systems, so you don’t have to include the font data, or when you need to support asian languages or large font sizes (which would make the font textures very large using normal font textures).

动态字体,选择了动态字体,UNITY编辑器不会产生一张包括所有字符的字体贴图。而是在使用时利用freetype字体引擎产生部分需要的贴图。
如果不选择动态字体,则打包时就多了一张包含全部字符的字体贴图,可能很大。如果用户机器上有对应字体时。。。

11:53 2016/12/29

Parent of RectTransform is being set with parent property. Consider using the SetParent method instead,
with the worldPositionStays argument set to false. This will retain local orientation and scale rather than world orientation and scale,
which can prevent common UI scaling issues.
10:00 2016/12/30
自己制作的按钮,点了之后没有表现效果,但事件响应了。这是由于按钮的 target graphic选错了,如果造成其它按钮正在使用的图片,则会效果会表现在其它按钮上。
正确的做法是选择自己按钮的一张图片,才能看到正确的表现效果。
11:12 2017/1/5
C# 字符串处理几个好用函数, substring, startwith, endwith, replace(str, str1, str2)
9:57 2017/1/6
Upgrade Guides 能看到各版本的改动
Upgrading to Unity 5.3 [Released 08dec15]
Upgrading to Unity 5.2 [Released 08sep15]
Upgrading to Unity 5.0 [Released 03mar15] (There are several sub-pages to this.)
Upgrading to Unity 4.0 [Released 13nov12]
Upgrading to Unity 3.5 [Released 14feb12]
11:54 2017/1/6
U3D的压缩算法有LZMA,LZ4,其中LZMA是7Z使用的压缩算法,压缩率:7z > rar > zip。LZ4压缩率要低一些,但速度极快,不用全部解压就可以使用其中文件。
因此在游戏中最适合使用LZ4,也就是 chunk-based的压缩。
对所有压缩算法的通用规则:图片视频几乎没什么压缩率,其它资源压缩较大,游戏模型原始资源很大但压缩率可达80-90%,因此,对于游戏资源,压缩后图片最占资源了。
特别是2D地图的游戏。