今天考試又被問到對Unity從來沒有查明的問題,就是Monobehaviour裡面原本被已經決定好的function的執行順序,像是Awake或者Start之類的,原本以為只有一些簡單的Awake、Start、OnEnable、Update、OnDisable、OnDestroy這幾個,查一下Unity官網,想不到還真多。
唯一讓我覺得奇怪的是Start竟然比OnEnable晚,真是奇怪.....
2015年3月24日 星期二
2015年3月9日 星期一
C# value type or reference type
在C# type內有些data type常常搞錯,不知道是value type或者reference type,因為考試又被電一次,在這備註一下。
主要是reference type才有可能為null。
主要是reference type才有可能為null。
string testValue;
if(testVlaue != null)
{
//是否會進來
}
在這邊因為string是reference type,只是他的operator"="被改寫成是用copy value的方式,看起來很像value type。所以這邊的if不會進來。decimal testValue;
if(testVlaue != null)
{
//是否會進來
}
decimal是value type所以這個if永遠都會進來。DateTime testValue;
if(testVlaue != null)
{
//是否會進來
}
DateTime這個data type也是令人意外的type,他是value type,所以這個if永遠都會進來。int? testValue;
if(testVlaue != null)
{
//是否會進來
}
這個int?就是reference type,所以這個if不會進來。
Tread start problem
今天考試被電了一回,題目是
public class A
{
public int count = 0;
public void AddCount(int value)
{
count += value;
}
}
public void test()
{
A recordA = new A();
Threand thread = new Thread(delegate()
{
recordA.AddCount(1);
}
thread.Start();
recordA.AddCount(1);
}
這邊是因為thread.Start()時,就有可能和下一行recordA.AddCount(1);同時執行,並且在拿count這個變數的值時,有可能都拿到相同的值。所以這邊應該在AddCount function內加lock。
2014年11月4日 星期二
Unity script hash code not compatible
1. prefab attach的script更新之後,將prefab包成Asset bundle,包出去之後。下次包版時,script有異動,但是即使Untiy 做Save project的動作,都不會造成prefab本身有任何異動,所以只有script再包出去,結果新的程式讀到舊的AssetBundle,就會造成錯誤。Untiy報的錯誤是 script hash code not compatible。
2014年10月31日 星期五
Unity mono Awake() and Start() timing
Awake呼叫的時機點在於Component被產生時的那瞬間。
那什麼時候代表Component產生時的那瞬間:
Start呼叫的時機點在於Component第一次Enable時的那瞬間。
那什麼時候代表Component Enable時的那瞬間:
Awake一定比Start還早,而且是場景上所有的Awake都結束後,才會呼叫Start。而預設各Component執行Start的順序等同各Component執行Awake的順序,除非在Awake動態改變Component的enable順序。
而Awake和Start最大的差別就在於一種情況:
當GameObject為Active,但是Component是Disabled的狀態,Awake會呼叫的但是Start是不會的。
GameObject上的Components彼此Awake的順序是?
理論上應該是新增Component的順序,但是基本上建議不要依賴這個順序,因為常常會忘記........雖然在Unity工具列的Edit->Project Settings->Script Execution Order來決定,但是一樣不方便。
GameObject之間的Components彼此Awake的順序是?
Unity並未定義GameObject之間的執行順序,一樣建議不要仰賴GameObject的Hierarchy來決定Components之間Awake的順序。
如果真的Components之間或GameObjects之間在Initial之間是有耦合狀態的,建議還是一個Master Script來管理,看是要動態新增GameObject或是動態新增Component,亦或是動態Enable GameObject或Component。
那什麼時候代表Component產生時的那瞬間:
- GameObject第一次Active的時候,就會呼叫所有Attached Components的Awake。
- 用程式呼叫AddComponent的時候
Start呼叫的時機點在於Component第一次Enable時的那瞬間。
那什麼時候代表Component Enable時的那瞬間:
- GameObject上有Implement Start或Update function這種的,在Inspector上就可以看到Script名稱旁有個Check Box,當GameObject第一次Active的時候且Check Box也是打"V"的狀態,就會呼叫Start
- 用程式呼叫Component.enabled = true時
Awake一定比Start還早,而且是場景上所有的Awake都結束後,才會呼叫Start。而預設各Component執行Start的順序等同各Component執行Awake的順序,除非在Awake動態改變Component的enable順序。
而Awake和Start最大的差別就在於一種情況:
當GameObject為Active,但是Component是Disabled的狀態,Awake會呼叫的但是Start是不會的。
GameObject上的Components彼此Awake的順序是?
理論上應該是新增Component的順序,但是基本上建議不要依賴這個順序,因為常常會忘記........雖然在Unity工具列的Edit->Project Settings->Script Execution Order來決定,但是一樣不方便。
GameObject之間的Components彼此Awake的順序是?
Unity並未定義GameObject之間的執行順序,一樣建議不要仰賴GameObject的Hierarchy來決定Components之間Awake的順序。
如果真的Components之間或GameObjects之間在Initial之間是有耦合狀態的,建議還是一個Master Script來管理,看是要動態新增GameObject或是動態新增Component,亦或是動態Enable GameObject或Component。
2014年10月15日 星期三
unity render material and shared material
Unity內的render.sharedMaterial,代表的是所有Unity內都使用這份,每次更換屬性時,如果這份是存在Asset內的話,就會發現這份也被修改過(用SVN就可檢查是否被修改)。
用Material xx = render.material,代表是untiy所複製的一份material,每次更換這material的屬性只會改到這份複製的。如果想要讓更改的資料回render,就要做render.material = xx
因為實在太危險了,所以特此記上!!!!!
用Material xx = render.material,代表是untiy所複製的一份material,每次更換這material的屬性只會改到這份複製的。如果想要讓更改的資料回render,就要做render.material = xx
因為實在太危險了,所以特此記上!!!!!
2014年6月4日 星期三
why grandson class should not call base.base.method?
主要問題是為什麼子類別不能直接呼叫base.base.method?
主要原因就是破壞物件封裝。假設今天有個collection-item的behavior class,有個child class是只能收集red item,而grand child class是只能收集big red item。如果今天grand child class不想透過parent檢查是否為red item,而直接呼叫grand parent add item,是不是有點奇怪?
public class Items
{
public void add(Item item) { ... }
}
public class RedItems extends Items
{
@Override
public void add(Item item)
{
if (!item.isRed())
{
throw new NotRedItemException();
}
super.add(item);
}
}
public class BigRedItems extends RedItems
{
@Override
public void add(Item item)
{
if (!item.isBig())
{
throw new NotBigItemException();
}
super.add(item);
}
}
public class NaughtyItems extends RedItems
{
@Override
public void add(Item item)
{
// I don't care if it's red or not. Take that, RedItems!
super.super.add(item);
}
}
訂閱:
文章 (Atom)