遊戲設計社
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Unity 3D 腳本教學 (四)

向下

Unity 3D 腳本教學 (四) Empty Unity 3D 腳本教學 (四)

發表  aaa1218bbb 周五 11月 25, 2011 2:09 pm

運行時類
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不會立即發生一個新的關卡在當前遊戲幀之後被加載。如果關卡加載所請求的幀已經完成isL​​oadingLevel返回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中的函數只需要使用標準的語法即可,例如:

// 使用來自Unity的調用,這將接受
// "Hello from Unity!" 做為參數
function MyFunction2( arg )
{
alert( arg );
}
-->

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 ( ) ;
}
}
aaa1218bbb
aaa1218bbb
社員
社員

文章數 : 65
積分 : 4747
注冊日期 : 2011-11-22

https://design.666forum.com

回頂端 向下

回頂端


 
這個論壇的權限:
無法 在這個版面回復文章