เคลีย Grid อัตโนมัติ ก่อนหรือหลัง Grid ถูก Enable หรือ Disable by

28
Feb
0

บางครั้งเราก็จำเป็นที่จะต้อง ล้าง Grid ใหม่ทุกครั้งที่ Show หน้าที่มี Grid นี้ หรือ ล้างทุกครั้งที่เราละจาก Grid นี้ไป ไม่ว่าจะเหตุผลเรื่องของ Memory หรืออื่นๆก็ตาม วันนี้เรามีวีธีง่ายๆที่จะทำให้ชีวิตดีขึ้นมากก มาฝากครั้บบ

1. เราต้องแก้ไขไฟล์ UIGrid ของ NGUI ก่อนครับให้เราเพิ่ม code นี้ไปใน UIGrid ครับ
public bool clearGridOnEnable = false;
public bool clearGridOnDisable = false;

void OnEnable() {
if (clearGridOnEnable)
{
for (int i = 0; i < grid.transform.childCount; i++) {
Destroy (grid.transform.GetChild (i).gameObject);
}

if (detachChildren) {
grid.transform.DetachChildren ();
}

Resources.UnloadUnusedAssets ();
System.GC.Collect ();
}
}

void OnDisable() {
if (clearGridOnDisable)
{
for (int i = 0; i < transform.childCount; i++) {
Destroy (transform.GetChild (i).gameObject);
}

if (detachChildren) {
transform.DetachChildren ();
}

Resources.UnloadUnusedAssets ();
System.GC.Collect ();
}
}

2. ถ้าทำแค่นี้ ก็ยังไม่ทำให้ชีวิตเราง่ายขึ้นเท่าไหร่ครับ เพราะว่ามันจะไม่มี clearGridOnEnable หรือ clearGridOnDisable ขึ้นมาใน Inspector ให้เราตั้งค่าครับ ก็ให้เราเพิ่ม code นี้ในไฟล์ UIGridEditor ครับ
NGUIEditorTools.DrawProperty("Clear on Enable", serializedObject, "clearGridOnEnable");
NGUIEditorTools.DrawProperty("Clear on Disable", serializedObject, "clearGridOnDisable");

ให้เราเพิ่ม code ไปก่อนบรรทัดนี้นะครับ
serializedObject.ApplyModifiedProperties();

3. ต่อไปจากนี้ เราก็จะมี การตั้งค่าใน Inspector เพิ่มมาให้เราตั้งค่าครับ หน้าตาแบบนี้ครับ

Grid

หากเราต้องการให้ Grid ล้างค่า ของ ใน Grid ทุกครั้งเราก็แค่ ติ๊กถูกใน Inspector เท่านั้นเองครับ

Enjoy this article?

Consider subscribing to our RSS feed!

ไม่มีความเห็น

ยังไม่มีความเห็น

ใส่ความเห็น

RSS feed for comments on this post

 เราชนะรอบ 4 | ยืมเงิน 3000 ด่วน | แอพกู้เงิน | แอพเงินด่วน | สินเชื่อออนไลน์อนุมัติทันที | Site Map | กู้เงินก้อน | กระเป๋าตัง | thisshop และ ยืมเงินฉุกเฉิน 5000 ด่วน