12 第1页 | 共2 页下一页
返回列表 发新帖
查看: 2952|回复: 18
打印 上一主题 下一主题

【转】使用C#写u3d的脚本需要注意的问题

[复制链接]

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

跳转到指定楼层
楼主
发表于 2011-7-26 08:42:48 |只看该作者 |倒序浏览
Writing Scripts in C#

       使用C#写脚本



        Apart from syntax, there are some differences when writing scripts in C# or Boo. Most notable are:

        除了语法外,使用C#或Boo会有一些差别,最明显的是:



        1. Inherit from MonoBehaviour

        继承之MonoBehaviour类



        All behaviour scripts must inherit from MonoBehaviour (directly or indirectly). This happens automatically in Javascript, but must be explicitly explicitly inside C# or Boo scripts. If you create your script inside Unity through the Asset -> Create -> C Sharp/Boo Script menu, the created template will already contain the necessary definition.

        所有的行为脚本代码必须继承之MonoBehaviour类(直接或间接)。如果使用的是javascript的话会自动(隐性)的继承,如果使用的是 C#或Boo就必须明确地指定其继承于MonoBehaviour。如果你是在u3d中通过“Asset->Create->C Sharp Script/Boo Script”来创建了脚本代码文件的话,u3d的脚本创建模板将会提前将相关继承语句定义在脚本代码文件中。



        public class NewBehaviourScript : MonoBehaviour {...}     // C#



        class NewBehaviourScript (MonoBehaviour): ...      # Boo





        2. Use the Awake or Start function to do initialisation.

        使用Awake或Start方法进行初始化。



        What you would put outside any functions in Javascript, you put inside Awake or Start function in C# or Boo.

        ,你(需要)在C#或Boo在使用Awake或Start方法。



        The difference between Awake and Start is that Awake is&nbsp***n when a scene is loaded and Start is called just before the first call to an Update or a FixedUpdate function. All Awake functions are called before any Start functions are called.

Awake和Start之间的区别在于:Awake是当一个场景调入过程完成后会自动运行,而Start则是会在Update或FixedUpdate方法被第一次调用之前被运行。所有的Awake方法运行的优先级会高于任意的Start方法。







        3. The class name must match the file name.

        (文件中)主类名必须与文件名相同。



        In Javascript, the class name is implicitly set to the file name of the script (minus the file extension). This must be done manually in C# and Boo.

        在javascript脚本文件中,u3d虽然没有明确地定义主类,但事实上,u3d已经隐性地自动定义了主类,并将类名设置为等于脚本文件名(不包括扩展名)。

        如果使用的是C#a或Boo脚本,那就必须得手动的将主类名设置为与文件同名。



        4. Coroutines have a different syntax in C#.

        使用C#实现协同,在语法上会有一处不同。



        Coroutines have to have a return type of IEnumerator and you yield using yield return ... ; instead of just yield

        (U3D中的)协同会(同时)用一个属于IEnumerator接口类型(枚举)的返回值和你使用的yield 返回值...;来替代yield......;

        如下面代码:



        using System.Collections;

        using UnityEngine;

        public class NewBehaviourScript : MonoBehaviour {

                // C# coroutine

                IEnumerator SomeCoroutine () {

                        // Wait for one frame

                        yield return 0;



                        // Wait for two seconds

                        yield return new WaitForSeconds (2);

}

        }



        5. Don't use namespaces.

        不要使用命名空间。

        Unity doesn't support placing your scripts inside of a namespace at the moment. This requirement will be removed in a future version.

        U3D目前不支持你在脚本中使用命名空间,这个需求会在未来的版本中实现。





        6. Only member variables are serialized and are shown in the Inspector.

        只有(public公有的)成员变量是可以在U3D程序的Inspector栏中会被以序列形式显示出来



        Private and protected member variables are shown only in Expert Mode. Properties are not serialized or shown in the inspector.

        私有类型(private)和成员类型(protected)变量只能在专家模式(Expert Mode)下可见,(而且)属性(Properties)



        7. Avoid using the cons***ctor.

        避免使用构造函数



        Never initialize any values in the cons***ctor. Instead use Awake or Start for this purpose. Unity automatically invokes the cons***ctor even when in edit mode. This usually happens directly after compilation of a script, because the cons***ctor needs to be invoked in order to retrieve default values of a script. Not only will the cons***ctor be called at unforeseen times, it might also be called for prefabs or inactive game objects.

        不要通过构造函数来初始化变量。这些工作可以使用第2条中的Awake方法和Start方法来替代(换句话来说就是在u3d中,Awake方法和 Start方法是每个脚本文件类中默认的构造函数)。U3D甚至可以在标准编辑模式下就调用它们。它们通常是直接汇编在脚本中,因为构造函数需要检索默认脚本变量用于引用。(u3d)在任意的时候不光可以调用构造函数,还可能会调用预设(物体)或未被唤醒的游戏物体。



        In the case of eg. a singleton pattern using the cons***ctor this can have severe consequences and lead to seemingly random null reference exceptions.

        实例化(C#脚本文件)时,单脚本文件状态下使用(自定义的)构造函数(可能)会导致严重的后果,并且会产生引用为空的异常。



        So if you want to implement eg. a singleton pattern do not use the the cons***ctor, instead use Awake. Actually there is no reason why you should ever have any code in a cons***ctor for a class that inherits from MonoBehaviour.

        所以,如果你实例化C#脚本文件(即运行C#脚本文件。这是c#程序运行的基本方式,详细内容可以从C#专门的教材中了解),单脚文件不要使用(自定义的)的构造函数,直接使用Awake方法替代即可,实在没有理由为一个继承之MonoBehaviour的(文件)类写任何的(构造函数)代码。
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

797

主题

1

听众

1万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
5568
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2011-8-2 21:20:22 |只看该作者
回复

使用道具 举报

315

主题

0

听众

1万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
10878
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2011-8-8 13:55:27 |只看该作者
顶起!
回复

使用道具 举报

315

主题

0

听众

1万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
10878
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

地板
发表于 2011-8-8 14:13:07 |只看该作者
学习了。
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

5#
发表于 2011-8-20 11:08:23 |只看该作者
学习,为了以后!!!
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

6#
发表于 2012-1-30 23:18:56 |只看该作者
美酒令人回味,音乐让人陶醉,好书百读不悔,情意形影相随,节日问候最可贵。又到年终岁尾,愿你幸福健康作陪,笑得合不拢嘴,预祝春节快乐!
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

7#
发表于 2012-3-3 23:19:19 |只看该作者
真是不错啊
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

8#
发表于 2012-3-19 23:34:00 |只看该作者
不错 非常经典  实用
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

9#
发表于 2012-3-24 23:32:58 |只看该作者
跑着去顶朋友滴铁
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

10#
发表于 2012-4-3 13:07:19 |只看该作者
不会吧,太恐怖了
回复

使用道具 举报

12 第1页 | 共2 页下一页
返回列表 发新帖
您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-15 17:27 , Processed in 0.091577 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部