查看: 1816|回复: 0
打印 上一主题 下一主题

Unity3D脚本中文教程2.2

[复制链接]

1602

主题

1

听众

2万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
24658
精华
6

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

跳转到指定楼层
楼主
发表于 2012-5-14 14:37:38 |只看该作者 |倒序浏览
◆  function AddMixingTransform (mix : Transform, recursive : bool = ***e) : void

描述:添加应该被动画的变换。这允许你缩减需要创建的动画数量。

例如你可能有一个挥手的动画。你可能想在一个空闲角色或行走的角色上播放挥手动画。那么你需要为空闲和行走分别创建挥手动画。运用合成挥手动画,它将由肩膀完全控制。但是下半身不会受它的影响,继续播放空闲或行走动画。因此你只需要一个挥手动画。

如果recursive为真,所有mix变换的子也都将被动画。如果你不调用AddMixingTransform,所有动画曲线将被使用。

// 使用路径添加混合

var shoulder : Transform;

animation["wave_hand"].AddMixingTransform(shoulder);

function Start ()

{

//使用路径添加混合变换

var mixTransform = transform.Find("root/upper_body/left_shoulder");

animation["wave_hand"].AddMixingTransform(mixTransform);

}

Application



访问应用程序的运行时数据。

这个类包含静态的方法来查找相关的信息并控制运行时数据。

类变量

◆  static var absoluteURL : string

描述:到web播放器数据文件夹的绝对路径(只读)。

Application.absoluteURL 和Application.srcValue允许你检测unityWeb数据文件是否被移动或链接接到其他位置。你也许想保护这两者来防止盗用数据文件的行为。

// 检测你的数据文件是否被移动到其他的服务器

// 或是被链接到其他地方

function Start ()

