Unity-笔记.docx
《Unity-笔记.docx》由会员分享,可在线阅读,更多相关《Unity-笔记.docx(42页珍藏版)》请在冰点文库上搜索。
![Unity-笔记.docx](https://file1.bingdoc.com/fileroot1/2023-4/28/de7175ca-2925-4671-bd61-194e2ae347cc/de7175ca-2925-4671-bd61-194e2ae347cc1.gif)
Uniyt3D笔记
Unity笔记
向量的减法
unity旋转的方式:
1、欧拉角:
沿自身角度旋转的度数unity欧拉角其实是四元数
2、四元数:
(1,1,1,1)
Transform组件作用:
1、控制游戏对象的变换位置旋转缩放
2、维持父子关系
脚本组件:
(添加到游戏对象上以实现用户自定义的一些功能)
1、自动继承MonoBehaviour类
2、类名必须与脚本名一致
3、transform.parent; 获取/重新指定当前游戏对象父对象的transform组件
4、transform.root; 获取当前游戏对象的根父对象
5、transform.Find(”Cube“); 获取当前游戏对象叫做Cube的对象
6、transform.FindChild(“Cube”);获取当前游戏对象叫做Cube的对象,如果多个默认返回第一个
7、transform.Position:
世界坐标系中的位置
8、transform.locaPosition:
局部坐标系中的位置
9、transform.rotation; 世界坐标系中的位置四元数
10、transform.localRotation; 局部坐标系中的位置四元数
11、transform.localScale; x,y,z轴缩放比例操作
12、transform.Translate; 对象的位置移动操作
13、transform.Rotate; 对象的轴旋转操作
14、transform.eulerAngles=newVector3(0f,45f,0f); 欧拉角-属性
回调方法
UnityC#脚本默认方法继承MonoBehaviour被叫做运行时类,运行时不能手动实例化
publicclasshello:
MonoBehaviour //默认继承MonoBehaviour类
{
//Usethisforinitialization //在游戏开始的第一帧运行一次
voidStart()
{
}
//Updateiscalledonceperframe //每一帧调用一次
voidUpdate()
{
}
}
Unity脚本生命周期系统自带9个回调方法
这写方法都不是MonoBehaviour中定义的方法,而是通过反射调用的一些事件
一:
游戏开始的第一帧执行一次的方法:
Awake:
在游戏开始的第一帧执行一次,你的脚本组件无论是可用还是不可用,它都会执行,在Awake中做一些初始化操作
OnEnable:
当脚本组件激活的时候执行一次被调用
Start:
游戏开始第一帧执行一次,在第一次调用Update之前调用一次Start,做一些初始化操作,初始化public成员
二:
游戏开始后持续执行
FixedUpdate:
每0.02秒执行一次,固定时间间隔默认0.02秒调用,一般我们会把处理物理的代码放在这里
Update:
每帧执行一次,执行的次数取决于计算机的配置(用于写人物移动逻辑)
LateUpdate:
每帧执行一次,(用于写摄像机移动逻辑),在Update方法调用完之后调用
三:
OnGUI持续调用
OnGUI:
IMGUI-代码需要写在OnGUI方法中每帧执行两次,游戏开始到结束持续执行
四:
不可用的时候执行
OnDisable:
当脚本组件处于不可用状态的时候执行一次,取消激活状态后调用
OnDestroy:
当脚本组件被销毁时,执行一次
常用调式方法:
1、Print() 属于MonoBehaviour类的成员
2、DEbug.Log () 独立,密封的类常用调式方法
Ctrl+shift+N新建空物体
Debug.DrawLine()创建一条射线
//画一条射线
Debug.DrawRay(newVector3(0,0,0),newVector3(0,0,1),Color.red);
设置脚本执行顺序:
Edit-ProjectSettings-ScripeExecutionOrder
Unity自身也有一个object类
常用类
一、GameObject游戏对象类
01:
gameObjectthis.获取当前脚本所挂载的游戏对象,在属性面板中能够修改的组件都能通过脚本进行修改获取
1、gameObject.activeSelf:
游戏对象激活状态
2、gameObject:
表示当前脚本组件所挂载的游戏对象,返回true或false
3、gameObject.SetActive(false):
设置游戏对象的激活状态
4、gameObject.name; 表示游戏对象的名称
5、gameObject.tag; 表示游戏对象的标签
6、gameObject.layer; 表示游戏对象的层
7、GameObject.Find("MainCamera");通过游戏对象的名称进行查找,返回的是一个游戏对象
8、GameObject.FindWithTag("Player");通过游戏对象的标签查找游戏对象,返回一个游戏对象
9、GameObject.FindGameObjectsWithTag("Hot");通过游戏对象的标签查找多个游戏对象,返回一个游戏对象数组
10、gameObject.AddComponent<>(); 给游戏对象添加指定类型的组件
11、GameObject.Destroy(gameObject); 立刻销毁游戏对象
12、GameObject.Destroy(gameObject,5f); 延时销毁游戏对象float类型的参数
销毁当前脚本所挂载的游戏对象
获取组件的方法:
普通的成员方法
//1找到摄像机对象声明一个对象进行接收
GameObjectcamera=GameObject.FindWithTag("MainCamera");
//2找到摄像机身上的组件<>表示得到组件的类名称无参数,小括号
floatdp=camera.GetComponent().depth;
//打印结果
Debug.Log(dp);
//找到Cube输出位置坐标
Vector3pos=GameObject.Find("Cube").GetComponent().position;
Debug.Log(pos);
transform:
表示当前游戏组件所挂载的游戏对象身上的Transform组件
this.GetComponent();获取当前游戏对象身上的组件
对于父物体与子物体的坐标位置,子物体分别有相对于父物体的世界坐标点与自己的坐标点
//让物体产生移动
transform.position+=newVector3(0,0,0.01f); //每秒走0.01秒
transform.Translate(newVector3(0,0,0.01f));//与上一个一样的效果
//让物体产生旋转
transform.Rotate(newVector3(1,0,0));//让物体每帧延x轴转1度
transform.eulerAngles+=newVector3(0,0,2);//与上一个一样的效果
//绕着一个物体旋转
transform.RotateAround(newVector3(0,0,0),newVector3(0,1,0),50);
星球的自转与公转
publicclassStarRun:
MonoBehaviour
{
//星球的直径
publicfloatradius=0.5f;
//星球的材质颜色
publicColorcolor;
//星球自转的速度
publicfloatrotationSpeed=3f;
//星球的公转速度
publicfloatrotationAroundSpeed=3f;
//公转所绕行的星球
publicTransformaroundPoint;
voidStart()
{
//设置球体半径,半径为1的时候*半径*2等于直径
transform.localScale=Vector3.one*radius*2;
//星球的材质,设置球体的颜色
GetComponent().material.color=color;
}
voidUpdate()
{
Rotation();
AroundRotation();
}
///
///自转
///
voidRotation()
{
//设置星球的自转
transform.Rotate(Vector3.up*rotationSpeed);
}
///
///公转
///
voidAroundRotation()
{
//设置星球的公转,位置,方向,速度
transform.RotateAround(aroundPoint.position,Vector3.up,rotationAroundSpeed);
}
}
坦克发射子弹
1:
publicclassFireMoeBullet:
MonoBehaviour
{
//Bullet预设体
publicGameObjectbullet;
voidUpdate()
{
Fire();
}
//子弹Bullte发射的方法
voidFire()
{
if(Input.GetMouseButtonUp(0))//如果按下了鼠标左键
{
//通过预设体在炮筒方向生成一个子弹bullet,强制转换
GameObjectcurrentBullet=(GameObject)Instantiate(bullet,transform.GetChild(3).position,Quaternion.identity);
//给子弹一个方向
currentBullet.GetComponent().dir=transform.forward;
}
}
}
2:
publicclassMoveBullte:
MonoBehaviour
{
//bullet移动
publicfloatmoveSpeed=10f;
publicfloatdeadTime=3f;
publicVector3dir;
voidFixedUpdate()
{
//定时销毁当前子弹对象
Destroy(gameObject,deadTime);
}
voidUpdate()
{
//子弹移动
transform.position+=dir*moveSpeed*Time.deltaTime;
}
}
二、Input类获取用户事件时使用一般都写在Update方法中
0表示左键,1表示右键,2表示中键滚轮
【一Mouse方法鼠标】
1、GetMouseButtonDown(0)获取鼠标按下的事件
2、GetMouseButton
(1) 获取鼠标持续按下的事件
3、GetMouseButtonUp
(2)获取鼠标弹起的事件
4、fieldOfView=60 视野角度
【二GetKey方法键盘】
1、GetKey(KeyCode.W) 获取按键持续按下的事件
2、GetKeyDown(KeyCode.W)获取按键按下的事件
3、GetKeyUP(KeyCode.W) 获取按键弹起的事件
4、如果一个值类型常量为公有的,那么unity脚本面板比脚本文件拥有更大的优先级。
Input类简介与虚拟按键
Horizontal:
水平方向
Vertical:
垂直方向
Rotate旋转角度
RotateAround:
旋转轴
//Pet绕着Tank目标旋转
publicTransformtarget;//声明组件
//表示绕着父物体的轴向进行指定度数旋转
voidUpdate()
{
//X轴顺时针旋转【left左】
transform.RotateAround(target.position,Vector3.left,2);
//X轴逆时针旋转【right右】
transform.RotateAround(target.position,Vector3.right,2);
//Y轴顺时针旋转【up上】
transform.RotateAround(target.position,Vector3.up,2);
//Y轴逆时针旋转【down下】
transform.RotateAround(target.position,Vector3.down,2);
//Z轴顺时针旋转【back后】
transform.RotateAround(target.position,Vector3.back,2);
//Z轴逆时针旋转【forward前】
transform.RotateAround(target.position,Vector3.forward,2);
}
publicfloatmoveSpeed=2f;//定义移动速度变量
voidUpdate()
{
//前移动
//GetKey表示键盘事件
if(Input.GetKey(KeyCode.W))//W键按下
{
//1帧移动move1m
//transform.Translate(Vector3.forward);
//1s移动move1m默认,后面可以声明一个变量修改移动的速度
//加Space.World按着世界的坐标,全局坐标
transform.Translate(Vector3.forward*Time.deltaTime*moveSpeed);
}
//左旋转
if(Input.GetKey(KeyCode.A))
{
transform.Rotate(Vector3.up);
}
//右旋转
if(Input.GetKey(KeyCode.D))
{
transform.Rotate(Vector3.down);
}
//后移动
if(Input.GetKey(KeyCode.S))
{
transform.Translate(Vector3.back*Time.deltaTime*moveSpeed);
}
}
随机生成一个RGB颜色
publicclassChangeColor:
MonoBehaviour
{
voidUpdate()
{
if(Input.GetKey(KeyCode.Space))
{
//随机数,静态方法,随机生成colorRGB颜色
floatr=Random.Range(0f,1f);
floatg=Random.Range(0f,1f);
floatb=Random.Range(0f,1f);
//创建一个随机颜色
Colorcolor=newColor(r,g,b);
//设置网格渲染颜色
GetComponent().material.color=color;
}
}
}
画一条射线,利用Input键盘的输入
publicclassMoveBoxScript:
MonoBehaviour
{
floathou;
floatver;
//速度
publicfloatspeed=3f;
voidUpdate()
{
hou=Input.GetAxis("Horizontal");
ver=Input.GetAxis("Vertical");
Vector3dir=newVector3(hou,0,ver);
//将方向向量画出来
Debug.DrawRay(Vector3.zero,dir,Color.red);
//物体在输入的方向移动位置+=速度*时间
transform.position+=dir*speed*Time.deltaTime;
}
}
物体的移动键盘的输入
publicclassTankMoveScript:
MonoBehaviour
{
publicfloatmoveSpeed;//移动速度
publicfloatrotateSpeed;//旋转速度
voidUpdate()
{
floathor=Input.GetAxis("Horizontal");//获取水平轴值
floatver=Input.GetAxis("Vertical");//获取垂直轴值
//坦克向前移动向后倒退【根据方向+1与-1进行判断】hor未调用的时候为0
transform.position+=transform.forward*ver*Time.deltaTime*moveSpeed;
//坦克的左右转向【根据方向+1与-1进行判断】ver未调用的时候为0
transform.Rotate(newVector3(0,hor*Time.deltaTime*rotateSpeed,0));
}
}
InputMouse鼠标
三、Time类
Time.timeScale=0.5f;
print(Time.time);//实时输出游戏从运行到现在的一个时间
print(Time.unscaledDeltaTime);//不受时间缩放影响的时间
print(Time.fixedTime);//物理总时间
print(Time.fixedDeltaTime);//物理间隔的时间,为固定值
预设体:
准备要去生成的物体设成预设体
Instantiate:
实例化生成一个对象
查找物体的方法:
【transform.Find依据名字查找【子物体】当前物体的this.transform激活的与隐藏的都能找到到】
firePosition=transform.Find("name");
【GameObject依据名字查找激活的物体】
firePosition=GameObject.Find("name").transform;
AudioSource音频
Lerp插值:
从a到b需要1s秒a->b,需要1s
插值公式:
a+(b-a)*t(t->[0.1])
floatnum=Mathf.Lerp(1,6,0.5f);
print(num);//结果3.5
小于0默认为最小值1,大于最大值6默认最大值6
Vector3v3=Vector3.Lerp(Vector3.zero,Vector3.one,0.5f);
print(v3);//结果0.5,0.5,0.5,xyz
让一个物体移动到另一个物体,时间固定Lerp练习
publicclassCapsuleScript:
MonoBehaviour
{
privateTransformtransPos;//目标
privateVector3a;//起点
privateVector3b;//终点
publicfloattime;//起点到终点移动的时间
privatefloattimer;//定义一个计时器,用来计算起点到终点的总时间
voidStart()
{
transPos=GameObject.Find("Sphere
(1)").transform;//累加
a=transf