Site icon บริษัท โค๊ดบี จำกัด

สอน Unity3D C# ตอน 5 จัดการกับ Object ใน Scene ด้วยโค๊ด

<h1>สอน Unity3D C&num; ตอน 5 จัดการกับ Object ใน Scene ด้วยโค๊ด<&sol;h1>&NewLine;<p><&sol;br><br &sol;>&NewLine;<strong>สอน Unity3D C&num; ตอน 5 จัดการกับ Object ใน Scene ด้วยโค๊ด<&sol;strong><br &sol;>&NewLine;ในโปรแกรม Unity3D นั้น การจัดการกับ Object หรือ UI Element ทั้งหมดใน Scene รวมไปถึง UI Canvas ทำได้ง่ายมากครับ<br &sol;>&NewLine;<&sol;br><br &sol;>&NewLine;เพียงแค่เราประกาศตัวแปรแบบ Public ตัวแปรนั้น ๆ จะกลายเป็น element หนึ่งในช่อง inspector เราสามารถลากรูป ปุ่ม เสียง หรือพิมพ์ข้อความ เพื่อกำหนดค่าให้กับตัวแปรนั้น ๆ ได้เลย และในไฟล์ &period;cs เราก็จะสามารถจัดการกับวัตถุนั้น ๆ ได้ผ่านตัวแปรแบบ public ที่ประกาศไว้นั่นแหล่ะครับ<&excl;--more--><&sol;p>&NewLine;<p><&sol;br><&sol;p>&NewLine;<h2>กำหนดค่าให้กับตัวแปรแบบ public โดยการลากวาง<&sol;h2>&NewLine;<p>1&period; ให้สร้าง UI Button&comma; UI Panel&comma; UI Image ขึ้นมา อย่างละ 1 อย่าง ตั้งชื่อตามในรูปด้านล่าง<br &sol;>&NewLine;2&period; สร้าง Empty Object ตั้งชื่อว่า AppController และสร้างไฟล์ C&num; script ตั้งชื่อว่า AppController&period;cs ลากไฟล์มาใส่ไว้ใน AppController Object<br &sol;>&NewLine;<&sol;br><&sol;p>&NewLine;<figure>&NewLine;<img src&equals;"https&colon;&sol;&sol;www&period;codebee&period;co&period;th&sol;labs&sol;wp-content&sol;uploads&sol;2017&sol;02&sol;สอน-Unity3D-1&period;png" alt&equals;"สอน-Unity3D" &sol;><br &sol;>&NewLine;<&sol;figure>&NewLine;<p><&sol;br><br &sol;>&NewLine;3&period; เปิดไฟล์ AppController&period;cs ในโปรแกรม MonoDevelop จากนั้นให้ประกาศตัวแปร 3 ตัว ตามชื่อของ Panel&comma; Button และ Image ที่เราได้สร้างไว้ในข้อแรก<&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " > &NewLine;using UnityEngine&semi; &NewLine;using System&period;Collections&semi; &NewLine;using UnityEngine&period;UI&semi; &sol;&sol; เพื่อให้ใช้งาน UI Component ของ Unity ได้ &NewLine; &NewLine;public class AppController &colon; MonoBehaviour &lbrace; &NewLine; &NewLine;&Tab;public GameObject panelBackground&semi; &NewLine;&Tab;public Button btnStart&semi; &NewLine;&Tab;public Image imageLoad&semi; &NewLine; &NewLine;&Tab;void Start &lpar;&rpar; &lbrace; &NewLine;&Tab;&Tab; &NewLine;&Tab;&rcub; &NewLine;&Tab;void Update &lpar;&rpar; &lbrace; &NewLine;&Tab;&Tab; &NewLine;&Tab;&rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>จากโค๊ดด้านบนจะมีข้อสังเกตุอยู่เล็กน้อยคือ Panel จะไม่มี UI Class เป็นของตัวเอง แต่จะอยู่ในคลาส GameObject แต่ในขณะที่ Button และ Image จะมี UI Class เป็นของตัวเอง ซึ่งอยู่ใน package คือ UnityEngine&period;UI&period;<&sol;p>&NewLine;<p>4&period; จากนั้นให้เปลี่ยนไปที่ workspace ของ unity เลือกไปที่ AppController Object เราจะเห็นว่ามี element เพิ่มขึ้นมา 3 ช่อง ตามตัวแปรที่เราได้ประกาศไว้ในไฟล์ AppController&period;cs<br &sol;>&NewLine;<&sol;br><&sol;p>&NewLine;<figure>&NewLine;<img src&equals;"https&colon;&sol;&sol;www&period;codebee&period;co&period;th&sol;labs&sol;wp-content&sol;uploads&sol;2017&sol;02&sol;สอน-Unity3D-2&period;png" alt&equals;"สอน-Unity3D" &sol;><br &sol;>&NewLine;<&sol;figure>&NewLine;<p><&sol;br><br &sol;>&NewLine;5&period; จากนั้นให้เริ่มจับคู่ตัวแปรและ object โดยให้ลาก object มาวางไว้ในช่อง ที่มีชื่อเดียวกันหรือ คลิกที่ไอคอนวงกลมหลังช่องแล้วเลือก object ที่มีชื่อตรงกันที่เราต้องการกำหนดค่าให้<&sol;p>&NewLine;<p><strong>จับคู่ให้ตัวแปร กับ Object ใน scene ด้วยการลากวาง<&sol;strong><&sol;p>&NewLine;<figure>&NewLine;<img src&equals;"https&colon;&sol;&sol;www&period;codebee&period;co&period;th&sol;labs&sol;wp-content&sol;uploads&sol;2017&sol;02&sol;สอน-Unity3D-3&period;gif" alt&equals;"สอน-Unity3D" &sol;><br &sol;>&NewLine;<&sol;figure>&NewLine;

Exit mobile version