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: }
  • 创建预设的游戏对象并在5秒后销毁
       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处理方法,都会接收到该消息并触发;

    之所以把这几个消息特意拎出来,是因为之前不知道这个的时候,消息都是先获取到游戏对象的指定组件,再调用指定组件的指定方法,现在看来觉得这种方式有点傻!

  • U3D与Web间的通信
       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: }
  • Unity Web 个性化参数配置

    //TO-DO 待完善

  • posted @ 2013-06-24 01:06  寻梦丄天涯  阅读(1036)  评论(0编辑  收藏  举报