2017-05-09 91 views
0
using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 

public class AnimationCamera : MonoBehaviour 
{ 
    public Camera animationCamera; 
    public Camera mainCamera; 
    Animator _anim; 
    List<string> animations = new List<string>(); 

    private void Start() 
    { 
     animationCamera.enabled = false; 
     mainCamera.enabled = true; 
     _anim = GetComponent<Animator>(); 

     foreach (AnimationClip ac in _anim.runtimeAnimatorController.animationClips) 
     { 
      animations.Add(ac.name + " " + ac.length.ToString()); 
     } 
     int cliptoplay = animations[0].IndexOf(" "); 
     string clip = animations[0].Substring(0, cliptoplay); 

    } 

最後在變量字符串剪輯我得到的名字。 而在列表動畫我有每個剪輯的長度。我如何讓List成爲屬性?

但我不知道我是否可以做這樣的事情,如果我只會在代碼中輸入visual studio:clip。 並在點(剪輯)後,我將有一個每個剪輯名稱的選項列表和它的長度。例如,如果我鍵入今天的動畫。我得到的屬性列表如下:動畫。添加或動畫。插入或動畫。索引

我想要做的是創建一些,所以如果我將鍵入剪輯。我將得到所有剪輯名稱和長度的列表,例如:Clip.anim_001_length_10或Clip.myanim_length_21

所以如果我想稍後使用它,將會更容易找到您要使用的剪輯。

+1

答案是否定的,你不能,因爲剪輯是字符串類型,並且值來自'animations'這是一個字符串列表。此外,值不能成爲屬性。屬性只是包含這些值的變量。你的榜樣無法實現。你可以做的是將'動畫'的類型改爲'AnimationClip'的列表,而不是獲取一個'字符串剪輯',你可以獲取一個'AnimationClip剪輯'。通過這樣做,您可以訪問「名稱」和「長度」屬性。 –

回答

0

希望我能正確理解你,但爲什麼不直接使用AnimationClip列表而不是字符串操作呢?

List<AnimationClip> animations = new List<AnimationClip>(); 

後來的後來,你可以通過創建新的AnimationClip對象,然後複製控制器的集合屬性來填充它:

foreach (AnimationClip ac in _anim.runtimeAnimatorController.animationClips) 
{ 
    animations.Add(new AnimationClip {name = ac.name, length = ac.length}); 
} 

現在,如果你想獲得的所有剪輯名稱的列表,你可以做是這樣的:

List<string> clipNames = animations.Select(clip => clip.name).ToList(); 

或者,如果你希望所有剪輯的長度< 30:

List<AnimationClip> shortClips = animations.Where(clip => clip.length < 30).ToList(); 
相關問題