U3D的一些常用基础脚本
1: var texture :Texture ;
2:
3: function Start () {
4: renderer.material.mainTexture = texture ;
5: renderer.material.color = Color.green ;
6: }
1: function Start () {
2: var count :int = transform.childCount ;
3: for( var i=0; i<count; i++ ) {
4: var child :Transform = transform.GetChild( i );
5: GameObject.Destroy( child.gameObject );
6: }
7: }
1: var prefab :GameObject ;
2: var pos :Vector3 ;
3: var rot :Quaternion ;
4:
5: function Start () {
6: var clone :GameObject = GameObject.Instantiate( prefab ) ; //此处不加位置旋转参数,则会使用预设的参数值
7:
8: var clone2 :GameObject = GameObject.Instantiate( prefab, pos, rot ) ;
9:
10: GameObject.Destroy( clone, 5 );
11: }
function SendMessage (methodName : String, value : object = null, options : SendMessageOptions = SendMessageOptions.RequireReceiver) : void
发消息给自己,一般是由其它的组件来获取消息并处理它;如果options不传,则可以不接收;如果options参数为requireReceiver且游戏对象没有该处理方法,则或报错!
function BroadcastMessage (methodName : String, parameter : object = null, options : SendMessageOptions = SendMessageOptions.RequireReceiver) : void
广播消息给自己及孩子节点,孩子节点递归;只要注册有MethodName处理方法,都会接收到该消息并触发;
function SendMessageUpwards (methodName : String, value : object = null, options : SendMessageOptions = SendMessageOptions.RequireReceiver) : void
广播消息给自己及父级节点,父级节点递归;只要注册有MethodName处理方法,都会接收到该消息并触发;
之所以把这几个消息特意拎出来,是因为之前不知道这个的时候,消息都是先获取到游戏对象的指定组件,再调用指定组件的指定方法,现在看来觉得这种方式有点傻!
1: function Start () {
2: Application.ExternalCall( "jsFunctionToBeCalledByUnity", "paramString" ) ;
3: }
4:
5: function UnityFunctionToBeCalledByJs( param :String ){
6: Debug.Log( "Function is Called By JS , param : " + param ) ;
7: Application.ExternalEval( " alert( 'msg' ) ; " );
8: }
9:
10: //WEB页面的JS
11: function jsFunctionToBeCalledByUnity( param ) {
12: u.getUnity().SendMessage("MyObject", "UnityFunctionToBeCalledByJs", "Hello from a web page!");
13: }
//TO-DO 待完善