Unity Transform類測試

官方API使用文檔

1. 案例1

1.1. 準備

導入MerchentGirl.unitypackage包裏的模型,位置如圖所示。
在這裏插入圖片描述
若想要GameScene鏡頭效果保持一致,則可以先點擊Main Camera,再點擊GameObject下的Align With View
在這裏插入圖片描述
在這裏插入圖片描述
新建腳本TestTransform,如果需要更改腳本的默認編輯器,可以點擊Edit->Preferences…->Extenternal Tools->External Script Editor
在入圖片描述

1.2. 測試1

1.2.1. 前臺

在這裏插入圖片描述

1.2.2. 代碼

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestTransform : MonoBehaviour
{
    public Transform cube, player;
    // Start is called before the first frame update
    void Start()
    {
        player = cube.transform.Find("Merchant_female@basice");
        Debug.Log(player);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

1.2.3. 結果

在這裏插入圖片描述

1.3. 測試2

1.3.1. 前臺

較測試1,新增一個Sphere,父子關係如圖所示:
在這裏插入圖片描述

1.3.2. 代碼

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestTransform : MonoBehaviour
{
    public Transform cube, player;
    // Start is called before the first frame update
    void Start()
    {
        player = cube.transform.Find("Merchant_female@basice");//找不到對象
        Debug.Log(player);
        player = cube.transform.Find("Cube/Merchant_female@basice");//找不到對象
        Debug.Log(player);
        player = cube.transform.Find("Sphere/Merchant_female@basice");//能找到對象
        Debug.Log(player);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

1.3.3. 結果

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章