零基础Unity学习笔记

Unity C# 基本概念 —— 基于官网手册的整理(未完)

2016-08-05  本文已影响322人  pirateH

一、通过组件操控物体

……scripting is also about modifying Component properties to manipulate GameObjects. The difference, though, is that a script can vary a property’s value gradually over time or in response to input from the user. By changing, creating and destroying objects at the right time, any kind of gameplay can be implemented.

来自http://docs.unity3d.com/Manual/ControllingGameObjectsComponents.html

That’s great!

1. 获取物体上的其他组件 GetComponent

获取

void Start () {

Rigidbody rb = GetComponent();

}

并可以设定该组件的属性值

rb.mass = 10f;

在该组件上使用函数

rb.AddForce(Vector3.up * 10f);

也可以获取另一个脚本

...you can use GetComponent as usual and just use the name of the script class (or the filename) to specify the Component type you want.

= GetComponent<GameController>();

2. 获取其他物体

(1)直接通过inspector指向获取 public GameObject player;

        如果类写的是组件类型可直接调用该物体的该类型组件:

        public Transform playerTransform; (inspector中拖进去还是GameObject)

(2)获取子物体(没懂)

(3)通过名字或tag获取物体:

player = GameObject.Find("MainHeroCharacter");

player = GameObject.FindWithTag("Player");

enemies = GameObject.FindGameObjectsWithTag("Enemy");

二、Event Functions

MonoBehavior下的function集锦:

http://docs.unity3d.com/ScriptReference/MonoBehaviour.html

1. Update Events

Update,FixedUpdate, LateUpdate

2.Initialization Events

Start & Awake

Awake:The Awake function is called for each object in the scene at the time when the scene loads. 

全部Awake会在Start之前完成。多个Awake之间、多个Start之间的执行顺序则是随机的。

3. GUI events

Unity has a system for rendering GUI controls over the main action in the scene and responding to clicks on these controls.

4.Physics events

类似 OnCollisionEnter,OnCollisionStay and OnCollisionExitOnTriggerEnter,OnTriggerStay and OnTriggerExit

三、时间与帧率控制

1.帧率平衡

物体移动时,为避免由于帧率变化导致的移动速度不稳定,引入Time.deltaTime

transform.Translate(0, 0, distancePerSecond * Time.deltaTime);

每秒移动距离固定,每帧移动距离变化

2. Maximum Allowed Timestep

 A limit on the amount of time Unity will spend processing physics and FixedUpdate calls during a given frame update. 一旦超出设定的时间限度,物理引擎就会停下来等待帧刷新完,物体的移动会受影响但不会很明显。

3.游戏时间的加速、减速和暂停 —— Time Scale

Time.timeScale = 0f; 暂停

Time.timeScale = 1f; 恢复正常

The Update function is likely to be called more often than usual when game time is slowed down but thedeltaTimestep reported each frame will simply be reduced.

Other script functions are not affected by the time scale so you can, for example, display a GUI with normal interaction when the game is paused.

Time.fixedDeltaTime也可以在脚本中设定。

4.录屏功能——Capture Framerate

Time.captureFramerate

When the property’s value is set to anything other than zero, game time will be slowed and the frame updates will be issued at precise regular intervals.

string name = string.Format("{0}/{1:D04} shot.png", folder, Time.frameCount );

Application.CaptureScreenshot(name);

四、创造和销毁物体

Instantiate

Instantiate(Object original);

Instantiate(Object original,Vector3 position,Quaternion rotation);

Destroy

// Kills the game object

Destroy (gameObject);

// Removes this script instance from the game object

Destroy (this);

// Removes the rigidbody from the game object

Destroy (rigidbody);

// Kills the game object in 5 seconds after loading the object

Destroy (gameObject, 5);

五、协程Coroutines

不运行完update就跳出

应用于渐变效果或等待(冷却时间)效果

六、脚本执行的顺序

http://docs.unity3d.com/Manual/ScriptCompileOrderFolders.html

七、命名空间Namespaces

全部脚本内容加前缀,防止不同脚本间的命名冲突

namespace Enemy {

public class Controller1 : MonoBehaviour {...}

}

2016.08.05 后面的都看不太懂了,先继续做roguelike教程去了

上一篇下一篇

猜你喜欢

热点阅读