I'm trying to call Restart()
on a stopwatch instance but am getting the following error when trying to call it:
Assets/Scripts/Controls/SuperTouch.cs(22,59): error CS1061: Type
System.Diagnostics.Stopwatch' does not contain a definition for
Restart' and no extension methodRestart' of type
System.Diagnostics.Stopwatch' could be found (are you missing a using directive or an assembly reference?)
This is my code:
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 ();
}
}
}
I'm guessing you are using .Net Framework 3.5
or below where the Restart
method of Stopwatch
doesnt exists.
If you want to replicate the same behavior you can do it like this.
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
The StartNew static method already exists on .Net Framework 2.0
More details about StartNew method here: Stopwatch.StartNew Method
Or alternatively, you can create an extension method for yourself.
Here is a mockup and usage.
public static class ExtensionMethods
{
public static void Restart(this Stopwatch watch)
{
watch.Stop();
watch.Start();
}
}
Consume like
class Program
{
static void Main(string[] args)
{
Stopwatch watch = new Stopwatch();
watch.Restart(); // an extension method
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加