unity学习1

来源:互联网 发布:js json特殊字符处理 编辑:程序博客网 时间:2024/06/10 22:07


  可以使用三种语言开发:C#、JavaScript和Boo
  1、Assets文件管理:
Senes - 场景
Script - 脚本
Resources - 资源包
Textures - 贴图
Materials - 材质
Audios - 声音
Models - 模型
  2、Start()方法-初始化
     拖放到游戏对象的脚本,其中属性可以用public修饰,这样就可以在inspector修改属性,没有这默认属性为私有。
  3、程序脚本中(动态)创建对象(运行时,对象才可以在层级视图中看到)
     创建一个立方体:
     GameObject cube =GameObject.CreatePrimitive(PrimitiveType.Cube);
     cube.name ="立方体";
     cube.renderer.material.color=Color.red;
  4、克隆对象(复制对象效率高,一般用于产生大量子弹)
     克隆上面创建的立方体:
     GameObject  c =(GameObject)Instantiate(cube);
  5、销毁对象
     3秒后销毁克隆的立方体
     Destroy(c,3);
  6、动态添加脚本
     把script脚本加到cube上
     cube.AddComponent("script")
     销毁cube对象上附加的脚本组件实例
     Destory(cube.GetComponent("script"));
  7、给游戏物体添加刚体
     Rigibody cube= GameObject.AddComponent("Rigibody");
  8、给游戏物体添加球体碰撞器
    SphereCollider  sc = GameObject.AddComponent ("SphereCollider");
  9、1)通过名称来查找:
       GameObject  player = new GameObject("Player");
       GameObject  go=GameObject.Find(“Player”);
     2)通过tag标签获取单个游戏对象:
       GameObject  go=GameObject.FindWithTag(“Player”);
       GameObject go=GameObject..FindGameObjectWithTag (“Player”);
     3)通过游戏标签获取多组游戏对象:
       GameObject[]  go=GameObject.FindGameObjectsWithTag (“Player”);
  10、组件引用(放到Start()方法中)
    GetComponent                得到组件
    GetComponents               得到组件列表(用于有多个同类型组件的时候)
    GetComponentInChildren      得到对象或对象子物体上的组件
    ScriptName other = GameObject.GetComponent<ScriptName>();?\脚本集合?
  11、常用核心类
    Time类:按A键X轴反方向(左),按D键X轴正方向(右)
           按W键Z轴反方向(左),按S键Z轴正方向(右)
                float move_h=0,move_v=0;
if (Input.GetKey (KeyCode.A)) {
move_h-=Time.deltaTime;
}
                 if (Input.GetKey (KeyCode.D)) {   
move_h+=Time.deltaTime;
}
                 if(Input.GetKey.(KeyCode.w)){
                      move_v+=Time.deltaTime;
                }
                 if(Input.GetKey.(KeyCode.w)){
                      move_v-=Time.deltaTime;
                }
                transform.Translate(new Vector3(move_h,0,move_v));
    Coroutine:   
    协同程序,即在主程序运行时同时开启另一段逻辑处理来协同当前程序的执行。
    在Unity3D中,使用MonoBehaviour.StartCoroutine方法即可开启一个协同程序,也就是说该方法必须在MonoBehaviour或继承于MonoBehaviour的类中调用。
    在Unity3D中,使用StartCoroutine(string methodName)可以开启一个线程。 
    Yield:(特殊类型Return语句,可以确保函数在下次执行时,从Yield语句开始执行)
          实例:
          void Start () {
            print("Start开始");   //执行一
            StartCoroutine("Do");//开启线程,跳转到Do()方法
            print("Do调用后");  //执行三
         }
 
           IEnumerator Do(){
           print("Do开始");  //执行二
           yield return new WaitForSeconds(3.0f);  ???
           print("暂停3秒后"); //执行四
         }
    Random类(随机数): float a=Random.value;//Random.取值范围
       int b=Random.Range(0,100)    包括最大和最小
       float c=Random.Range(0.0f,1.0f);   包括最小但不包括最大
    消息传递函数:
       GameObject.SendMassege:向自身的脚本发送消息
       GameObject.SendMassegeUpwards;向自身和父物体脚本发消息
       GameObject.BoardcastMessage;向自身和子物体脚本发消息
       实例:
        Sphere(子):       
        void Start () {
           gameObject.SendMessageUpwards ("Do","向父对象发出信息");
        }

        Cube(父):
        void Do(string message){
           print (message);
        }