{

var isPirated = false;

if (Application.platform == RuntimePlatform.WindowsWebPlayer ||Application.platform == RuntimePlatform.OSXWebPlayer)

{

if (Application.srcValue != "game.unity3d")

isPirated = ***e;

if (String.Compare (Application.absoluteURL,http://www.website.com/Game/game.unity3d,***e)!=0)

isPirated = ***e;

if (isPirated)

print("irated web player");

}

}

◆  static var dataPath : string

描述:包含游戏数据文件夹的路径(只读)。

这个值依赖于运行的平台:

Unity 编辑器:     <工程文件夹的路径>/Assets

Mac播放器:       <到播发器应用的路径>/Contents

Win播放器:       < 包含可执行播发器的文件夹的路径>Data

Dasboard窗口:    < dashboard widget bundle的路径>

Web播放器:       到播放器数据文件夹的绝对路径(没有实际的数据文件名称)

// 打印到数据文件夹的路径

Print(Application.dataPath);

◆  static var isEditor : bool

描述:是在Unity编辑器内运行?(只读)

如果游戏从Unity编辑器中运行,返回真;如果从其他部署目标运行返回假。

if (Application.isEditor)

{

print("We are***nning this from inside of the editor!");

}

◆  static var isLoadingLevel : bool

描述:正在加载某些关卡?(只读)

LoadLevel 和 LoadLevelAdditive不会立即发生一个新的关卡在当前游戏帧之后被加载。如果关卡加载所请求的帧已经完成isLoadingLevel返回***e。

参见:LoadLevel,LoadLevelAdditive

◆  static var isPlaying : bool

描述:在任何类型的播放器中时返回真(只读)。

在Unity编辑器中,如果处于播放模式时返回真。

if (Application.isPlaying)

{

print("In player or playmode");

}

◆  static var levelCount : int

描述:可用的总关卡数(只读)。

// 加载一个随机的关卡

Application.LoadLevel (Random.Range(0, Application.levelCount-1));

◆  static var loadedLevel : int

描述:最后一个被加载的关卡的索引(只读)。

print (Application.loadedLevel);

◆  static var loadedLevelName : string

描述:最后一个被加载的关卡的名称(只读)。

print (Application.loadedLevelName);

◆  static var platform : RuntimePlatform

描述:返回游戏运行的平台(只读)。

如果你要做一些平台相关的操作使用这个属性。参见:RuntimePlatform

function Start ()

{

if (Application.platform == RuntimePlatform.WindowsPlayer)

print ("Do something special here!");

}

◆  static var***nInBackground : bool

描述:应用程序在后太时是否应该被运行?

默认为假(当程序在后台时暂停)。

// 让游戏运行,即使是在后台

Application***nInBackground = ***e;

◆  static var srcValue : string

描述:相对于html文件的web播放器数据文件的路径(只读)。

这是被写到html文件中的路径,它是作为object的src参数和cmbed标签。因此如果它是绝对url,srcvalue将含有绝对路径。

Application.absoluteURL 和 Application.srcValue允许你检测你的unityWeb数据文件是否被移动或链接到其他位置。你也许想保护这两者来阻止盗用数据文件的行为。

// 检测你的数据文件是否被移到其他的服务器

// 或是被链接到其他地方

function Start ()

{

Var isPirated = false;

if (Application.platform == RuntimePlatform.WindowsWebPlayer ||Application.platform == RuntimePlatform.OSXWebPlayer)

{

if (Application.srcValue != "game.unity3d")

isPirated = ***e;

if (String.Compare (Application.absoluteURL,"http://www.website.com/Game/game.unity3d",***e)!= 0)

isPirated = ***e;

if (isPirated)

print("irated web player");

}

}

◆  static var streamedBytes : int

描述:我们从主Unityweb流中下载了多少字节(只读)。

在web播放器中这将返回到目前为止已经下载的压缩字节数。在独立模式或编辑器中

这个总是返回零。

参见:GetStreamProgressForLevel函数

◆  static var targetFrameRate : int

描述:命令游戏尝试以一个特定的帧率渲染。

设置targetFrameRate为-1(默认)使独立版游戏尽可能快的渲染,并且web播放器游戏以50-60帧/秒渲染,取决于平台。

注意设置targetFrameRate不会保证帧率,会因为平台的不同而波动,或者因为计算机太慢而不能取得这个帧率。

在编辑器中targetFrameRate被忽略。

◆  static var unityVersion : string

描述:用于播放内容的Unity运行时版本。

类方法

◆  static function CancelQuit () : void

描述:取消退出。这可以用来在退出游戏的时候显示一个退出画面。

这个函数只工作在播发器中,在web播放器或编辑器中不做任何事。

// 延迟2秒退出。

// 在这段时间内加载退出画面

var showSplashTimeout = 2.0;

private var allowQuitting = false;

function Awake () {

// 需要在多个关卡中使用的游戏物体

DontDestroyOnLoad (this);

}

function OnApplicationQuit () {

// 如果我们还没有加载到最后的退出画面

if (Application.loadedLevelName.ToLower()!= "finalsplash")

StartCoroutine("DelayedQuit");

// Don't allow the user to exit until we got permission in

if (!allowQuitting)

Application.CancelQuit();

}

function DelayedQuit ()

{

Application.LoadLevel("finalsplash");

// 等待showSplashTimecout

yield WaitForSeconds(showSplashTimeout);

// 然后退出

allowQuitting = ***e;

Application.Quit();

}

◆  static function CanStreamedLevelBeLoaded(levelIndex : int) : bool

描述:可以加载流式关卡了吗?

参见:GetStreamProgressForLevel函数。
◆  static function CanStreamedLevelBeLoaded(levelName : string) : bool

描述:可以加载流式关卡了吗?

参见:GetStreamProgressForLevel函数。

◆  static function CaptureScreenshot(filename : string) : void

描述:截取屏幕为PNG文件放置在路径filename。

如果文件已经存在,它将被覆盖。如果在web播放器或者Dashboard窗口中使用该函数,它将不做任何事情。

function OnMouseDown () {

Application.CaptureScreenshot("Screenshot.png");

}

◆  static function ExternalCall(functionName:string,params argsbject[]):void

描述:调用一个包含中网页中的函数(只用于Web Player)。

调用包含在网页中名为functionNameJavaScript函数,并传递给定的参数。支持原始的数据类型(string, int, float, char)和这些类型的数字。如何其他的对象被转化为字符串(使用ToString方法)并作为字符串传递。

传递的参数数量是可变的。

// 调用网页上的MyFunction1并不使用参数。

Application.ExternalCall ("MyFunction1");

//调用网页上的MyFunction2并使用字符串参数。

Application.ExternalCall ("MyFunction2", "Hello from Unity!");

//调用网页上的MyFunction3并使用几个不同类型的参数。

Application.ExternalCall ("MyFunction3", "one", 2, 3.0);

被调用的在HTML中的函数只需要使用标准的语法即可,例如:

<script language="JavaScript" type="text/javascript">

<!—

// 使用来自Unity的调用,这将接受

// "Hello from Unity!" 做为参数

function MyFunction2( arg )

{

alert( arg );

}

-->

</script>

See Also: Browser to Unity communication, Application.ExternalEval.

◆  static function Externaleval_r(script : string) : void

描述:调用包含在网页中的片段脚本函数(只用于Web Player)。

这将执行包含在网页中JavaScript片段script

// 导航到前一个页面

Application.Externaleval_r("history.back()");

See Also: Browser to Unity communication, Application.ExternalCall.

◆  static function GetStreamProgressForLevel(levelIndex : int) : float

描述:下载了多少?

在web播放器中这将返回这个关卡的进度。

参见:CanStreamedLevelBeLoaded

◆  static function GetStreamProgressForLevel (levelName : string) : float

描述:下载了多少?[ 0......1]

在web播放器中这将返回关卡的进度。

参见:CanStreamedLeverlBeLoaded 函数。

◆  static function LoadLevel(index : int) : void

描述:加载关卡。

这个函数按照索引加载关卡。在Unity中使用File->Build Settings.....菜单可以看到所有 关卡的索引列表。在你能过加载关卡之前你必须将它添加到游戏使用关卡列表中。在 Unity中使用File->Build Settings.....并添加你需要的关卡到关卡列表中。

//加载索引为 0 的关卡

Application . LoadLevel(0);

当加载崭新的关卡时,所有已经加载的游戏物体都将被销毁。如果你想让物体在被加 载新关卡时不被销毁,使用Object.DontDestroyOnLoad 。

◆  Static function LoadLevel( name : string) : void

描述:按照它的名称加载关卡。

在你能够加载关卡之前你必须将它添加到游戏使用的关卡列表中。在Unity中使用

File->Build Settings..... 并添加你需要的关卡到关卡列表中。关卡被加载所有激活物体上 的MonoBehaviour . OnLevelWasLoaded都被调用。

// 加载名为“HighScore”的关卡。

Application . LoadLevel("HighScore");

当加载新的关卡时,所有已经加载的游戏物体都将被销毁。 如果你想让物体在加载新

关卡时不被销毁,使用Object. DontDestroyOnLoad。

◆  static function LoadLevelAdditive ( index : int ) : void

◆  static function LoadLevelAdditive (name : string ) : void

描述:额外地加载一个关卡。

不同于LoadLevel,LoadLeavelAdditive 不会销毁当前关卡中的物体。新关卡中的物体

将被添加到当前关卡。这对于创建连续的虚拟世界时非常有用的,当你走过时更多的内

荣被加载。

◆  static function OpenURL( url : string ) : void

描述:在浏览器中打开url 。

在编辑器或者独立播放器模式下,这将在缺省的浏览器中使用新页打开url 。这将是浏

览器位于前端。

但在网页中执行时,包含插件的页将被重定向到 url 。

Function Start ( ) {

Application . OpenURL ("http://unity3d.com");

}

◆  Static function Quit ( ) : void

描述:退出应用程序。在编辑器或者web播放器中退出被忽略。

//当用户点击escape时退出播放器

Function Update ( ){

If ( Input GetKey ( "escape" )){

Application . Quit ( ) ;

}
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-7-14 10:00 , Processed in 0.133091 second(s), 28 queries .

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

© 2008-2019 Narkii Inc.

回顶部