我如何让List成为属性?
问题描述:
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
所以如果我想稍后使用它,将会更容易找到您要使用的剪辑。
答
希望我能正确理解你,但为什么不直接使用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();
答案是否定的,你不能,因为剪辑是字符串类型,并且值来自'animations'这是一个字符串列表。此外,值不能成为属性。属性只是包含这些值的变量。你的榜样无法实现。你可以做的是将'动画'的类型改为'AnimationClip'的列表,而不是获取一个'字符串剪辑',你可以获取一个'AnimationClip剪辑'。通过这样做,您可以访问“名称”和“长度”属性。 –