文章目录
视频教程
官方视频教程(中文字幕)请戳。下文是对其中几个章节的一些总结,方便自己回顾学习~
1.作为行为组件的脚本
C # C\# C#脚本应该被视作 u n i t y unity unity中的行为组件,它与其它的组件一样,可以依附在 G a m e O b j e c t GameObject GameObject上并显示在 I n s p e c t o r Inspector Inspector视图中。那么既然我们可以通过代码获得 R i g i d b o d y Rigidbody Rigidbody等组件,同理也就可以通过代码获得某个游戏对象上的脚本组件。不妨设游戏对象名称为 g m gm gm,脚本(类名)为 t e s t test test,那么通过以下代码即可获得该游戏对象的脚本组件:
test t = gm.GetComponent<test>();
6.作用域和访问修饰符
一般来说在脚本中用 p u b l i c public public修饰的变量可以在 I n s p e c t o r Inspector Inspector视图中显示并修改,用 p r i v a t e private private修饰的变量则不会显示(默认修饰符为 p r i v a t e private private),且公有变量可以被其它脚本访问。函数同理,只不过不会在视图中显示出来。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
public int a = 5;
private int b = 5;
int c = 5;
private void Update()
{
Debug.Log(a);
}
}
即便在游戏运行时,我们也可以通过
I
n
s
p
e
c
t
o
r
Inspector
Inspector视图修改
p
u
b
l
i
c
public
public变量的值,而不需要重新运行(方便我们调整参数的值)。但是在运行过程中修改的值最终并不会保存,这个大家试验一下便知。
如果我们在
I
n
s
p
e
c
t
o
r
Inspector
Inspector视图下修改了
A
A
A的值(未运行游戏哦),那么这个值会覆盖掉代码中的初始值:
如果我们在代码中又修改了
A
A
A的值,比如在
s
t
a
r
t
start
start或者其它函数中,那么代码中的值会覆盖掉
I
n
s
p
e
c
t
o
r
Inspector
Inspector中设置的值(前提是被调用过了):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
public int a = 5;
private int b = 5;
int c = 5;
private void Start()
{
a = 10;
}
private void Update()
{
Debug.Log(a);
}
}
在运行过程中依然可以通过
I
n
s
p
e
c
t
o
r
Inspector
Inspector视图修改
a
a
a的值;当运行结束后,
I
n
s
p
e
c
t
o
r
Inspector
Inspector视图下显示
A
A
A的值仍为
8
8
8(和原来保持一致)。
8.Update 和 FixedUpdate
非物理对象的移动、简单的计时器、输入检测等等一般都在 U p d a t e Update Update中完成,它的时间间隔并不是固定的; F i x e d U p d a t e FixedUpdate FixedUpdate按照固定时间间隔调用,任何影响刚体的动作都应该使用 F i x e d U p d a t e FixedUpdate FixedUpdate进行,并最好使用力来定义移动。
10.启用和禁用组件
修改 e n a b l e d enabled enabled的值。
11.激活游戏对象
通过函数 G a m e O b j e c t . s e t A c t i v e ( ) GameObject.setActive() GameObject.setActive()设置。
12.Translate 和 Rotate
这些函数作用于局部轴而非世界轴。如果想用碰撞体移动某个对象,也就是将会产生物理作用的物体,则不应该使用这些函数,而要考虑使用 P h y s i c s Physics Physics函数。
13.Look At
顾名思义, L o o k A t ( ) LookAt() LookAt()函数可以让游戏对象的正向指向世界中的另一个 t r a n s f o r m transform transform。
using UnityEngine;
using System.Collections;
public class CameraLookAt : MonoBehaviour
{
public Transform target;
void Update ()
{
transform.LookAt(target);
}
}
17.GetAxis
G
e
t
A
x
i
s
(
)
GetAxis()
GetAxis()会返回一个范围在
[
−
1
,
1
]
[-1,1]
[−1,1]的浮点值。轴可以在输入管理器中设置:菜单、
E
d
i
t
Edit
Edit、
P
r
o
j
e
c
t
S
e
t
t
i
n
g
s
Project\ Settings
Project Settings、
i
n
p
u
t
M
a
n
a
g
e
r
input\ Manager
input Manager。
可以把
G
e
t
A
x
i
s
GetAxis
GetAxis想象成一把滑尺,
G
r
a
v
i
t
y
Gravity
Gravity影响按钮松开后滑尺归零的速度;
S
e
n
s
i
t
i
v
i
t
y
Sensitivity
Sensitivity相反,它影响滑尺达到
−
1
、
1
-1、1
−1、1的速度;
D
e
a
d
Dead
Dead相当于一个盲区,当滑尺的值位于这个区域内时,
G
e
t
A
x
i
s
(
)
GetAxis()
GetAxis()会返回
0
0
0;当
S
n
a
p
Snap
Snap被勾选时,同时按下正负按钮会返回
0
0
0值。
G e t A x i s GetAxis GetAxis返回浮点值:
using UnityEngine;
using System.Collections;
public class AxisExample : MonoBehaviour
{
public float range;
public GUIText textOutput;
void Update ()
{
float h = Input.GetAxis("Horizontal");
float xPos = h * range;
transform.position = new Vector3(xPos, 2f, 0);
textOutput.text = "Value Returned: "+h.ToString("F2");
}
}
G e t A x i s R a w GetAxisRaw GetAxisRaw返回整数 ( − 1 、 0 、 1 ) (-1、0、1) (−1、0、1):
using UnityEngine;
using System.Collections;
public class AxisRawExample : MonoBehaviour
{
public float range;
public GUIText textOutput;
void Update ()
{
float h = Input.GetAxisRaw("Horizontal");
float xPos = h * range;
transform.position = new Vector3(xPos, 2f, 0);
textOutput.text = "Value Returned: "+h.ToString("F2");
}
}
18.OnMouseDown
可检测碰撞体、 G U I GUI GUI元素上的鼠标点击事件。如果是碰撞体,那么该游戏对象也需要 r i g i d b o d y rigidbody rigidbody组件。
21.数据类型
using UnityEngine;
using System.Collections;
public class DatatypeScript : MonoBehaviour
{
void Start ()
{
//值类型变量
Vector3 pos = transform.position;
pos = new Vector3(0, 2, 0);
//引用类型变量
Transform tran = transform;
tran.position = new Vector3(0, 2, 0);
}
}
25.Invoke
I n v o k e Invoke Invoke可以让某一函数被延迟执行。 I n v o k e R e p e a t i n g InvokeRepeating InvokeRepeating可以让这个函数被调用多次:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
private void Start()
{
Invoke("myfunc", 1f);//1s之后调用myfuc函数
InvokeRepeating("myfunc", 1f, 0.5f);//1s之后调用myfunc函数 之后每0.5s调用一次
CancelInvoke("myfunc");//停止调用myfuc函数
}
private void myfunc()
{
Debug.Log("test invoke!");
}
}
26.枚举
枚举是一组常量,而且可以让你的代码更加清晰易懂。
using UnityEngine;
using System.Collections;
public class EnumScript : MonoBehaviour
{
enum Direction {North, East, South, West};
//4个值默认为 0 1 2 3
void Start ()
{
Direction myDirection;
myDirection = Direction.North;
}
Direction ReverseDirection (Direction dir)
{
if(dir == Direction.North)
dir = Direction.South;
else if(dir == Direction.South)
dir = Direction.North;
else if(dir == Direction.East)
dir = Direction.West;
else if(dir == Direction.West)
dir = Direction.East;
return dir;
}
}