您的位置:首页 > 移动开发 > Unity3D

Unity3D脚本中文系列教程(四)

2012-11-29 11:01 417 查看
二、 运行时类

AnimationCurve



动画曲线,在给定的时间添加关键帧并确定曲线。

变量

◆ var
keys : Keyframe[]

描述:定义在动画曲线中的所有键。这让你从数组中清理,添加或移除键。

如果键没有按照时间顺序,它们会在赋值的时候自动排序。

◆ var
length : int

描述:曲线中键的数量(只读)。

◆ var
preWrapMode : WrapMode

描述:第一帧之前动画的行为。

◆ var
this[index : int] : Keyframe

描述:取向索引为index的键(只读)。

构造函数

◆ static
function AnimationCurve(params keys : Keyframe[]) : AnimationCurve

描述:从任意数量的关键帧创建一个动画曲线。

该函数从可变数量的Keyframe参数创建一个曲线,如果你想从一个关键帧数组中创建一个曲线,创建一个空的曲线并指定keys属性。

//一个慢退慢出的动画曲线(切线都是平的)。

var curve = new AnimationCurve(Keyframe(0, 0), Keyframe(1, 1);

function Update ()

{

transform.position.x = Time.time;

transform.position.y = curve.Evaluate(Time.time);

}

◆ static
function AnimationCurve () : AnimationCurve

描述:创建一个空的动画曲线

函数

◆ function
AddKey (time : float, value : float) : int

描述:添加一个新的键到曲线。

平滑切线将被自动为该键的计算,返回该键的索引,如果因为在同一时间上已经有另一个关键帧而不能添加键,将返回-1。

◆ function
AddKey (key : Keyframe) : int

描述:添加一个新的键到曲线。

返回该键的索引,如果因为在同一时间上已经有另一个关键帧而不能添加键,将返回-1。

◆ function
Evaluate (time : float) : float

描述:该动画曲线在time的值。

◆ function
MoveKey (index : int, key : Keyframe) : int

描述:移除index处的关键帧并插入键。

如果一个关键帧已经存在于key-time,老的关键帧位置时间key[index].time/将被用来替换,这对于在一个曲线编辑器中拖动关键帧是一个理想的行为,移动它后返回关键帧的索引。

◆ function
RemoveKey (index : int) : void

描述:移除一个键

◆ function
SmoothTangents (index : int, weight : float) : void

描述:平滑位于index处的关键帧的进出切线。

权值为0时平均切线。

类方法

◆ static
function EaseInOut (timeStart : float, valueStart : float, timeEnd : float, valueEnd : float) : AnimationCurve

描述:一个渐进渐出的曲线,开始于timeStart,valueStart并结束于timeEnd, valueEnd.

◆ static
function Linear (timeStart : float, valueStart : float, timeEnd : float, valueEnd : float) : AnimationCurve

描述:一个直线,开始于timeStart,valueStart并结束于timeEnd, valueEnd.

AnimationEvent



AnimationEvent类似于SendMessage让你调用一个脚本函数,这个脚本是动画播放的一部分。

变量

◆ var
animationState : AnimationState

描述:引发这个事件的动画状态。

当这个方法在动画事件回调之外被调用用时返回null。

◆ var
date:string

描述:存储在动画剪辑中的字符串数据并将被发送到动画事件。

◆ var
functionName : string

描述:被调用的函数的名称

这与调用gameObject.SendMessage(animationEvent.functionName,animationEvent)相同;

◆ var
messageOptions :SendMessageOptions

描述:如果选项被设置为SendMessageOptions.RequireReceiver(缺省),当消息没有被任何组件接收时将打印一个错误消息。

◆ var
time:float

描述:该事件被引发的时间。

构造函数

◆ static
function AnimationEvent () : AnimationEvent

描述:创建一个新的动画事件

AnimationState



AnimationState完全控制动画混合。

在大多数情况下Animation接口就足够了,并且更容易使用。如果你需要完全控制动画播放过程中的混合时,使用AnimationState。

当动画播放时,AnimationState允许你修改速度,权值。时间和层。也可以设置动画合成和wrapMode

动画

变量

◆ var
blendMode : AnimationBlendMode

描述:使用哪个混合模式?

// 设置leanLeft动画为附加混合

animation["leanLeft"].blendMode = AnimationBlendMode.Additive;

◆ var
clip :AnimationClip

描述:该动画状态播放的剪辑。

// 打印动画剪辑的帧频到控制台

print(animation[“walk”]clio.frameRate);

◆ var
enabled : bool

描述:启用/禁用动画

对于需要考虑任何影响的动画,权值需要设置成为一个大于零的值。如果动画被禁用,时间将暂停直到动画再次启用。

// 启用walk循环

animation["Walk"].enabled = true;

animation["Walk"].weight = 1.0;

◆ var
layer : int

描述:动画的层。在计算混合权值时,位于较高层的动画将首先获得它们的权值。

只有较高层的动画没有使用完全全部权值时,较低层的动画才能接收混合权值。

// 放置walk和run动画在层1

animation["Walk"].layer = 1;

animation["Run"].layer = 1;

◆ var
length : float

描述:动画剪辑的长度,以秒计。

// 打印Walk动画的长度

print (animation["Walk"].length);

◆ var
name :string

描述:动画的名称。

◆ var
normalizedSpeed : float

描述:归一化播放速度。

这最常用于混合两个动画时同步播放速度。在多数情况下使用animation.SyncLayer是更容易也更好

// 同步run和walk速度

animation["Run"].normalizedSpeed = animation["Walk"].speed;

◆ var
normalizedTime : float

描述:动画的当前归一化时间。

1为动画的末端。 0.5为动画的中部。

// 快进到动画的中部

animation["Walk"].normalizedTime = 0.5;

◆ var
speed : float

描述:动画的播放速度。1为正常播放速度。

负的播放速度将回放动画。

// 向后走

animation["Walk"].speed = -1.0;

// 以双倍速度行走

animation["Walk"].speed = 2;

◆ var
time :float

描述:动画的当前时间

如果时间大于长度它将按照wrapMode回绕。该值可以大于动画的长度。看这种情况下播放模式将在采样前重映射时间。这个值从0到无穷。

// 回退walk动画

animation["Walk"].time = 0.0;

◆ var
weight : float

描述:动画的权值

// 设置walk动画的混合权值为0.5

animation["Walk"].weight = 0.5;

◆ var
wrapMode : WrapMode

描述:动画的回绕模式

默认的wrapMode被初始化为在Animation组件中设置的回绕模式值。

// 设置walk动画回绕模式为循环

animation["Walk"].wrapMode = WrapMode.Loop;

函数

◆ function
AddMixingTransform (mix : Transform, recursive : bool = true) : 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 = true;

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

isPirated = true;

if (isPirated)

print("Pirated 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 running this from inside of the editor!");

}

◆ static
var isLoadingLevel : bool

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

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

参见: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 runInBackground : bool

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

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

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

Application.runInBackground = true;

◆ 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 = true;

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

isPirated = true;

if (isPirated)

print("Pirated 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 = true;

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 args:object[]):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 ( ) ;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: