unity
场景视图 游戏视图 层级视图 资源视图
x为右 y为上 z为前方
扳手的标志是一个属性
立方体的标志是方法
1.脚本语言
using 引用命名空间,引用地址
类名和脚本名字必须一致
Public class Abc :MonoBe
公开的类 继承了Mono
void Start(){}
生命周期
开始执行(只执行一次)
void Update(){}
每帧执行(每帧执行),1秒大概执行50次 0.02s
1、 脚本执行 需要依赖对象
自己写的脚本跟组件一个级别
组件 :就是添加一个功能
remove compotent 移除脚本
如何找到对象
2、获取对象
public GameObject cube;
cube.name = "孙悟空";
立方体 有 Mesh renderer(组件) – Material
MeshRenderer mesh(名字) = cube.GetComponent<想获取的组件>()
mesh.material.color = Color.red;
相当于获取到了一个MeshRenderer类型的对象
小技巧:组件就是类,类就是组件
①公开拖拽游戏对象
public GameOjbect obj;
②Find查找游戏对象
cube = GameObject.Find("Cube");
cube.SetActive(false); 如何禁用一个对象
③通过标签查找对象
GameObject.FindGameObjectsWithTag() 查找一类的,返回一个数组
GameObject.FindGameObjectWithTag() 返回一个对象
实例化对象(创建、克隆)
首先要有一个被创建的对象(母体)
public GameObject obj;(母体)
private GameObject objTemp;(生成克隆的新对象)
objTemp = GameObject.Instantiate(obj) as GameObject;
objTemp.GetComponent<Transform>().position = new Vector3(x,y,z);
Vector3结构 三维向量
(j+i,i,k+i)
当把一个脚本(组件)放在一个物体上的时候,会默认生成一个该脚本类型的对象.
所有的对象都是GamaObject,所有的物体都有Transform
public Transform tra;
public GameObject game;
this指的是当前脚本所在的对象
this指的是把脚本放在一个物体上,这个物体用的就是this
Tramsform(变换组件)
作为一个所有对象都有的组件,只需用(.)出来就行
通过位置找到对象,也可以通过对象找到位置
this.transform.gameObject.transform.gameObject
GameObject(类) 和 gameobject(对象)
TransForm(类) 和transform(对象)
sphere.gameObject
this.transform.position = new Vector3()
this.transform.eulerAngles = new Vector(0,35,0) 给物体一个角度,按y轴旋转35
欧拉角和四元数
欧拉角三个参数,能直接看出来物体的角度,万向节锁死
四元数四个参数,都可以改变物体的角度,但是四元数不直观,但是四元数可以避免死锁问题
this.transform.rotation = Quaternion.Euler(new Vector3(0,35,0));
this.transform.eulerAngles += new Vector3(0,1,0)
this.transform.rotation *= Quaternion.Euler(new Vector(0,35,0))
四元数没有+= 只有*= ,他跟欧拉角的+=是一样的
移动物体
this.transform.Translate(new Vector(0,1,0)) 移动物体的方法
this.transform.Rotate(new Vector3(0,1,0)) 旋转物体的方法
this.transform.RotateAround(sphere.transform.positon,new Vector(0,1,0),3) 按照一个点旋转
this.transform.Translate(Vector3.right*0.01f)朝x轴的正方移动 newVector3(1,0,0);
this.transform.Translate(Vector3.right*0.01f,space world)
朝着世界的x轴正方移动
this.transform.Translate(transform.forward);
transform.forward 当前物体的正前方,不规则的,相对于世界的旋转角度
if(Vector3.Distance(sphere.position,this.transform.position)<1);
private float timer
定义计时器对象
Time.deltaTime 每帧执行花费的时间
private GameObject obj
Destroy(obj,10);
10秒之后删除对象
获取文本对象
using UnityEngine.UI;
引用UI地址
string timerTemp = string.Format("{0:d2},((int)timer)")
tex.GetComponent<Text>().text=((int)timer).Tostring
private void FixedUpdate()
{
固定更新 处理物理系统
}