Unity C# (2) จัดการ GameObject

Game Development 16 พฤศจิกายน พ.ศ. 2566 351
Home / Articles / 67

สวัสดีครับ ในบทความนี้ ผมจะพามาแนะนำให้รู้จักกับ Properties ต่าง ๆ ของ GameObject เช่น Tag และ Layer ของ GameObject และการจัดการ GameObject เช่น ให้แสดงหรือซ่อน (SetActive) เป็นต้น

แสดงพิกัดของ GameObject ใน Scene

ตัวอย่าง Code ด้านล่างนี้อยู่ใน GameObject หนึ่ง โดยผมได้สร้างตัวแปรประเภท float ได้แก่ PosX, PosY และ PosZ ไว้สำหรับเก็บพิกัดของ GameObject จากตัวแปร Object01 ซึ่งเมื่อสร้างตัวแปรทั้ง 4 อันแล้ว จะเกิด Field ขึ้นมาใน Inspector เมื่อเอา GameObject ใดๆ ลากเข้าไปใส่ในช่อง Object01

เมื่อ Script ทำงาน ในทุก ๆ เฟรมจะเกิดการดึงพิกัด x,y,z จากตัวแปร Object01 ออกมา และเก็บไว้ใน PosX, PosY และ PosZ

using UnityEngine;
using System.Collections;

public class MyScript : MonoBehaviour {

    public GameObject Object01;
    public float PosX;
    public float PosY;
    public float PosZ;

    void Start () {
     
    }
	
    void Update () {
        PosX = Object01.transform.position.x;
        PosY = Object01.transform.position.y;
        PosZ = Object01.transform.position.z; 
    }
}

แสดง Tag และ Layer ของ GameObject

ตัวอย่าง Code ข้างล่าง เป็นการยกตัวอย่างการแสดงข้อมูล Tag และ Layer ID ลงใน Console ของ Unity ด้วยฟังก์ชั่น Debug.Log() หนึ่งครั้ง เมื่อเกมเริ่ม

using UnityEngine;
using System.Collections;

public class MyScript : MonoBehaviour {

    public GameObject Object01;

    void Start () {
        Debug.Log("Object Tag Value is: "+Object01.tag);
        Debug.Log("Object Layer ID is: "+Object01.layer); 
    }
	
    void Update () {
    
    }
}

แสดงหรือซ่อน GameObject

ในตัวอย่างข้างล่างนี้ เป็นการ แสดงและซ่อน GameObject ที่อยู่ในตัวแปร Object01 ทุกครั้งที่กดปุ่ม I บนคีย์บอร์ด เราจะแสดงหรือซ่อน GameObject ด้วยฟังก์ชั่น SetActive() ซึ่งควบคุมด้วยตัวแปล state ซึ่งเป็นประเภท boolean (true or false) มีค่าเริ่มต้นเป็น false

using UnityEngine;
using System.Collections;

public class MyScript : MonoBehaviour {

    public GameObject Object01;
    public bool state = false;

    void Start () {
        Object01.SetActive(state);    
    }
	
    void Update () {
        if(Input.GetKeyDown(KeyCode.I)) {
            if(state == false) {
                state = true;
            } else {
                state = false;
            }
            Object01.SetActive(state);
        }
    }
}

กำหนดตำแหน่งใหม่ของ GameObject

เราสามารถกำหนดตำแหน่งใหม่ของ GameObject ได้โดยการเปลี่ยนค่าใน transfrom.position ไปเป็น Class Vector3 ค่าใหม่

Vector3 คือ Class ที่เอาไว้เก็บค่าพิกัด (Condinate) แบบ x,y,z  โครงสร้างคือ Vector3(x,y,z)

เมื่อผมต้องการจะเปลี่ยนตำแหน่งของ Object01 ไปเป็นตำแหน่งใหม่ที่พิกัด x=100, y=10, z=125 ผมจะต้องเขียน ดังนี้

using UnityEngine;
using System.Collections;

public class MyScript : MonoBehaviour {

    public GameObject Object01;

    void Start () {
        Object01.transform.position = new Vector3(100,10,125);
    }
	
    void Update () {
    
    }
}

GetChild เพื่อเข้าถึง GameObject ลูก

ฟังกชั่น GetChild สามารถใส่ลำดับของ Child GameObeject ที่อยู่ใน Parent GameObject ได้ดังนี้ ตัวอย่าง Parent คือ Object01

using UnityEngine;
using System.Collections;

public class MyScript : MonoBehaviour {

    public GameObject Object01;

    void Start () {
        Object01_Child = Object01.transform.GetChild(0);
    }
	
    void Update () {
    
    }
}

หา Child GameObject จากชื่อ

ตัวอย่างในตัวแปร This_Object_Head คือการค้นหา GameObject ชื่อ "Head" ณ GameObject ปัจจุบันที่ Script ทำงานอยู่

using UnityEngine;
using System.Collections;

public class MyScript : MonoBehaviour {

    private GameObject This_Object_Head;

    void Start () {
        This_Object_Head = GameObject.Find("Head");
    }
	
    void Update () {
    
    }
}

อ้างอิง: 

docs.unity3d.com/ScriptReference/GameObject

discussions.unity.com/203918

unity3d.com/ScriptReference/Transform.GetChild

Profile Picture.
  • Name (Pen name): Sunny Jirakit (Sunny420x)
  • Study: Bachelor Degree of Computer Science from Chiang Mai Rajabhat University
  • Personality: Architect (INTJ-T)
  • Experience: JavaScript,  Angular.js, React.js, Next.js  Express.js, Unity C#, Socket.io