天天看点

unity之常用核心类

一、Mono Develop脚本编辑器介绍:

       Unity3D目前支持三种语言的脚本程序,包括C#  JavaScript    Boo, 在一个游戏中开发者可以使用一种或者使用多种语言来实现脚本的控制。

       三种不同的语言其实效率是相当的,最后都会编译成Unity3D内置的中间代码,目前国内用C#的占大多数。

二、项目工程分层设计:

      Senes:  存放场景。      Scripts   存放脚本        Rwsources    资源包        Texture   贴图         Materials:     材质

      Audios    声音      Models    模型

      初始化不要放到构造函数中,而是放到Atart()方法中。

      放到游戏对象中的脚本(实例化) 可以在Insepector 中修改属性,   Get/set 写法属性不被识别, 直接Public 字段就能当属性用,不写修饰符就

是私有private.

三、动态对象

1、创建、克隆、销毁游戏对象

       GameObject cube=Ganmeobject.CreatePrimitive(PrimitiveType.Cube);

       cube.name="cube1";//设定名字

       cube.renderer.material.color=Color.red;

2、动态克隆对象

       GameObject obj=(GameObject)Instantiate(cube);

3、动态销毁对象

       Destroy(obj,1);

4、给游戏物体添加名为FoobarScript脚本

       GameObject.AddComponent(“FoobarScript”);

5、给游戏对象添加刚体

       Rigidbody rb=GameObject.AddConmponent("Rigdbody");

6、给游戏物体添加球体碰撞器

       SphereCollider sc GameObject.AddComponent("SphereCollider");

7、通过名称来查找

       GameObject player=new GameObject("Player");

       GameObject go=GanmeObject.Find("Player");

8、通过tag标签获取单个游戏对象

       GameObject go=GanmeObject.FindWithTag("Player");

       GameObject go=GanmeObject.FindGameObjectWithTag("Player");

9、通过游戏标签获取多组游戏对象

       GameObject[] go=GameObject.FindGameObjectWithTag("Player");

二、协同程序

1、Coroutine

       协同程序:即在主程序运作时同时开启另外一个逻辑处理来协同当前程序的执行。

2、Yield

       Yield语句是一种特说类型的return语句,它可以确保函数在下一次被执行时,不是从头开始,而是从yield语句开始。

       例如:void Start()

       {

        print("Start开始");

       StartCoroutine((Do));

       print("Do调用后");

       }

       IEnumerator Do()

       {

       print("Do开始");

       yield return new WaitForSecond(3.0f);

       print ("暂停3秒后");

      }

消息传递函数:

       GameObject.SenMessage: 向自身的脚本中发送消息。

        GameObject.BroadcastMessage向自身及物体的脚本发送信息。

       GameObject.SenMessageUpwards: 向自身及父物体中发送消息。

Sphere:   

       void Start () 

       {

       gameObject.SendMessageUpwards ("Do","向父对象发出信息");

       }

       void Do(string message)

      {

       print (message);

      }

欢迎来到我们的狗刨网。我们的网址是http://www.gopedu.com/。