Unity学习笔记

脚本代码运行顺序

Unity编辑器中的脚本执行顺序是Awake——>OnEnable——>Start——>FixedUpdate——>yield WaitForFixedUpdata——>OnTrigger——>OnCollision——>OnMouse**(输入事件)——>Update——>yield WaitForSecone——>StartCoroutine——>LateUpdate(游戏逻辑)——>屏幕渲染——>OnGui(GUI渲染)——>OnDisable——>OnDstory。

但是不同脚本之间的Awake、OnEable和Start等函数的执行是有规律的:

1.最先被添加到GameObject上的脚本中的Awake、OnEable是最后被调用的,反之最后被添加的则会最先被调用。

2.当每个脚本的Awake、OnEable都被调用完了之后,才会调用各个脚本的Start函数。

向量

既有大小,也有方向。

向量的模

舍去向量的方向,只求向量的大小。

单位向量

大小为1的向量。

单位化,归一化

把向量转为单位向量的过程。

点乘

A·B=x1x2+y1y2=a·b·cosθ

通过该公式可快速得到AB向量之间的夹角,将AB向量单位化,那么x1x2+y1y2=cosθ

代码实现

  void Start()
{
//向量,坐标,旋转,缩放
Vector3 v = new Vector3(1,1,1);
v = Vector3.zero;
v = Vector3.one;
v = Vector3.forward;//(0,0,1)
v = Vector3.right;//(1,0,0)
Vector3 v2 = Vector3.forward;
//计算两个向量的夹角
Debug.Log(Vector3.Angle(v, v2));
//计算两点之间的距离
Debug.Log(Vector3.Distance(v, v2));
//点乘
Debug.Log(Vector3.Dot(v, v2));
//叉乘
Debug.Log(Vector3.Cross(v, v2));
//插值
Debug.Log(Vector3.Lerp(Vector3.zero, Vector3.one, 0.5f));
//向量的模
Debug.Log(v.magnitude);
//规范化向量
Debug.Log(v.normalized);
}

欧拉角与四元数

 void Start()
{
//旋转:欧拉角,四元数
Vector3 rotate = new Vector3(0, 30, 0);
//无旋转四元数
Quaternion quaternion = Quaternion.identity;
//将rotate的欧拉角传给四元数
quaternion = Quaternion.Euler(rotate);
//四元数转为欧拉角
rotate = quaternion.eulerAngles;
//看向一个物体
quaternion = Quaternion.Euler(new Vector3(0,0,0));
}

Debug使用

  void Start()
{
Debug.Log("text");
//警告显示
Debug.LogWarning("test");
//错误显示
Debug.LogError("text3");
}
 // Update is called once per frame
void Update()
{
//绘制一条线,第一个参数为起点,第二个为终点
Debug.DrawLine(Vector3.zero, Vector3.one,Color.red);
//绘制一条射线,第一个参数为起点,第二个为射线方向
Debug.DrawRay(Vector3.zero, Vector3.up, Color.blue);
}
}

动态修改物体属性

 
public class Empty : MonoBehaviour
{
//与物体进行关联
public GameObject Cube;
void Start()
{
//拿到当前脚本所挂载的游戏物体
GameObject go = this.gameObject;
Debug.Log(gameObject.name);
//打印图层layer
Debug.Log(gameObject.layer);
//打印立方体名称
Debug.Log(Cube.name);
//当前真正的激活状态
Debug.Log(Cube.activeInHierarchy);
//当前自身的激活状态
Debug.Log(Cube.activeSelf);
 //获取Transform组件
Debug.Log(transform.position);
//获取其他组件(固定写法)
BoxCollider bc = GetComponent<BoxCollider>();
 //获取当前物体的子物体身上的某个组件
GetComponentInChildren<CapsuleCollider>(bc);
//获取当前物体的父物体身上的某个组件
GetComponentInParent<BoxCollider>();
//添加一个组件
gameObject.AddComponent<AudioSource>();
//通过游戏物体名称获取游戏物体
GameObject text = GameObject.Find("text");
Debug.Log(text.name);
//通过游戏标签来获取游戏物体
text = GameObject.FindWithTag("Finish");
Debug.Log(text.name);
//更改物体激活状态
text.SetActive(false);
}

获取预设体

 public class Empty : MonoBehaviour
{
//获取预设体
public GameObject Prefab;
}
void Start()
{
//通过预设体实例化游戏物体
Instantiate(Prefab);
}

 

 

如果这篇文章对您有所帮助,不妨打赏一下作者吧~
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