Unity学习一之(初学脚本 MonoBehaviour类 )
来源:互联网 发布:武汉mac口红专柜在哪里 编辑:程序博客网 时间:2024/04/28 10:02
在看到Unity界面的第一眼,突然感觉到无穷的压力.全英文界面对于我这种初学者而且英文渣得一#的人实在是有点吃不消.用了一天的时间了解了Unity的工具栏的使用以及各种按键的作用,同时刷完了我第一个UNITY地形,感觉很有爱.....软件的使用会随着以后的开发练习会逐步熟练,所以不再多说.
我所知道的Unity是基于组件开发的引擎,当然我现在也只出于理解的层面.我们可以在各种游戏对象上添加自己写好的脚本,让我们的游戏对象生龙活虎.所以写脚本是一个Unity程序开发者必须熟练的一个技能.UNITY支持3种脚本,C#,javascript,boo;对于有C或C++基础的人来说,C#会很好上手,毕竟基本语法相差不太大. 如果自己单纯为了玩Unity,自娱自乐的话,javascript就可以应付大部分你想要的,但如果想要做大型的游戏项目,C#必不可少.最后是Boo,我不会而且我从没听说过.
先说脚本,每一个嵌入在GameObject(游戏对象)里的脚本都有自己的生命周期. 默认的方法执行顺序是(精简版^.^): Awake() --- > OnEnable()- --->Start() --- >FixUpdate()----->Update() ----> LateUpdate() ----->OnGUI() ----->OnDisable() -----> OnDestroy(). 知道脚本内部执行顺序之后,我们还要知道如果有很多脚本分别嵌入到很多游戏对象上时各脚本的执行顺序.通过实验显示.对多个游戏对象而言,后添加的游戏对象中的脚本先执行,先添加的后执行(有点像数据结构中的栈(LIFO));对于单个游戏对象而言,如果内嵌多个脚本,那么同上,后添加的脚本先执行,先添加的脚本后执行.当然我们还可以自己定义脚本的执行顺序,做法:单击脚本文件,在Inspector中选择Execution Order,就可以点击''+"为添加的脚本文件设置Time值,Unity会很自觉地把这些脚本按照它们屁股后面的值从小到大排列,并从上往下依次执行.
当我第一次创建脚本的时候,发现脚本文件里的那个类都是继承于MonoBehaviour这个类,这个类很重要.以下是MonoBehaviour中的重要方法.
Functions函数
- InvokeInvokes the method methodName in time seconds.
在time秒调用methodName方法;简单说,根据时间调用指定方法名的方法 - InvokeRepeatingInvokes the method methodName in time seconds.
在time秒调用methodName方法;简单说,每隔指定时间调用指定方法名的方法 - CancelInvokeCancels all Invoke calls on this MonoBehaviour.
取消这个MonoBehaviour上的所有调用。 - IsInvokingIs any invoke on methodName pending?
某指定函数是否在等候调用。 - StartCoroutineStarts a coroutine.
开始协同程序。 - StopCoroutineStops all coroutines named methodName running on this behaviour.
停止这个动作中名为methodName的所有协同程序 - StopAllCoroutinesStops all coroutines running on this behaviour.
停止所有动作的协同程序
Overridable Functions可重写函数
- UpdateUpdate is called every frame, if the MonoBehaviour is enabled.
当MonoBehaviour启用时,其Update在每一帧被调用。 - LateUpdateLateUpdate is called every frame, if the Behaviour is enabled.
当Behaviour启用时,其LateUpdate在每一帧被调用。 - FixedUpdateThis function is called every fixed framerate frame, if the MonoBehaviour is enabled.
当MonoBehaviour启用时,其 FixedUpdate 在每一帧被调用。 - AwakeAwake is called when the script instance is being loaded.
当一个脚本实例被载入时Awake被调用。 - StartStart is called just before any of the Update methods is called the first time.
Start仅在Update函数第一次被调用前调用。 - ResetReset to default values.
重置为默认值。 - OnMouseEnterOnMouseEnter is called when the mouse entered the GUIElement or Collider.
当鼠标进入到GUIElement(GUI元素)或Collider(碰撞体)中时调用OnMouseEnter。 - OnMouseOverOnMouseOver is called every frame while the mouse is over the GUIElement or Collider.
当鼠标悬浮在GUIElement(GUI元素)或Collider(碰撞体)上时调用 OnMouseOver . - OnMouseExitOnMouseExit is called when the mouse is not any longer over the GUIElement or Collider.
当鼠标移出GUIElement(GUI元素)或Collider(碰撞体)上时调用OnMouseExit。 - OnMouseDownOnMouseDown is called when the user has pressed the mouse button while over the GUIElement or Collider.
当鼠标在GUIElement(GUI元素)或Collider(碰撞体)上点击时调用OnMouseDown。 - OnMouseUpOnMouseUp is called when the user has released the mouse button.
当用户释放鼠标按钮时调用OnMouseUp。 - OnMouseDragOnMouseDrag is called when the user has clicked on a GUIElement or Collider and is still holding down the mouse.
当用户鼠标拖拽GUIElement(GUI元素)或Collider(碰撞体)时调用 OnMouseDrag 。 - OnTriggerEnterOnTriggerEnter is called when the Collider other enters the trigger.
当Collider(碰撞体)进入trigger(触发器)时调用OnTriggerEnter。 - OnTriggerExitOnTriggerExit is called when the Collider other has stopped touching the trigger.
当Collider(碰撞体)停止触发trigger(触发器)时调用OnTriggerExit。 - OnTriggerStayOnTriggerStay is called once per frame for every Collider other that is touching the trigger.
当碰撞体接触触发器时,OnTriggerStay将在每一帧被调用。 - OnCollisionEnterOnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider.
当此collider/rigidbody触发另一个rigidbody/collider时,OnCollisionEnter将被调用。 - OnCollisionExitOnCollisionExit is called when this collider/rigidbody has stopped touching another rigidbody/collider.
当此collider/rigidbody停止触发另一个rigidbody/collider时,OnCollisionExit将被调用。 - OnCollisionStayOnCollisionStay is called once per frame for every collider/rigidbody that is touching rigidbody/collider.
当此collider/rigidbody触发另一个rigidbody/collider时,OnCollisionStay将会在每一帧被调用。 - OnControllerColliderHitOnControllerColliderHit is called when the controller hits a collider while performing a Move.
在移动的时,当controller碰撞到collider时OnControllerColliderHit被调用。 - OnJointBreakCalled when a joint attached to the same game object broke.
当附在同一对象上的关节被断开时调用。 - OnParticleCollisionOnParticleCollision is called when a particle hits a collider.
当粒子碰到collider时被调用。 - OnBecameVisibleOnBecameVisible is called when the renderer became visible by any camera.
当renderer(渲染器)在任何相机上可见时调用OnBecameVisible。 - OnBecameInvisibleOnBecameInvisible is called when the renderer is no longer visible by any camera.
当renderer(渲染器)在任何相机上都不可见时调用OnBecameInvisible。 - OnLevelWasLoadedThis function is called after a new level was loaded.
当一个新关卡被载入时此函数被调用。 - OnEnableThis function is called when the object becomes enabled and active.
当对象变为可用或激活状态时此函数被调用。 - OnDisableThis function is called when the behaviour becomes disabled () or inactive.
当对象变为不可用或非激活状态时此函数被调用。 - OnDestroyThis function is called when the MonoBehaviour will be destroyed.
当MonoBehaviour将被销毁时,这个函数被调用。 - OnPreCullOnPreCull is called before a camera culls the scene.
在相机消隐场景之前被调用。 - OnPreRenderOnPreRender is called before a camera starts rendering the scene.
在相机渲染场景之前被调用。 - OnPostRenderOnPostRender is called after a camera finished rendering the scene.
在相机完成场景渲染之后被调用。 - OnRenderObjectOnRenderObject is called after camera has rendered the scene.
在相机场景渲染完成后被调用。 - OnWillRenderObjectOnWillRenderObject is called once for each camera if the object is visible.
如果对象可见每个相机都会调用它。 - OnGUIOnGUI is called for rendering and handling GUI events.
渲染和处理GUI事件时调用。 - OnRenderImageOnRenderImage is called after all rendering is complete to render image
当完成所有渲染图片后被调用,用来渲染图片后期效果。 - OnDrawGizmosSelectedImplement this OnDrawGizmosSelected if you want to draw gizmos only if the object is selected.
如果你想在物体被选中时绘制gizmos,执行这个函数。 - OnDrawGizmosImplement this OnDrawGizmos if you want to draw gizmos that are also pickable and always drawn.
如果你想绘制可被点选的gizmos,执行这个函数。 - OnApplicationPauseSent to all game objects when the player pauses.
当玩家暂停时发送到所有的游戏物体。 - OnApplicationFocusSent to all game objects when the player gets or looses focus.
当玩家获得或失去焦点时发送给所有游戏物体。 - OnApplicationQuitSent to all game objects before the application is quit.
在应用退出之前发送给所有的游戏物体。 - OnPlayerConnectedCalled on the server whenever a new player has successfully connected.
当一个新玩家成功连接时在服务器上被调用。 - OnServerInitializedCalled on the server whenever a Network.InitializeServer was invoked and has completed.
当Network.InitializeServer被调用并完成时,在服务器上调用这个函数。 - OnConnectedToServerCalled on the client when you have successfully connected to a server
当你成功连接到服务器时,在客户端调用。 - OnPlayerDisconnectedCalled on the server whenever a player disconnected from the server.
当一个玩家从服务器上断开时在服务器端调用。 - OnDisconnectedFromServerCalled on the client when the connection was lost or you disconnected from the server.
当失去连接或从服务器端断开时在客户端调用。 - OnFailedToConnectCalled on the client when a connection attempt fails for some reason.
当一个连接因为某些原因失败时在客户端调用。 - OnFailedToConnectToM...Called on clients or servers when there is a problem connecting to the MasterServer.
当报告事件来自主服务器时在客户端或服务器端调用。 - OnMasterServerEventCalled on clients or servers when reporting events from the MasterServer.
当报告事件来自主服务器时在客户端或服务器端调用。 - OnNetworkInstantiateCalled on objects which have been network instantiated with Network.Instantiate
当一个物体使用Network.Instantiate进行网络初始化时调用。 - OnSerializeNetworkViewUsed to customize synchronization of variables in a script watched by a network view.
在一个网络视图脚本中,用于自定义变量同步。
Class Functions类函数
- printLogs message to the Unity Console. This function is identical to Debug.Log.记录消息到Unity控制台,这个函数和Debug.Log作用相同。
Inherited members继承成员
Inherited Variables继承变量
- enabledEnabled Behaviours are Updated, disabled Behaviours are not.
启用行为被更新,禁用行为不更新。
- transformThe Transform attached to this GameObject (null if there is none attached).
Transform附加到GameObject(游戏物体)(如无附加则为空)。 - rigidbodyThe Rigidbody attached to this GameObject (null if there is none attached).
Rigidbody附加到GameObject(游戏物体)(如无附加则为空)。 - cameraThe Camera attached to this GameObject (null if there is none attached).
Camera附加到GameObject(游戏物体)(如无附加则为空)。 - lightThe Light attached to this GameObject (null if there is none attached).
Light附加到GameObject(游戏物体)(如无附加则为空)。 - animationThe Animation attached to this GameObject (null if there is none attached).
Animation附加到GameObject(游戏物体)(如无附加则为空)。 - constantForceThe ConstantForce attached to this GameObject(null if there is none attached).
ConstantForce附加到GameObject(游戏物体)(如无附加则为空)。 - rendererThe Renderer attached to this GameObject (null if there is none attached).
Renderer附加到GameObject(游戏物体)(如无附加则为空)。 - audioThe AudioSource attached to this GameObject(null if there is none attached).
AudioSource附加到GameObject(游戏物体)(如无附加则为空)。 - guiTextThe GUIText attached to this GameObject (null if there is none attached).
GUIText附加到GameObject(游戏物体)(如无附加则为空)。 - networkViewThe NetworkView attached to this GameObject(Read Only). (null if there is none attached)
NetworkView附加到GameObject(游戏物体)(只读)(如无附加则为空)。 - guiTextureThe GUITexture attached to this GameObject(Read Only). (null if there is none attached)
GUITexture附加到GameObject(游戏物体)(只读)(如无附加则为空)。 - colliderThe Collider attached to this GameObject (null if there is none attached).
Collider附加到GameObject(游戏物体)(如无附加则为空)。 - hingeJointThe HingeJoint attached to this GameObject (null if there is none attached).
HingeJoint附加到GameObject(游戏物体)(如无附加则为空)。 - particleEmitterThe ParticleEmitter attached to this GameObject(null if there is none attached).
ParticleEmitter附加到GameObject(游戏物体)(如无附加则为空)。 - gameObjectThe game object this component is attached to. A component is always attached to a game object.
组件附加的游戏物体。一个组件总是被附加到一个游戏物体。 - tagThe tag of this game object.
游戏物体的标签。
- nameThe name of the object. //物体的名字
- hideFlagsShould the object be hidden, saved with the scene or modifiable by the user?
物体是否被隐藏、保存在场景中或被用户修改?
常用的控制台调试方法是上面的print(),其实它的内部实现就是Debug.Log().
今天先整这一点,加油.
- Unity学习一之(初学脚本 MonoBehaviour类 )
- unity学习一之(初学脚本 Transform类 )
- Unity学习之MonoBehaviour中的常用函数
- Unity学习笔记(2)- MonoBehaviour类的生命周期及脚本报错收集
- Unity Manual之MonoBehaviour
- unity之MonoBehaviour
- Unity进阶修炼之路(一)——MonoBehaviour
- Unity中的MonoBehaviour类
- unity 脚本 function 生命周期 MonoBehaviour Mono行为
- unity 脚本 function 生命周期 MonoBehaviour Mono行为
- Unity学习笔记——MonoBehaviour类Invoke, Coroutine
- Unity学习笔记——MonoBehaviour类Invoke, Coroutine
- unity——MonoBehaviour类
- Unity 入门总结 Monobehaviour 类
- unity学习笔记#1--继承自monobehaviour的脚本不能用new来实例化
- Unity MonoBehaviour
- unity脚本学习(一)
- unity初学一
- Material Design设计规范
- 程序员总结:帮助你早些明白一些道理
- 分布式缓存-Memcached
- 1005.Spell It Right
- Oracle10g数据库备份与恢复总结1--Oracle备份与恢复之概述
- Unity学习一之(初学脚本 MonoBehaviour类 )
- #undef 标识符
- 中国互联网量级分化严重:小米将360踢出第二阵营
- 斜率优化DP BZOJ 1010
- Java内存区域
- 2014年博客之星评选,请大家多多支持
- Java web过滤器验证登录(避免未经登录进入主页)
- HTML 入门教程一[基础内容]
- 1006.Sign In and Sign Out