我正在尝试调用Restart()秒表实例,但在尝试调用时遇到以下错误:
资产/脚本/控件/ SuperTouch.cs(22,59):错误CS1061:键入
System.Diagnostics.Stopwatch' does not contain a definition forRestart'并且找不到扩展方法Restart' of typeSystem.Diagnostics.Stopwatch'(您是否缺少using指令或程序集引用?)
这是我的代码:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
namespace Controls
{
public class SuperTouch
{
public Vector2 position { get { return points [points.Count - 1]; } }
public float duration { get { return (float)stopwatch.ElapsedMilliseconds; } }
public float distance;
public List<Vector2> points = new List<Vector2> ();
public Stopwatch stopwatch = new Stopwatch ();
public void Reset ()
{
points.Clear ();
distance = 0;
stopwatch.Restart ();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我猜你正在使用.Net Framework 3.5或低于不存在的Restart方法Stopwatch.
如果要复制相同的行为,可以这样做.
Stopwatch watch = new Stopwatch();
watch.Start();
// do some things here
// output the elapse if needed
watch = Stopwatch.StartNew(); // creates a new Stopwatch instance
// and starts it upon creation
Run Code Online (Sandbox Code Playgroud)
StartNew静态方法已经存在 .Net Framework 2.0
有关StartNew方法的更多详细信息,请参见:Stopwatch.StartNew方法
或者,您也可以为自己创建扩展方法.
这是一个模型和用法.
public static class ExtensionMethods
{
public static void Restart(this Stopwatch watch)
{
watch.Stop();
watch.Start();
}
}
Run Code Online (Sandbox Code Playgroud)
消费就好
class Program
{
static void Main(string[] args)
{
Stopwatch watch = new Stopwatch();
watch.Restart(); // an extension method
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7375 次 |
| 最近记录: |