Unity,为什么在 c# 脚本中使用 var 动画时某些属性不存在?
Unity, Why in c# script when using the var animation some properties not exist?
我想做的是自动让角色走到一个特定的位置,或者只是走到一个特定的方向。
using UnityEngine;
using System.Collections;
public class Ai : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
var v = Input.GetAxis("Vertical");
if(Mathf.Abs(v) > 0.1f) {
animation["Walk"].speed = v;
animation.CrossFade("Walk");
transform.position.z += v;
}
else animation.CrossFade("Idle");
}
}
速度和 CrossFade 不存在。
更新我的尝试:
using UnityEngine;
using System.Collections;
public class Ai : MonoBehaviour {
Animation animation;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
var v = Input.GetAxis("Vertical");
if(Mathf.Abs(v) > 0.1f) {
animation["Walk"].speed = v;
animation.CrossFade ("Walk");
transform.position.z += v;
}
else animation.CrossFade("Idle");
}
}
将动画设为Animation的全局变量。
现在动画具有属性 speed 和 CrossFade 但现在我收到一个新的警告和一个新的错误:
警告在线:
Animation animation;
Ai.animation' hides inherited member
UnityEngine.Component.animation'。如果有意隐藏,请使用 new 关键字
而错误就在线:
transform.position.z += v;
无法修改“[=32=]”的值类型 return 值。考虑将值存储在临时变量中
因为var
代表匿名类型。它们只会在编译时解析。
正如 Pieter Witvoet 在评论中提到的那样,通常情况下,智能感知对于隐式类型变量应该没有问题。
但在您的情况下,您重叠了两个属性,所以现在 IDE 变得很困惑。
所以现在你的智能感知不是奖励,class 明确你的对象是。
只需将 var animation
替换为 Animation animation
,您的智能感知就会提供所有缺失的功能和属性。
https://msdn.microsoft.com/en-us/library/bb397696.aspx
第二个问题:
transform.position.z += new Vector3(0,0,v);
x、y 和 z 的 Vector3
属性都是只读的。
我想做的是自动让角色走到一个特定的位置,或者只是走到一个特定的方向。
using UnityEngine;
using System.Collections;
public class Ai : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
var v = Input.GetAxis("Vertical");
if(Mathf.Abs(v) > 0.1f) {
animation["Walk"].speed = v;
animation.CrossFade("Walk");
transform.position.z += v;
}
else animation.CrossFade("Idle");
}
}
速度和 CrossFade 不存在。
更新我的尝试:
using UnityEngine;
using System.Collections;
public class Ai : MonoBehaviour {
Animation animation;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
var v = Input.GetAxis("Vertical");
if(Mathf.Abs(v) > 0.1f) {
animation["Walk"].speed = v;
animation.CrossFade ("Walk");
transform.position.z += v;
}
else animation.CrossFade("Idle");
}
}
将动画设为Animation的全局变量。 现在动画具有属性 speed 和 CrossFade 但现在我收到一个新的警告和一个新的错误:
警告在线:
Animation animation;
Ai.animation' hides inherited member
UnityEngine.Component.animation'。如果有意隐藏,请使用 new 关键字
而错误就在线:
transform.position.z += v;
无法修改“[=32=]”的值类型 return 值。考虑将值存储在临时变量中
因为var
代表匿名类型。它们只会在编译时解析。
正如 Pieter Witvoet 在评论中提到的那样,通常情况下,智能感知对于隐式类型变量应该没有问题。
但在您的情况下,您重叠了两个属性,所以现在 IDE 变得很困惑。
所以现在你的智能感知不是奖励,class 明确你的对象是。
只需将 var animation
替换为 Animation animation
,您的智能感知就会提供所有缺失的功能和属性。
https://msdn.microsoft.com/en-us/library/bb397696.aspx
第二个问题:
transform.position.z += new Vector3(0,0,v);
x、y 和 z 的 Vector3
属性都是只读的。