官方API使用文档
文章目录1. 案例11.1. 准备1.2. 测试11.2.1. 前台1.2.2. 代码1.2.3. 结果1.3. 测试21.3.1. 前台1.3.2. 代码1.3.3. 结果2. 案例22.1. 测试12.1.1. 前台2.1.2. 代码2.1.3. 结果2.2. 测试22.2.1. 前台2.2.2. 代码2.2.3. 结果2.3. 测试32.3.1. 前台2.3.2. 代码2.3.3. 结果3. 举一反三3.1. 前台3.2. 代码3.3. 结果 1. 案例1 1.1. 准备导入MerchentGirl.unitypackage
包里的模型,位置如图所示。
若想要Game与Scene镜头效果保持一致,则可以先点击Main Camera,再点击GameObject下的Align With View。
新建脚本TestTransform
,如果需要更改脚本的默认编辑器,可以点击Edit->Preferences…->Extenternal Tools->External Script Editor
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
,父子关系如图所示:
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. 结果
2. 案例2
2.1. 测试1
2.1.1. 前台
新建Cube和Test脚本,并将Test挂在Cube上。
using UnityEngine;
public class Test : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log(transform.localPosition);
Debug.Log(transform.localScale);
Debug.Log(transform.localEulerAngles);
}
// Update is called once per frame
void Update()
{
}
}
2.1.3. 结果
2.2. 测试2
2.2.1. 前台
调整Cube的transform参数,结果如图所示:
using UnityEngine;
public class Test : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log(transform.localPosition);
Debug.Log(transform.localScale);
Debug.Log(transform.localEulerAngles);
}
// Update is called once per frame
void Update()
{
}
}
2.2.3. 结果
2.3. 测试3
2.3.1. 前台
2.3.2. 代码
using UnityEngine;
public class Test : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log(transform.localPosition);
Debug.Log(transform.localScale);
Debug.Log(transform.localEulerAngles);
Debug.Log(transform.right);//1,0,0 x轴
Debug.Log(transform.up);//0,1,0 y轴
Debug.Log(transform.forward);//0,0,1 z轴
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.A))
{
transform.localPosition = new Vector3(7.7f, -4.26f, 0.9f);
}
}
}
2.3.3. 结果
3. 举一反三
Public Method | Description |
---|---|
GetChild | Returns a transform child by index. |
IsChildOf | Is this transform a child of parent? |
新建Cube、Sphere对象,Test脚本,将Test挂载到Main Camera上,然后把Cube、Sphere与Test中的变量进行绑定。
using UnityEngine;
public class test : MonoBehaviour
{
public Transform cube, sphere;
// Start is called before the first frame update
void Start()
{
//GetChild Returns a transform child by index.
Debug.Log(cube.transform.GetChild(0));
//IsChildOf Is this transform a child of parent?
Debug.Log(sphere.transform.IsChildOf(cube.transform));
}
// Update is called once per frame
void Update()
{
}
}
3.3. 结果