这里有很多U3D资源、U3D培训视频、U3D常见问题、U3D项目源码我们可以一起交流学习


 
  可以使用三种语言开发:C#、JavaScript和Boo
  1、Assets文件管理:
Senes - 场景
Script - 脚本
Resources - 资源包
Textures - 贴图
Materials - 材质
Audios - 声音
Models - 模型
  2、Start()方法-初始化
     拖放到游戏对象的脚本,其中属性可以用public修饰,这样就可以在inspector修改属性,没有这默认属性为私有。
  3、程序脚本中(动态)创建对象(运行时,对象才可以在层级视图中看到)
     创建一个立方体:
     GameObject cube =GameObject.CreatePrimitive(PrimitiveType.Cube);
     cube.name ="立方体";
     cube.renderer.material.color=Color.red;
  4、克隆对象(复制对象效率高,一般用于产生大量子弹)
     克隆上面创建的立方体:
     GameObject  c =(GameObject)Instantiate(cube);
  5、销毁对象
     3秒后销毁克隆的立方体
     Destroy(c,3);
  6、动态添加脚本
     把script脚本加到cube上
     cube.AddComponent("script")
     销毁cube对象上附加的脚本组件实例
     Destory(cube.GetComponent("script"));
  7、给游戏物体添加刚体
     Rigibody cube= GameObject.AddComponent("Rigibody");
  8、给游戏物体添加球体碰撞器
    SphereCollider  sc = GameObject.AddComponent ("SphereCollider");
  9、1)通过名称来查找:
       GameObject  player = new GameObject("Player");
       GameObject  go=GameObject.Find(“Player”);
     2)通过tag标签获取单个游戏对象:
       GameObject  go=GameObject.FindWithTag(“Player”);
       GameObject go=GameObject..FindGameObjectWithTag (“Player”);
     3)通过游戏标签获取多组游戏对象:
       GameObject[]  go=GameObject.FindGameObjectsWithTag (“Player”);
  10、组件引用(放到Start()方法中)
    GetComponent                得到组件
    GetComponents               得到组件列表(用于有多个同类型组件的时候)
    GetComponentInChildren      得到对象或对象子物体上的组件
    ScriptName other = GameObject.GetComponent<ScriptName>();?\脚本集合?
  11、常用核心类
    Time类:按A键X轴反方向(左),按D键X轴正方向(右)
           按W键Z轴反方向(左),按S键Z轴正方向(右)
                float move_h=0,move_v=0;
if (Input.GetKey (KeyCode.A)) {
move_h-=Time.deltaTime;
}
                 if (Input.GetKey (KeyCode.D)) {   
move_h+=Time.deltaTime;
}
                 if(Input.GetKey.(KeyCode.w)){
                      move_v+=Time.deltaTime;
                }
                 if(Input.GetKey.(KeyCode.w)){
                      move_v-=Time.deltaTime;
                }
                transform.Translate(new Vector3(move_h,0,move_v));
    Coroutine:   
    协同程序,即在主程序运行时同时开启另一段逻辑处理来协同当前程序的执行。
    在Unity3D中,使用MonoBehaviour.StartCoroutine方法即可开启一个协同程序,也就是说该方法必须在MonoBehaviour或继承于MonoBehaviour的类中调用。
    在Unity3D中,使用StartCoroutine(string methodName)可以开启一个线程。 
    Yield:(特殊类型Return语句,可以确保函数在下次执行时,从Yield语句开始执行)
          实例:
          void Start () {
            print("Start开始");   //执行一
            StartCoroutine("Do");//开启线程,跳转到Do()方法
            print("Do调用后");  //执行三
         }
 
           IEnumerator Do(){
           print("Do开始");  //执行二
           yield return new WaitForSeconds(3.0f);  ???
           print("暂停3秒后"); //执行四
         }
    Random类(随机数): float a=Random.value;//Random.取值范围
       int b=Random.Range(0,100)    包括最大和最小
       float c=Random.Range(0.0f,1.0f);   包括最小但不包括最大
    消息传递函数:
       GameObject.SendMassege:向自身的脚本发送消息
       GameObject.SendMassegeUpwards;向自身和父物体脚本发消息
       GameObject.BoardcastMessage;向自身和子物体脚本发消息
       实例:
        Sphere(子):       
        void Start () {
           gameObject.SendMessageUpwards ("Do","向父对象发出信息");
        }

        Cube(父):
        void Do(string message){
           print (message);
        }



 
0 0
原创粉丝点击