一.Component
Component类提供了查找(在当前物体、后代、先辈)组件的功能
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ComponentDemo : MonoBehaviour
{
private void OnGUI()
{
if (GUILayout.Button("将当前物体颜色设置为红"))
{
GetComponent<MeshRenderer>().material.color = Color.red;
}
if (GUILayout.Button("获取当前物体所有组件"))
{
var allComponent = GetComponents<Component>();
foreach (var item in allComponent)
{
Debug.Log(item);
}
}
if (GUILayout.Button("获取所有子物体中的MeshRenderer"))
{
var allRenderer = GetComponentsInChildren<MeshRenderer>();
foreach (var item in allRenderer)
{
item.material.color = Color.red;
}
}
if (GUILayout.Button("获取所有先辈物体中的MeshRenderer"))
{
var allRenderer = GetComponentsInParent<MeshRenderer>();
foreach (var item in allRenderer)
{
item.material.color = Color.red;
}
}
}
/*
GetComponent:获取当前物体其他组件类型的引用。
GetComponents:获取当前物体所有组件引用。
GetComponentsInChildren:查找指定类型组件(从自身开始,并搜索所有后代)
GetComponentInChildren:查找指定类型组件(从自身开始,并搜索所有后代,查找到第一个满足条件则结束)
GetComponentsInParent:查找指定类型组件(从自身开始,并搜索所有先辈)
*/
}
二.Transform
Transform 类 提供了查找(根据名字、索引获取子物体)、移动、旋转、缩放物体的功能。
using UnityEngine;
using System.Collections;
/// <summary>
/// Transform 类 提供了查找(根据名字、索引获取子物体)、移动、旋转、缩放物体的功能。
/// </summary>
public class TransformDemo : MonoBehaviour
{
private void OnGUI()
{
if (GUILayout.Button("Find"))
{
//通过名称获取子物体变换组件引用
//this.transform.Find("子物体名称")
//备注:如果通过路径获取物体,那么路径一定不能发生改变
Transform childTF = this.transform.Find("子物体名称/子物体名称");
//通过变换组件查找其他类型组件
childTF.GetComponent<MeshRenderer>().material.color = Color.red;
}
if (GUILayout.Button("获取所有子物体(孙子不要)"))
{
//写法1
int count = transform.childCount;
for (int i = 0; i < count; i++)
{
//根据索引获取子物体
Transform tf = transform.GetChild(i);
}
//写法2
foreach (var child in transform)
{
//child子物体变换组件
}
}
if (GUILayout.RepeatButton("自转"))
{
//按下按钮 沿Y轴旋转1度
transform.Rotate(0, 1, 0);//沿自身坐标轴
//transform.Rotate(0, 1, 0,Space.World);//沿世界身坐标轴
}
if (GUILayout.RepeatButton("移动"))
{
//移动
transform.Translate(0, 0, 1);//沿自身坐标轴
//transform.Translate(0, 1, 0,Space.World);//沿世界身坐标轴
}
if (GUILayout.Button("LookAt"))
{
//注视旋转 物体Z轴指向目标位置
transform.LookAt(targetTF);
}
if (GUILayout.RepeatButton("围绕旋转"))
{
// (围绕的目标点,围绕的轴向,围绕的角度)
transform.RotateAround(targetTF.position, Vector3.forward, 1);
}
if (GUILayout.RepeatButton("设置父物体"))
{
//将当前物体 设置为 targetTF 的子物体
transform.SetParent(targetTF);
}
}
public Transform targetTF;
/*
Transform 类
-- Variables
childCount
localPosition:子物体相对于父物体坐标[编译器中显示的]
localScale:子物体相对于父物体的缩放比例[编译器中显示的]
lossyScale:(只读)可以理解为相对于模型的缩放比(父物体.localScale * 自身物体.localScale)
parent
position:相对于世界坐标系坐标
root
-- Public Functions
Find :通过名称查找子物体.
GetChild:根据索引获取子物体
LookAt
Rotate
RotateAround
SetParent
Translate
*/
}
三.GameObject
Unity 场景中所有实体的基类。
变量
- activeInHierarchy 理解为物体实际激活状态(物体自身被禁用或者父物体被禁用都为false)
- activeSelf 物体在Inspector面板激活状态
- layer 该游戏对象所在的层。
公共函数
- AddComponent:为游戏对象添加组件
- SetActive 根据给定的值 true 或 /false/,激活/停用 GameObject。
静态函数
- Find :根据名称查找游戏对象(慎用,如同大海捞针)
- FindGameObjectsWithTag:获取使用指定标签的所有物体
- FindWithTag:获取使用标签的一个物体
四.Object
Unity 可以引用的所有对象的基类。
您从 Object 派生的任何公共变量都将在 Inspector 中显示为放置目标, 让您能够从 GUI 设置其值。UnityEngine.Object 是所有 Unity 内置对象的基类
静态函数
- destroy 删除 GameObject、组件或资源。
- DontDestroyOnLoad 在加载新的 Scene 时,请勿销毁 Object。
- FindObjectOfType 返回第一个类型为 type 的已加载的激活对象。
- FindObjectsOfType 返回所有类型为 type 的已加载的激活对象的列表。
- Instantiate 克隆 original 对象并返回克隆对象。
五.Time
从 Unity 获取时间信息的接口。
静态变量
- time 该帧开始的时间(只读)。此为自游戏启动以来的时间(以秒为单位)。
- deltaTime 完成上一帧所用的时间(以秒为单位)(只读)。
- timeScale 时间缩放
- unscaledDeltaTime 不受缩放影响的每帧经过时间
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TimeDemo : MonoBehaviour
{
public float a, b, c;
//渲染场景时执行 不受TimeScale影响
void Update()
{
a = Time.time; //受缩放影响的游戏运行时间
b = Time.unscaledDeltaTime; // 不受缩放影响的游戏运行时间
c = Time.realtimeSinceStartup; //实际游戏运行时间
//每渲染帧 执行1次 旋转1度、
//1秒旋转 ?度
//帧多 1秒旋转速度快 希望1帧旋转量小
// 少 慢 希望 大
//this.transform.Rotate(0, 1, 0);
//所以为了转速恒定
//当旋转/移动 速度*每帧消耗事件,可以保证旋转/移动 速度不受渲染影响
this.transform.Rotate(0, 10 * Time.deltaTime, 0);
//游戏暂停,个别物体不受影响
this.transform.Rotate(0, 10 * Time.unscaledDeltaTime, 0);;
}
//固定0.02秒执行一次,与渲染无关,受TimeScale影响
public void FixedUpdate()
{
//this.transform.Rotate(0, 1, 0);
}
//游戏暂停
private void OnGUI()
{
if (GUILayout.Button("暂停游戏"))
Time.timeScale = 0;
if (GUILayout.Button("继续游戏"))
Time.timeScale = 1;
}
}
练习
做一个2分钟的倒计时,剩余10秒后文字变红色
using UnityEngine;
using UnityEngine.UI;
public class CountDownTimer : MonoBehaviour
{
public int Second = 120;
private Text text;
public float timer;
private float nextTime = 1;
// Start is called before the first frame update
void Start()
{
text = GetComponent<Text>();
//重复调用(被执行的方法名称,第一次执行的时间,每次执行间隔)
InvokeRepeating("Timer", 1,1);
//Invoke(被执行的方法,开始调用的时间);
}
// Update is called once per frame
void Update()
{
//Timer1();
//Timer2();
}
//方法3:InvokeRepeating("Timer", 1,1);
//每隔固定时间重复执行
private void Timer()
{
if (Second > 0)
{
Second--;
text.text = string.Format("{0:d2}:{1:d2}", Second / 60, Second % 60);
if (Second <= 10)
text.color = Color.red;
}
else
{
CancelInvoke("Timer");
}
}
//方法1:Time.time
private void Timer1()
{
//如果到了修改时间
if (Time.time >= nextTime)
{
nextTime = Time.time + 1;
if (Second > 0)
{
Second--;
text.text = string.Format("{0:d2}:{1:d2}", Second / 60, Second % 60);
if (Second <= 10)
text.color = Color.red;
}
}
}
//方法2:Time.deltaTime
private void Timer2()
{
//累加每帧间隔
timer += Time.deltaTime;
if (timer >= 1)
{
timer = 0;
if (Second > 0)
{
Second--;
text.text = string.Format("{0:d2}:{1:d2}", Second / 60, Second % 60);
if (Second <= 10)
text.color = Color.red;
}
}
}
}
六.总结
如何查找某个组件引用
Component 类
— 实例方法
— 查找当前物体其他类型组件引用
— 在先辈物体中查找指定类型组件引用
— 后代
Transform 类
— 实例方法
— 通过名称查找子物体
— 索引
GameObject 类
— 静态方法
— 通过标签查找物体(单个、所有)
— 通过名称找物体(慎用)
— 实例方法
— 查找当前物体其他类型组件
— 在先辈物体中查找指定类型组件引用
— 后代
Object 类
— 静态方法
— 根据类型查找对象