文章目录
- 1. 案例1
- 1.1. 准备
- 1.2. 测试1
- 1.2.1. 前台
- 1.2.2. 代码
- 1.2.3. 结果
- 1.3. 测试2
- 1.3.1. 前台
- 1.3.2. 代码
- 1.3.3. 结果
- 2. 案例2
- 2.1. 测试1
- 2.1.1. 前台
- 2.1.2. 代码
- 2.1.3. 结果
- 2.2. 测试2
- 2.2.1. 前台
- 2.2.2. 代码
- 2.2.3. 结果
- 2.3. 测试3
- 2.3.1. 前台
- 2.3.2. 代码
- 2.3.3. 结果
- 3. 举一反三
- 3.1. 前台
- 3.2. 代码
- 3.3. 结果
- 4. Translate函数
- 4.1. 准备
- 4.1.1. 创建Material
- 4.1.2. 改变对象的Material
- 4.2. 测试1
- 4.2.1. 前台
- 4.2.2. 代码
- 4.2.3. 结果
- 4.3. 测试2
- 4.3.1. 前台
- 4.3.2. 代码
- 4.3.3. 结果
- 5. rotate
- 5.1. 前台
- 5.2. 代码
- 5.3. 结果
- 6. rotateAround
- 6.1. 前台
- 6.2. 代码
- 6.3. 结果
- 7. 父子关系案例
- 7.1. 前台
- 7.2. 代码
- 7.3. 结果
- 8. 作业(钟表)
- 8.1. 素材
- 8.2. 前台
- 8.3. 代码
- 8.4. 结果
1. 案例1
1.1. 准备
导入MerchentGirl.unitypackage
包里的模型,位置如图所示。
若想要Game与Scene镜头效果保持一致,则可以先点击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. 结果
2. 案例2
2.1. 测试1
2.1.1. 前台
新建Cube和Test脚本,并将Test挂在Cube上。
2.1.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);
}
// Update is called once per frame
void Update()
{
}
}
2.1.3. 结果
2.2. 测试2
2.2.1. 前台
调整Cube的transform参数,结果如图所示:
2.2.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);
}
// 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? |
3.1. 前台
新建Cube、Sphere对象,Test脚本,将Test挂载到Main Camera上,然后把Cube、Sphere与Test中的变量进行绑定。
3.2. 代码
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. 结果
4. Translate函数
4.1. 准备
4.1.1. 创建Material
创建Material
调整颜色
4.1.2. 改变对象的Material
单击对象,再单击Element 0对应的小圆圈
通过上下滑动鼠标滚轮浏览所有的Material,然后点击选中所需要的Material
4.2. 测试1
4.2.1. 前台
4.2.2. 代码
using UnityEngine;
public class TestTranslate : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//每秒前进一次
transform.Translate(Vector3.forward * Time.deltaTime);
}
}
4.2.3. 结果
会看到对象朝Z轴移动
4.3. 测试2
4.3.1. 前台
4.3.2. 代码
using UnityEngine;
public class TestTranslate : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//transform.Translate(Vector3.forward * Time.deltaTime, Space.World); //世界坐标下的坐标
//transform.Translate(Vector3.forward * Time.deltaTime, Space.Self); //本地坐标下的坐标
transform.Translate(transform.forward * Time.deltaTime, Space.World); //世界坐标下的坐标
//transform.Translate(transform.forward * Time.deltaTime, Space.Self); //本地坐标下的坐标
}
}
4.3.3. 结果
世界坐标下的与本地坐标下的移动Vector3.forward或transform.forward,会有所不同。
5. rotate
5.1. 前台
将脚本同时挂载到图中的两个对象上
5.2. 代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestRotate : MonoBehaviour
{
public float speed = 100f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Rotate(Vector3.right * speed * Time.deltaTime, Space.World);
}
}
5.3. 结果
会看到两个对象都在旋转
6. rotateAround
6.1. 前台
6.2. 代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class rotateAround : MonoBehaviour
{
// Start is called before the first frame update
GameObject cube, player;
void Start()
{
cube = GameObject.Find("Cube");
player = GameObject.Find("Merchant_female@basice");
}
// Update is called once per frame
void Update()
{
player.transform.RotateAround(cube.transform.position, Vector3.up, 10);
}
}
6.3. 结果
会看到人物围绕着四方体旋转。
7. 父子关系案例
7.1. 前台
7.2. 代码
本次使用中文编程,即变量名、类名的命名使用中文命名。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class 父子关系 : MonoBehaviour
{
// Start is called before the first frame update
GameObject 立方体, 胶囊, 圆柱;
void Start()
{
立方体 = GameObject.Find("Cube");
圆柱 = GameObject.Find("Cylinder");
Debug.Log(圆柱.transform.parent);
Debug.Log(圆柱.transform.parent.parent);
Debug.Log(圆柱.transform.root);
Debug.Log(立方体.transform.childCount);
//解除与立方体的父子关系
//立方体.transform.DetachChildren();
//Debug.Log(立方体.transform.childCount);
DetachAllChildren(立方体.transform);
}
// Update is called once per frame
void Update()
{
}
void DetachAllChildren(Transform transform)
{
if (transform.childCount == 0)
{
return;
}
for (int i = 0; i < transform.childCount; i++)
{
DetachAllChildren(transform.GetChild(i));
}
transform.DetachChildren();
}
}
7.3. 结果
8. 作业(钟表)
8.1. 素材
- Unity添加图片到场景中
- 表框
- 分针
- 秒针
8.2. 前台
8.3. 代码
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Clock : MonoBehaviour
{
GameObject bg, s, m, h;
// Start is called before the first frame update
void Start()
{
bg = GameObject.Find("表框");
s = GameObject.Find("秒针");
m = GameObject.Find("分针");
h = GameObject.Find("时针");
s.transform.RotateAround(bg.transform.position, Vector3.forward, DateTime.Now.Second * 6f);
m.transform.RotateAround(bg.transform.position, Vector3.forward, (DateTime.Now.Minute + DateTime.Now.Second / 60f) * 6f);
h.transform.RotateAround(bg.transform.position, Vector3.forward, (DateTime.Now.Hour + DateTime.Now.Minute / 60f) * 30f);
}
// Update is called once per frame
void Update()
{
s.transform.RotateAround(bg.transform.position, Vector3.forward, Time.deltaTime * 6);
m.transform.RotateAround(bg.transform.position, Vector3.forward, Time.deltaTime * 0.1f);
h.transform.RotateAround(bg.transform.position, Vector3.forward, Time.deltaTime * 0.05f/6);
}
}
8.4. 结果